├── .gitignore
├── .idea
├── codeStyles
│ └── Project.xml
├── dbnavigator.xml
└── runConfigurations.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── francoisdexemple
│ │ └── materialpreferencedemo
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── ic_launcher-web.png
│ ├── java
│ │ └── com
│ │ │ └── francoisdexemple
│ │ │ └── materialpreferencedemo
│ │ │ ├── Application.java
│ │ │ ├── Demo.java
│ │ │ ├── ExampleMaterialPreferenceActivity.java
│ │ │ ├── ExampleMaterialPreferenceFragment.java
│ │ │ ├── ExampleMaterialPreferenceFragmentActivity.java
│ │ │ ├── ExampleMaterialPreferenceLicenseActivity.java
│ │ │ ├── MainActivity.java
│ │ │ └── custom
│ │ │ ├── MyCustomItem.java
│ │ │ └── MyViewTypeManager.java
│ ├── res
│ │ ├── drawable-v24
│ │ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ │ ├── activity_example_material_preference_fragment.xml
│ │ │ ├── activity_main.xml
│ │ │ ├── content_example_material_preference_fragment.xml
│ │ │ ├── content_main.xml
│ │ │ ├── custom_item.xml
│ │ │ ├── mp_material_preference_action_item_overridefornoicons.xml
│ │ │ └── mp_material_preference_list_card_overrideforlistlayout.xml
│ │ ├── menu
│ │ │ ├── menu_example_material_preference.xml
│ │ │ └── menu_main.xml
│ │ ├── mipmap-anydpi-v26
│ │ │ ├── ic_launcher.xml
│ │ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ ├── ic_launcher_foreground.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.png
│ │ │ ├── ic_launcher_foreground.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ ├── ic_launcher_foreground.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ ├── ic_launcher_foreground.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ ├── ic_launcher_foreground.png
│ │ │ └── ic_launcher_round.png
│ │ ├── values
│ │ │ ├── colors.xml
│ │ │ ├── dimens.xml
│ │ │ ├── ic_launcher_background.xml
│ │ │ ├── strings.xml
│ │ │ └── styles.xml
│ │ └── xml
│ │ │ └── backup_descriptor.xml
│ └── web_hi_res_512.png
│ └── test
│ └── java
│ └── com
│ └── francoisdexemple
│ └── materialpreferencedemo
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── materialpreference
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── francoisdexemple
│ │ └── materialpreference
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── francoisdexemple
│ │ │ └── materialpreference
│ │ │ ├── ConvenienceBuilder.java
│ │ │ ├── MaterialPreferenceActivity.java
│ │ │ ├── MaterialPreferenceFragment.java
│ │ │ ├── adapters
│ │ │ ├── MaterialPreferenceItemAdapter.java
│ │ │ └── MaterialPreferenceListAdapter.java
│ │ │ ├── holders
│ │ │ └── MaterialPreferenceItemViewHolder.java
│ │ │ ├── items
│ │ │ ├── MaterialPreferenceActionItem.java
│ │ │ ├── MaterialPreferenceCheckBoxItem.java
│ │ │ ├── MaterialPreferenceItem.java
│ │ │ ├── MaterialPreferenceItemOnClickAction.java
│ │ │ ├── MaterialPreferenceOnCheckedChangedListener.java
│ │ │ ├── MaterialPreferenceSwitchItem.java
│ │ │ └── MaterialPreferenceTitleItem.java
│ │ │ ├── model
│ │ │ ├── MaterialPreferenceCard.java
│ │ │ └── MaterialPreferenceList.java
│ │ │ └── util
│ │ │ ├── DefaultViewTypeManager.java
│ │ │ ├── OpenSourceLicense.java
│ │ │ └── ViewTypeManager.java
│ └── res
│ │ ├── layout
│ │ ├── material_preference_action_item.xml
│ │ ├── material_preference_activity.xml
│ │ ├── material_preference_checkbox_item.xml
│ │ ├── material_preference_fragment.xml
│ │ ├── material_preference_list_card.xml
│ │ ├── material_preference_switch_item.xml
│ │ └── material_preference_title_item.xml
│ │ └── values
│ │ ├── attrs.xml
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── francoisdexemple
│ └── materialpreference
│ └── ExampleUnitTest.java
├── screenshots
├── 1.png
├── 2.png
├── 2_mini.png
├── 3.png
├── 4.png
├── 5.png
└── 6.png
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/libraries
5 | /.idea/modules.xml
6 | /.idea/workspace.xml
7 | .DS_Store
8 | /build
9 | /captures
10 | .externalNativeBuild
11 |
12 |
13 | # Created by https://www.gitignore.io/api/android,windows,androidstudio
14 |
15 | ### Android ###
16 | # Built application files
17 | *.apk
18 | *.ap_
19 |
20 | # Files for the ART/Dalvik VM
21 | *.dex
22 |
23 | # Java class files
24 | *.class
25 |
26 | # Generated files
27 | bin/
28 | gen/
29 | out/
30 |
31 | # Gradle files
32 | .gradle/
33 | build/
34 |
35 | # Local configuration file (sdk path, etc)
36 | local.properties
37 |
38 | # Proguard folder generated by Eclipse
39 | proguard/
40 |
41 | # Log Files
42 | *.log
43 |
44 | # Android Studio Navigation editor temp files
45 | .navigation/
46 |
47 | # Android Studio captures folder
48 | captures/
49 |
50 | # Intellij
51 | *.iml
52 | .idea/workspace.xml
53 | .idea/tasks.xml
54 | .idea/gradle.xml
55 | .idea/dictionaries
56 | .idea/libraries
57 |
58 | # External native build folder generated in Android Studio 2.2 and later
59 | .externalNativeBuild
60 |
61 | # Freeline
62 | freeline.py
63 | freeline/
64 | freeline_project_description.json
65 |
66 | ### Android Patch ###
67 | gen-external-apklibs
68 |
69 | ### AndroidStudio ###
70 | # Covers files to be ignored for android development using Android Studio.
71 |
72 | # Built application files
73 |
74 | # Files for the ART/Dalvik VM
75 |
76 | # Java class files
77 |
78 | # Generated files
79 |
80 | # Gradle files
81 | .gradle
82 |
83 | # Signing files
84 | .signing/
85 |
86 | # Local configuration file (sdk path, etc)
87 |
88 | # Proguard folder generated by Eclipse
89 |
90 | # Log Files
91 |
92 | # Android Studio
93 | /*/build/
94 | /*/local.properties
95 | /*/out
96 | /*/*/build
97 | /*/*/production
98 | *.ipr
99 | *~
100 | *.swp
101 |
102 | # Android Patch
103 |
104 | # External native build folder generated in Android Studio 2.2 and later
105 |
106 | # NDK
107 | obj/
108 |
109 | # IntelliJ IDEA
110 | *.iws
111 | /out/
112 |
113 | # User-specific configurations
114 | .idea/caches/
115 | .idea/libraries/
116 | .idea/shelf/
117 | .idea/.name
118 | .idea/compiler.xml
119 | .idea/copyright/profiles_settings.xml
120 | .idea/encodings.xml
121 | .idea/misc.xml
122 | .idea/modules.xml
123 | .idea/scopes/scope_settings.xml
124 | .idea/vcs.xml
125 | .idea/jsLibraryMappings.xml
126 | .idea/datasources.xml
127 | .idea/dataSources.ids
128 | .idea/sqlDataSources.xml
129 | .idea/dynamic.xml
130 | .idea/uiDesigner.xml
131 |
132 | # OS-specific files
133 | .DS_Store
134 | .DS_Store?
135 | ._*
136 | .Spotlight-V100
137 | .Trashes
138 | ehthumbs.db
139 | Thumbs.db
140 |
141 | # Legacy Eclipse project files
142 | .classpath
143 | .project
144 | .cproject
145 | .settings/
146 |
147 | # Mobile Tools for Java (J2ME)
148 | .mtj.tmp/
149 |
150 | # Package Files #
151 | *.war
152 | *.ear
153 |
154 | # virtual machine crash logs (Reference: http://www.java.com/en/download/help/error_hotspot.xml)
155 | hs_err_pid*
156 |
157 | ## Plugin-specific files:
158 |
159 | # mpeltonen/sbt-idea plugin
160 | .idea_modules/
161 |
162 | # JIRA plugin
163 | atlassian-ide-plugin.xml
164 |
165 | # Mongo Explorer plugin
166 | .idea/mongoSettings.xml
167 |
168 | # Crashlytics plugin (for Android Studio and IntelliJ)
169 | com_crashlytics_export_strings.xml
170 | crashlytics.properties
171 | crashlytics-build.properties
172 | fabric.properties
173 |
174 | ### AndroidStudio Patch ###
175 |
176 | !/gradle/wrapper/gradle-wrapper.jar
177 |
178 | ### Windows ###
179 | # Windows thumbnail cache files
180 | ehthumbs_vista.db
181 |
182 | # Folder config file
183 | Desktop.ini
184 |
185 | # Recycle Bin used on file shares
186 | $RECYCLE.BIN/
187 |
188 | # Windows Installer files
189 | *.cab
190 | *.msi
191 | *.msm
192 | *.msp
193 |
194 | # Windows shortcuts
195 | *.lnk
196 |
197 |
198 | # End of https://www.gitignore.io/api/android,windows,androidstudio
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # material-preference-library : DEPRECATED PLEASE LOOK HERE FOR THE SAME THING [material-about-library from daniel-stoneuk][6]
2 |
3 | Makes it easy to create a beautiful preference screen for your app. Generates an `Activity` or `Fragment`.
4 |
5 | Fork from here: [material-about-library from daniel-stoneuk][6]
6 |
7 | If you use this library in your app, please let me know and I'll add it to the list.
8 |
9 |
10 | ## Screenshots
11 |
12 | | Light with Light Action Bar | Light with Dark Action Bar | Dark with Light Action Bar | Dark with Dark Action Bar | Custom Cardview Background | Custom card & action item layout |
13 | | ------------------------------ | ------------------------------ | ------------------------------ | ------------------------------ | ------------------------------ | ------------------------------ |
14 | |  |  |  |  |  |  |
15 |
16 | ## Features
17 |
18 | - Material design
19 | - Modular backend
20 | - Easy to implement
21 | - Simple but intuitive API
22 | - Dynamic item support
23 | - Support Switch and Checkbox (more after)
24 |
25 | ## Dependency
26 |
27 | _material-preference-library_ is available on [**jitpack.io**][1] and has support for different support library versions. See the full list [here](https://jitpack.io/#filol/material-preference-library).
28 |
29 | **Gradle dependency:**
30 |
31 | ```gradle
32 | allprojects {
33 | repositories {
34 | maven { url 'https://jitpack.io' }
35 | }
36 | }
37 | ```
38 |
39 | ```gradle
40 | dependencies {
41 | implementation 'com.github.filol:material-preference-library:1.0'
42 | }
43 | ```
44 |
45 | ## Setup
46 |
47 | ### Activity
48 |
49 | Your `Activity` must extend [`MaterialPreferenceActivity`][materialpreferenceactivityjava] and be in your _AndroidManifest.xml_:
50 |
51 | ```java
52 | public class ExampleMaterialPreferenceActivity extends MaterialPreferenceActivity {
53 |
54 | @Override
55 | @NonNull
56 | protected MaterialPreferenceList getMaterialPreferenceList(@NonNull Context context) {
57 | return new MaterialPreferenceList.Builder()
58 | .build(); // This creates an empty screen, add cards with .addCard()
59 | }
60 |
61 | @Override
62 | protected CharSequence getActivityTitle() {
63 | return getString(R.string.mp_title_preference);
64 | }
65 |
66 | }
67 | ```
68 |
69 | Ensure that the theme extends either of these themes, and apply primary & accent colours:
70 |
71 | - `Theme.mp.Light` (light theme with light toolbar)
72 | - `Theme.mp.Light.DarkActionBar` (light theme with dark toolbar)
73 | - `Theme.mp.Dark` (dark theme with dark toolbar)
74 | - `Theme.mp.Dark.LightActionBar` (dark theme with light toolbar)
75 |
76 | ```xml
77 |
78 |
79 |
81 |
82 |
83 | ```
84 |
85 | ```xml
86 |
91 | ```
92 |
93 | ### Fragment
94 |
95 | Your `Fragment` must extend [`MaterialPreferenceFragment`][materialpreferencefragmentjava].
96 |
97 | ```java
98 | public class ExampleMaterialPreferenceFragment extends MaterialPreferenceFragment {
99 |
100 | @Override
101 | protected MaterialPreferenceList getMaterialPreferenceList(final Context activityContext) {
102 | return new MaterialPreferenceList.Builder()
103 | .build(); // This creates an empty screen, add cards with .addCard()
104 | }
105 |
106 | @Override
107 | protected int getTheme() {
108 | return R.style.AppTheme_MaterialPreferenceActivity_Fragment;
109 | }
110 |
111 | }
112 | ```
113 |
114 | Pass in a theme that extends one of the styles above
115 |
116 | ```xml
117 |
122 | ```
123 |
124 | ### Add Cards:
125 |
126 | Start building a "card" using [`MaterialPreferenceCard.Builder()`][8]
127 |
128 | ```java
129 | public class ExampleMaterialPreferenceActivity extends MaterialPreferenceActivity {
130 |
131 | @Override
132 | @NonNull
133 | protected MaterialPreferenceList getMaterialPreferenceList(@NonNull Context context) {
134 | MaterialPreferenceCard card = new MaterialPreferenceCard.Builder()
135 | // Configure card here
136 | .build();
137 |
138 | return new MaterialPreferenceList.Builder()
139 | .addCard(card)
140 | .build();
141 | }
142 | }
143 | ```
144 |
145 | Give the card a title by calling `.title()` on the `MaterialPreferenceCard.Builder`
146 |
147 | ```java
148 | MaterialPreferenceCard card = new MaterialPreferenceCard.Builder()
149 | .title("Author")
150 | .build();
151 | ```
152 |
153 | ### Add Items to a card:
154 |
155 | There are currently 4 types of items you can add to a card - [`MaterialPreferenceTitleItem`][9], [`MaterialPreferenceActionItem`][10], [`MaterialPreferenceCheckboxItem`][12] and [`MaterialPreferenceSwitchItem`][13]. Other types of items are planned, for example "person" items which feature buttons to showcase a single person. Feel free to submit a PR or Issue for more item ideas.
156 |
157 | - `MaterialPreferenceActionItem`: Standard item with text, icon and optional subtext.
158 | - `MaterialPreferenceTitleItem`: Larger item with large icon (e.g. app icon) and larger text.
159 |
160 | [`MaterialPreferenceTitleItem`][9] is created with [`MaterialPreferenceTitleItem.Builder()`][9] and lets you specify **text** and an **icon**.
161 |
162 | ```java
163 | MaterialPreferenceCard.Builder cardBuilder = new MaterialPreferenceCard.Builder();
164 | cardBuilder.addItem(new MaterialPreferenceTitleItem.Builder()
165 | .text("Material Preference Library")
166 | .icon(R.mipmap.ic_launcher)
167 | .build());
168 | ```
169 |
170 | [`MaterialPreferenceItem`][10] is created with [`MaterialPreferenceItem.Builder()`][10] and lets you specify **text**, **sub-text**, an **icon** and an **OnClickAction**.
171 |
172 | ```java
173 | cardBuilder.addItem(new MaterialPreferenceActionItem.Builder()
174 | .text("Version")
175 | .subText("1.0.0")
176 | .icon(R.drawable.ic_about_info)
177 | .setOnClickAction(new MaterialPreferenceActionItem.OnClickAction() {
178 | @Override
179 | public void onClick() {
180 | Toast.makeText(ExampleMaterialPreferenceActivity.this, "Version Tapped", Toast.LENGTH_SHORT)
181 | .show();
182 | }
183 | })
184 | .build());
185 | ```
186 |
187 | [`MaterialPreferenceCheckboxItem`][12] is created with [`MaterialPreferenceCheckboxItem.Builder()`][12] and lets you specify **text**, **sub-text**, an **icon** and an **onCheckedChangedListener**.
188 |
189 | ```java
190 | cardBuilder.addItem(new MaterialPreferenceCheckboxItem.Builder()
191 | .text("Activate something")
192 | .subText("desciption")
193 | .icon(R.drawable.ic_about_info)
194 | .setOnCheckedChanged(new MaterialPreferenceOnCheckedChangedListener() {
195 | @Override
196 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
197 | Toast.makeText(c,"Now : "+isChecked,Toast.LENGTH_SHORT).show();
198 | }
199 | })
200 | .build());
201 | ```
202 |
203 |
204 | [`MaterialPreferenceSwitchItem`][10] is created with [`MaterialPreferenceSwitchItem.Builder()`][10] and lets you specify **text**, **sub-text**, an **icon** and an **onCheckedChangedListener**.
205 |
206 | ```java
207 | cardBuilder.addItem(new MaterialPreferenceSwitchItem.Builder()
208 | .text("Version")
209 | .subText("1.0.0")
210 | .icon(R.drawable.ic_about_info)
211 | .setOnCheckedChanged(new MaterialPreferenceOnCheckedChangedListener() {
212 | @Override
213 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
214 | Toast.makeText(c,"Now : "+isChecked,Toast.LENGTH_SHORT).show();
215 | }
216 | })
217 | .build());
218 | ```
219 |
220 | ### Return the list:
221 |
222 | Create a [`MaterialPreferenceList`][11] using [`MaterialPreferenceList.Builder()`][11], passing in the cards you would like to display.
223 |
224 | ```java
225 | MaterialPreferenceCard card = new MaterialPreferenceCard.Builder()
226 | .title("Hey! I'm a card")
227 | .build();
228 |
229 | return new MaterialPreferenceList.Builder()
230 | .addCard(card)
231 | .build();
232 | ```
233 |
234 | Check out a working example in [`Demo.java`][3].
235 |
236 | **Tip:** You can either use _Strings_ / _Drawables_ or _Resources_ when creating `MaterialPreferenceItem`'s
237 |
238 | **Tip:** Use [Android-Iconics][iconics] for icons. "Android-Iconics - Use any icon font, or vector (.svg) as drawable in your application."
239 |
240 | **Tip:** Use [ConvenienceBuilder][conveniencebuilderjava] to easily create items or OnClickActions.
241 |
242 | **Tip:** Customise text colour and card colour in your styles. Example below:
243 |
244 | ```xml
245 |
259 | ```
260 |
261 | ### Dynamic items:
262 | It's possible to create dynamic items that either change on tap (or when any other event occurs). There are two examples in the sample application. Simply change the items in the list variable and then call `refreshMaterialPreferenceList()`. DiffUtil calculates the changes to animate in the RecyclerView.
263 |
264 | ```java
265 | final MaterialPreferenceActionItem item = new MaterialPreferenceActionItem.Builder()
266 | .text("Dynamic UI")
267 | .subText(subText)
268 | .icon(new IconicsDrawable(c)
269 | .icon(CommunityMaterial.Icon.cmd_refresh)
270 | .color(ContextCompat.getColor(c, R.color.mp_color_icon_dark_theme)
271 | ).sizeDp(18))
272 | .build();
273 | item.setOnClickAction(new MaterialPreferenceItemOnClickAction() {
274 | @Override
275 | public void onClick() {
276 | item.setSubText("Random number: " + ((int) (Math.random() * 10)));
277 | refreshMaterialPreferenceList();
278 | }
279 | });
280 | ```
281 |
282 | ### Custom card and Action layout
283 | To get a layout that is similar to the 6th screenshot above simply override the files `mp_material_preference_action_item` and `mp_material_preference_list_card` by creating new layout resources with the same filename. See [here](https://github.com/filol/material-preference-library/tree/master/materialpreference/src/main/res/layout).
284 |
285 | ## Contributors
286 |
287 | ### This Library
288 | - François Dexemple ([@filol](https://github.com/filol))
289 |
290 | ### Original Library
291 | - Daniel Stone ([@daniel-stoneuk](https://github.com/daniel-stoneuk))
292 | - Robert Pösel ([@Robyer](https://github.com/Robyer))
293 | - Jonas Uekötter ([@ueman](https://github.com/ueman))
294 | - Rainer Lang ([@Rainer-Lang](https://github.com/rainer-lang))
295 | - Sebastian Guillen ([@code-schreiber](https://github.com/code-schreiber))
296 | - and [others](https://github.com/daniel-stoneuk/material-about-library/graphs/contributors)
297 |
298 | ## License
299 |
300 | ```
301 | Copyright 2016-2018 François Dexemple
302 |
303 | Licensed under the Apache License, Version 2.0 (the "License");
304 | you may not use this file except in compliance with the License.
305 | You may obtain a copy of the License at
306 |
307 | http://www.apache.org/licenses/LICENSE-2.0
308 |
309 | Unless required by applicable law or agreed to in writing, software
310 | distributed under the License is distributed on an "AS IS" BASIS,
311 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
312 | See the License for the specific language governing permissions and
313 | limitations under the License.
314 | ```
315 |
316 | [1]: https://jitpack.io
317 | [2]: http://i.imgur.com/xXWDmLb.png
318 | [3]: https://github.com/filol/material-preference-library/blob/master/app/src/main/java/com/francoisdexemple/materialpreferencedemo/Demo.java
319 | [4]: http://i.imgur.com/HEm08Ax.png
320 | [5]: https://play.google.com/store/apps/details?id=com.danielstone.energyhive
321 | [6]: https://github.com/daniel-stoneuk/material-about-library
322 | [8]: https://github.com/filol/material-preference-library/blob/master/materialpreference/src/main/java/com/francoisdexemple/materialpreference/model/MaterialPreferenceCard.java
323 | [9]: https://github.com/filol/material-preference-library/blob/master/materialpreference/src/main/java/com/francoisdexemple/materialpreference/items/MaterialPreferenceTitleItem.java
324 | [10]: https://github.com/filol/material-preference-library/blob/master/materialpreference/src/main/java/com/francoisdexemple/materialpreference/items/MaterialPreferenceActionItem.java
325 | [11]: https://github.com/filol/material-preference-library/blob/master/materialpreference/src/main/java/com/francoisdexemple/materialpreference/model/MaterialPreferenceList.java
326 | [12]: https://github.com/filol/material-preference-library/blob/master/materialpreference/src/main/java/com/francoisdexemple/materialpreference/items/MaterialPreferenceCheckBoxItem.java
327 | [13]: https://github.com/filol/material-preference-library/blob/master/materialpreference/src/main/java/com/francoisdexemple/materialpreference/items/MaterialPreferenceSwitchItem.java
328 | [conveniencebuilderjava]: https://github.com/filol/material-preference-library/blob/master/materialpreference/src/main/java/com/francoisdexemple/materialpreference/ConvenienceBuilder.java
329 | [iconics]: https://github.com/mikepenz/Android-Iconics
330 | [materialpreferenceactivityjava]: https://github.com/filol/material-preference-library/blob/master/app/src/main/java/com/francoisdexemple/materialpreferencedemo/ExampleMaterialPreferenceActivity.java
331 | [materialpreferencefragmentjava]: https://github.com/filol/material-preference-library/blob/master/app/src/main/java/com/francoisdexemple/materialpreferencedemo/ExampleMaterialPreferenceFragment.java
332 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | defaultConfig {
6 | applicationId "com.francoisdexemple.materialpreferencedemo"
7 | minSdkVersion 19
8 | targetSdkVersion 27
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(include: ['*.jar'], dir: 'libs')
23 | implementation 'com.android.support:appcompat-v7:27.1.1'
24 | implementation 'com.android.support.constraint:constraint-layout:1.1.0'
25 | testImplementation 'junit:junit:4.12'
26 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
28 | implementation project(':materialpreference')
29 |
30 | implementation "com.android.support:appcompat-v7:$supportLibVersion"
31 | implementation "com.android.support:design:$supportLibVersion"
32 | implementation "com.mikepenz:iconics-core:2.9.5@aar"
33 | implementation 'com.mikepenz:community-material-typeface:2.0.46.1@aar'
34 |
35 | debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanaryVersion"
36 | releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakCanaryVersion"
37 | testImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakCanaryVersion"
38 | }
39 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/francoisdexemple/materialpreferencedemo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreferencedemo;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.francoisdexemple.materialpreferencedemo", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
15 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
28 |
31 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/app/src/main/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/ic_launcher-web.png
--------------------------------------------------------------------------------
/app/src/main/java/com/francoisdexemple/materialpreferencedemo/Application.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreferencedemo;
2 |
3 | import com.squareup.leakcanary.LeakCanary;
4 |
5 | public class Application extends android.app.Application {
6 |
7 | @Override public void onCreate() {
8 | super.onCreate();
9 | if (LeakCanary.isInAnalyzerProcess(this)) {
10 | // This process is dedicated to LeakCanary for heap analysis.
11 | // You should not init your app in this process.
12 | return;
13 | }
14 | LeakCanary.install(this);
15 | // Normal app init code...
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/app/src/main/java/com/francoisdexemple/materialpreferencedemo/ExampleMaterialPreferenceActivity.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreferencedemo;
2 | import android.content.Context;
3 | import android.net.Uri;
4 | import android.os.Bundle;
5 | import android.support.annotation.NonNull;
6 | import android.support.annotation.Nullable;
7 | import android.support.design.widget.Snackbar;
8 | import android.support.v4.content.ContextCompat;
9 | import android.util.Log;
10 | import android.view.Menu;
11 | import android.view.MenuItem;
12 | import android.widget.Toast;
13 |
14 | import com.francoisdexemple.materialpreference.ConvenienceBuilder;
15 | import com.francoisdexemple.materialpreference.MaterialPreferenceActivity;
16 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceActionItem;
17 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceItemOnClickAction;
18 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceTitleItem;
19 | import com.francoisdexemple.materialpreference.model.MaterialPreferenceCard;
20 | import com.francoisdexemple.materialpreference.model.MaterialPreferenceList;
21 | import com.francoisdexemple.materialpreference.util.ViewTypeManager;
22 | import com.francoisdexemple.materialpreferencedemo.custom.MyCustomItem;
23 | import com.francoisdexemple.materialpreferencedemo.custom.MyViewTypeManager;
24 | import com.mikepenz.community_material_typeface_library.CommunityMaterial;
25 | import com.mikepenz.iconics.IconicsDrawable;
26 |
27 | public class ExampleMaterialPreferenceActivity extends MaterialPreferenceActivity {
28 |
29 | public static final String THEME_EXTRA = "";
30 | public static final int THEME_LIGHT_LIGHTBAR = 0;
31 | public static final int THEME_LIGHT_DARKBAR = 1;
32 | public static final int THEME_DARK_LIGHTBAR = 2;
33 | public static final int THEME_DARK_DARKBAR = 3;
34 | public static final int THEME_CUSTOM_CARDVIEW = 4;
35 |
36 | protected int colorIcon = R.color.mp_color_icon_light_theme;
37 |
38 | @NonNull
39 | @Override
40 | protected MaterialPreferenceList getMaterialPreferenceList(@NonNull final Context c) {
41 | MaterialPreferenceCard.Builder advancedCardBuilder = new MaterialPreferenceCard.Builder();
42 | advancedCardBuilder.title("Advanced");
43 |
44 | advancedCardBuilder.addItem(new MaterialPreferenceTitleItem.Builder()
45 | .text("TitleItem OnClickAction")
46 | .icon(R.mipmap.ic_launcher)
47 | .setOnClickAction(ConvenienceBuilder.createWebsiteOnClickAction(c, Uri.parse("http://www.daniel-stone.uk")))
48 | .build());
49 |
50 | advancedCardBuilder.addItem(new MaterialPreferenceActionItem.Builder()
51 | .text("Snackbar demo")
52 | .icon(new IconicsDrawable(c)
53 | .icon(CommunityMaterial.Icon.cmd_code_tags)
54 | .color(ContextCompat.getColor(c, colorIcon))
55 | .sizeDp(18))
56 | .setOnClickAction(new MaterialPreferenceItemOnClickAction() {
57 | @Override
58 | public void onClick() {
59 | Snackbar.make(((ExampleMaterialPreferenceActivity) c).findViewById(R.id.mp_material_preference_activity_coordinator_layout), "Test", Snackbar.LENGTH_SHORT).show();
60 | }
61 | })
62 | .build());
63 |
64 | advancedCardBuilder.addItem(new MaterialPreferenceActionItem.Builder()
65 | .text("OnLongClickAction demo")
66 | .icon(new IconicsDrawable(c)
67 | .icon(CommunityMaterial.Icon.cmd_hand_pointing_right)
68 | .color(ContextCompat.getColor(c, colorIcon))
69 | .sizeDp(18))
70 | .setOnLongClickAction(new MaterialPreferenceItemOnClickAction() {
71 | @Override
72 | public void onClick() {
73 | Toast.makeText(c, "Long pressed", Toast.LENGTH_SHORT).show();
74 | }
75 | })
76 | .build());
77 |
78 | advancedCardBuilder.addItem(new MyCustomItem.Builder()
79 | .text("Custom Item")
80 | .icon(new IconicsDrawable(c)
81 | .icon(CommunityMaterial.Icon.cmd_code_braces)
82 | .color(ContextCompat.getColor(c, colorIcon))
83 | .sizeDp(18))
84 | .build());
85 |
86 | advancedCardBuilder.addItem(createDynamicItem("Tap for a random number & swap position", c));
87 |
88 | return Demo.createMaterialPreferenceList(c, colorIcon, getIntent().getIntExtra(THEME_EXTRA, THEME_LIGHT_DARKBAR)).addCard(advancedCardBuilder.build());
89 | }
90 |
91 | private MaterialPreferenceActionItem createDynamicItem(String subText, final Context c) {
92 | final MaterialPreferenceActionItem item = new MaterialPreferenceActionItem.Builder()
93 | .text("Dynamic UI")
94 | .subText(subText)
95 | .icon(new IconicsDrawable(c)
96 | .icon(CommunityMaterial.Icon.cmd_refresh)
97 | .color(ContextCompat.getColor(c, colorIcon)
98 | ).sizeDp(18))
99 | .build();
100 | item.setOnClickAction(new MaterialPreferenceItemOnClickAction() {
101 | @Override
102 | public void onClick() {
103 | getList().getCards().get(4).getItems().remove(getList().getCards().get(4).getItems().indexOf(item));
104 | int newIndex = ((int) (Math.random() * 5));
105 | getList().getCards().get(4).getItems().add(newIndex, item);
106 | item.setSubText("Random number: " + ((int) (Math.random() * 10)));
107 | setMaterialPreferenceList(getList());
108 | }
109 | });
110 |
111 | return item;
112 | }
113 |
114 |
115 | @Override
116 | protected void onCreate(@Nullable Bundle savedInstanceState) {
117 | Log.i("test", "onCreate: " + getIntent().getIntExtra(THEME_EXTRA, THEME_LIGHT_DARKBAR));
118 | switch (getIntent().getIntExtra(THEME_EXTRA, THEME_LIGHT_DARKBAR)) {
119 | case THEME_LIGHT_LIGHTBAR:
120 | setTheme(R.style.AppTheme_MaterialPreferenceActivity_Light);
121 | colorIcon = R.color.mp_color_icon_light_theme;
122 | break;
123 | case THEME_DARK_LIGHTBAR:
124 | setTheme(R.style.AppTheme_MaterialPreferenceActivity_Dark_LightActionBar);
125 | colorIcon = R.color.mp_color_icon_dark_theme;
126 | break;
127 | case THEME_LIGHT_DARKBAR:
128 | setTheme(R.style.AppTheme_MaterialPreferenceActivity_Light_DarkActionBar);
129 | colorIcon = R.color.mp_color_icon_light_theme;
130 | break;
131 | case THEME_DARK_DARKBAR:
132 | setTheme(R.style.AppTheme_MaterialPreferenceActivity_Dark);
133 | colorIcon = R.color.mp_color_icon_dark_theme;
134 | break;
135 | case THEME_CUSTOM_CARDVIEW:
136 | setTheme(R.style.AppTheme_MaterialPreferenceActivity_Light_DarkActionBar_CustomCardView);
137 | colorIcon = R.color.mp_color_icon_dark_theme;
138 | break;
139 | }
140 |
141 | super.onCreate(savedInstanceState);
142 |
143 | // Call this method to let the scrollbar scroll off screen
144 | // setScrollToolbar(true);
145 | }
146 |
147 | @Override
148 | protected CharSequence getActivityTitle() {
149 | return getString(R.string.mp_title_preference);
150 | }
151 |
152 | @NonNull
153 | @Override
154 | protected ViewTypeManager getViewTypeManager() {
155 | return new MyViewTypeManager();
156 | }
157 |
158 | @Override
159 | public boolean onCreateOptionsMenu(Menu menu) {
160 | super.onCreateOptionsMenu(menu);
161 | getMenuInflater().inflate(R.menu.menu_example_material_preference, menu);
162 | return true;
163 | }
164 |
165 | @Override
166 | public boolean onOptionsItemSelected(MenuItem item) {
167 | if (item.getItemId() == R.id.action_refresh) {
168 | refreshMaterialPreferenceList();
169 | return true;
170 | }
171 | return super.onOptionsItemSelected(item);
172 | }
173 | }
174 |
--------------------------------------------------------------------------------
/app/src/main/java/com/francoisdexemple/materialpreferencedemo/ExampleMaterialPreferenceFragment.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreferencedemo;
2 | import android.content.Context;
3 | import android.os.Handler;
4 | import android.support.v4.content.ContextCompat;
5 | import android.util.Log;
6 |
7 |
8 | import com.francoisdexemple.materialpreference.MaterialPreferenceFragment;
9 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceActionItem;
10 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceItemOnClickAction;
11 | import com.francoisdexemple.materialpreference.model.MaterialPreferenceList;
12 | import com.mikepenz.community_material_typeface_library.CommunityMaterial;
13 | import com.mikepenz.iconics.IconicsDrawable;
14 |
15 | import static com.francoisdexemple.materialpreferencedemo.ExampleMaterialPreferenceActivity.THEME_LIGHT_DARKBAR;
16 |
17 |
18 | public class ExampleMaterialPreferenceFragment extends MaterialPreferenceFragment {
19 |
20 | private MaterialPreferenceActionItem createDynamicItem(String subText, final Context c) {
21 | final MaterialPreferenceActionItem item = new MaterialPreferenceActionItem.Builder()
22 | .text("Dynamic UI")
23 | .subText(subText)
24 | .icon(new IconicsDrawable(c)
25 | .icon(CommunityMaterial.Icon.cmd_refresh)
26 | .color(ContextCompat.getColor(c, R.color.mp_color_icon_dark_theme)
27 | ).sizeDp(18))
28 | .build();
29 | item.setOnClickAction(new MaterialPreferenceItemOnClickAction() {
30 | @Override
31 | public void onClick() {
32 | item.setSubText("Random number: " + ((int) (Math.random() * 10)));
33 | refreshMaterialPreferenceList();
34 | }
35 | });
36 | return item;
37 |
38 | }
39 |
40 | @Override
41 | protected MaterialPreferenceList getMaterialPreferenceList(final Context c) {
42 | MaterialPreferenceList list = Demo.createMaterialPreferenceList(c, R.color.mp_color_icon_dark_theme, THEME_LIGHT_DARKBAR);
43 |
44 | list.getCards().get(2).getItems().add(createDynamicItem("Tap for a random number", c));
45 |
46 | final MaterialPreferenceActionItem time = new MaterialPreferenceActionItem.Builder()
47 | .text("Unix Time In Millis")
48 | .subText("Time")
49 | .icon(new IconicsDrawable(c)
50 | .icon(CommunityMaterial.Icon.cmd_clock)
51 | .color(ContextCompat.getColor(c, R.color.mp_color_icon_dark_theme)
52 | ).sizeDp(18))
53 | .build();
54 | list.getCards().get(2).getItems().add(time);
55 |
56 | return list;
57 | }
58 |
59 | final Handler handler = new Handler();
60 | Runnable runnable = new Runnable() {
61 | @Override
62 | public void run() {
63 | Log.i("MaterialPrefFragment", "Updating with time");
64 | if (getList().getCards().size() > 0) {
65 | ((MaterialPreferenceActionItem) getList().getCards().get(2).getItems().get(7)).setSubText("" + System.currentTimeMillis());
66 | refreshMaterialPreferenceList();
67 | }
68 | handler.postDelayed(this, 1000);
69 | }
70 | };
71 |
72 |
73 | @Override
74 | public void onResume() {
75 | super.onResume();
76 | runnable.run();
77 | }
78 |
79 | @Override
80 | public void onPause() {
81 | super.onPause();
82 | handler.removeCallbacks(runnable);
83 | }
84 |
85 | @Override
86 | protected int getTheme() {
87 | return R.style.AppTheme_MaterialPreferenceActivity_Fragment;
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/app/src/main/java/com/francoisdexemple/materialpreferencedemo/ExampleMaterialPreferenceFragmentActivity.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreferencedemo;
2 |
3 | import android.os.Bundle;
4 | import android.support.v4.app.NavUtils;
5 | import android.support.v7.app.ActionBar;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.support.v7.widget.Toolbar;
8 |
9 | public class ExampleMaterialPreferenceFragmentActivity extends AppCompatActivity {
10 |
11 | @Override
12 | protected void onCreate(Bundle savedInstanceState) {
13 | super.onCreate(savedInstanceState);
14 | setContentView(R.layout.activity_example_material_preference_fragment);
15 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
16 | setSupportActionBar(toolbar);
17 | getSupportFragmentManager().beginTransaction()
18 | .replace(R.id.container, ExampleMaterialPreferenceFragment.newInstance(new ExampleMaterialPreferenceFragment()))
19 | .commit();
20 |
21 | setTitle("Preference Fragment Activity");
22 |
23 | if (NavUtils.getParentActivityName(this) != null) {
24 | ActionBar actionBar = getSupportActionBar();
25 | if (actionBar != null) {
26 | actionBar.setDisplayHomeAsUpEnabled(true);
27 | }
28 | }
29 |
30 | }
31 |
32 |
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/francoisdexemple/materialpreferencedemo/ExampleMaterialPreferenceLicenseActivity.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreferencedemo;
2 | import android.content.Context;
3 | import android.support.annotation.NonNull;
4 | import android.view.MenuItem;
5 |
6 | import com.francoisdexemple.materialpreference.model.MaterialPreferenceList;
7 |
8 |
9 | public class ExampleMaterialPreferenceLicenseActivity extends ExampleMaterialPreferenceActivity {
10 |
11 | @NonNull @Override
12 | protected MaterialPreferenceList getMaterialPreferenceList(@NonNull final Context c) {
13 | return Demo.createMaterialPreferenceLicenseList(c, colorIcon);
14 | }
15 |
16 | @Override
17 | protected CharSequence getActivityTitle() {
18 | return getString(R.string.mp_title_licenses);
19 | }
20 |
21 | @Override
22 | public boolean onOptionsItemSelected(MenuItem item) {
23 | switch (item.getItemId()) {
24 | case android.R.id.home:
25 | finish();
26 | return true;
27 | default:
28 | return false;
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/app/src/main/java/com/francoisdexemple/materialpreferencedemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreferencedemo;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.support.v7.widget.Toolbar;
7 | import android.view.Menu;
8 | import android.view.MenuItem;
9 | import android.view.View;
10 | import android.widget.Button;
11 |
12 | public class MainActivity extends AppCompatActivity {
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | setContentView(R.layout.activity_main);
18 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
19 | setSupportActionBar(toolbar);
20 |
21 | Button fragmentButton = (Button) findViewById(R.id.fragment_button);
22 | fragmentButton.setOnClickListener(new View.OnClickListener() {
23 | @Override
24 | public void onClick(View v) {
25 | Intent i = new Intent(MainActivity.this, ExampleMaterialPreferenceFragmentActivity.class);
26 | startActivity(i);
27 | }
28 | });
29 |
30 | }
31 |
32 | public void onActivityButtonPressed(View view) {
33 | Intent i = new Intent(MainActivity.this, ExampleMaterialPreferenceActivity.class);
34 | i.putExtra(ExampleMaterialPreferenceActivity.THEME_EXTRA, Integer.parseInt(view.getTag().toString()));
35 | startActivity(i);
36 | }
37 |
38 | @Override
39 | public boolean onCreateOptionsMenu(Menu menu) {
40 | // Inflate the menu; this adds items to the action bar if it is present.
41 | getMenuInflater().inflate(R.menu.menu_main, menu);
42 | return true;
43 | }
44 |
45 | @Override
46 | public boolean onOptionsItemSelected(MenuItem item) {
47 | // Handle action bar item clicks here. The action bar will
48 | // automatically handle clicks on the Home/Up button, so long
49 | // as you specify a parent activity in AndroidManifest.xml.
50 | int id = item.getItemId();
51 |
52 | //noinspection SimplifiableIfStatement
53 | if (id == R.id.action_settings) {
54 | Intent i = new Intent(this, ExampleMaterialPreferenceActivity.class);
55 | startActivity(i);
56 | return true;
57 | }
58 |
59 | return super.onOptionsItemSelected(item);
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/app/src/main/java/com/francoisdexemple/materialpreferencedemo/custom/MyCustomItem.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreferencedemo.custom;
2 |
3 | import android.content.Context;
4 | import android.graphics.drawable.Drawable;
5 | import android.os.Build;
6 | import android.support.annotation.DrawableRes;
7 | import android.support.annotation.IntDef;
8 | import android.support.annotation.StringRes;
9 | import android.text.Html;
10 | import android.util.TypedValue;
11 | import android.view.Gravity;
12 | import android.view.View;
13 | import android.widget.ImageView;
14 | import android.widget.LinearLayout;
15 | import android.widget.TextView;
16 |
17 |
18 | import com.francoisdexemple.materialpreference.holders.MaterialPreferenceItemViewHolder;
19 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceItem;
20 | import com.francoisdexemple.materialpreferencedemo.R;
21 |
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 |
25 | import static android.view.View.GONE;
26 |
27 | public class MyCustomItem extends MaterialPreferenceItem {
28 |
29 | public interface OnClickListener {
30 | void onClick();
31 | }
32 |
33 |
34 |
35 | @Retention(RetentionPolicy.SOURCE)
36 | @IntDef({GRAVITY_TOP, GRAVITY_MIDDLE, GRAVITY_BOTTOM})
37 | public @interface IconGravity {
38 | }
39 |
40 | public static final int GRAVITY_TOP = 0;
41 | public static final int GRAVITY_MIDDLE = 1;
42 | public static final int GRAVITY_BOTTOM = 2;
43 |
44 |
45 | private CharSequence text = null;
46 | private int textRes = 0;
47 |
48 | private CharSequence subText = null;
49 | private int subTextRes = 0;
50 |
51 | private Drawable icon = null;
52 | private int iconRes = 0;
53 | private boolean showIcon = true;
54 | private int iconGravity = GRAVITY_MIDDLE;
55 |
56 | private MyCustomItem.OnClickListener onClickListener = null;
57 |
58 | @Override
59 | public int getType() {
60 | return MyViewTypeManager.ItemType.CUSTOM_ITEM;
61 | }
62 |
63 | @Override
64 | public String getDetailString() {
65 | return "MyCustomItem{" +
66 | "text=" + text +
67 | ", textRes=" + textRes +
68 | ", subText=" + subText +
69 | ", subTextRes=" + subTextRes +
70 | ", icon=" + icon +
71 | ", iconRes=" + iconRes +
72 | ", showIcon=" + showIcon +
73 | ", iconGravity=" + iconGravity +
74 | ", onClickListener=" + onClickListener +
75 | '}';
76 | }
77 |
78 | public MyCustomItem(MyCustomItem item) {
79 | this.id = item.getId();
80 | this.text = item.getText();
81 | this.textRes = item.getTextRes();
82 | this.subText = item.getSubText();
83 | this.subTextRes = item.getSubTextRes();
84 | this.icon = item.getIcon();
85 | this.iconRes = item.getIconRes();
86 | this.showIcon = item.shouldShowIcon();
87 | this.iconGravity = item.getIconGravity();
88 | this.onClickListener = item.getOnClickListener();
89 | }
90 |
91 | @Override
92 | public MyCustomItem clone() {
93 | return new MyCustomItem(this);
94 | }
95 |
96 | public static MaterialPreferenceItemViewHolder getViewHolder(View view) {
97 | return new MyCustomItemViewHolder(view);
98 | }
99 |
100 | public static class MyCustomItemViewHolder extends MaterialPreferenceItemViewHolder implements View.OnClickListener {
101 | public final View view;
102 | public final ImageView icon;
103 | public final TextView text;
104 | public final TextView subText;
105 | public MyCustomItem.OnClickListener onClickListener;
106 |
107 | MyCustomItemViewHolder(View view) {
108 | super(view);
109 | this.view = view;
110 | icon = (ImageView) view.findViewById(R.id.mp_item_image);
111 | text = (TextView) view.findViewById(R.id.mp_item_text);
112 | subText = (TextView) view.findViewById(R.id.mp_action_item_subtext);
113 |
114 | view.setOnClickListener(this);
115 | onClickListener = null;
116 | }
117 |
118 | @Override
119 | public void onClick(View v) {
120 | if (onClickListener != null) {
121 | onClickListener.onClick();
122 | }
123 | }
124 | }
125 |
126 | private MyCustomItem(Builder builder) {
127 | this.text = builder.text;
128 | this.textRes = builder.textRes;
129 |
130 | this.subText = builder.subText;
131 | this.subTextRes = builder.subTextRes;
132 |
133 | this.icon = builder.icon;
134 | this.iconRes = builder.iconRes;
135 |
136 | this.showIcon = builder.showIcon;
137 |
138 | this.iconGravity = builder.iconGravity;
139 |
140 | this.onClickListener = builder.onClickListener;
141 | }
142 |
143 | public MyCustomItem(CharSequence text, CharSequence subText, Drawable icon, OnClickListener onClickListener) {
144 | this.text = text;
145 | this.subText = subText;
146 | this.icon = icon;
147 | this.onClickListener = onClickListener;
148 | }
149 |
150 | public MyCustomItem(CharSequence text, CharSequence subText, Drawable icon) {
151 | this.text = text;
152 | this.subText = subText;
153 | this.icon = icon;
154 | }
155 |
156 | public MyCustomItem(int textRes, int subTextRes, int iconRes, OnClickListener onClickListener) {
157 | this.textRes = textRes;
158 | this.subTextRes = subTextRes;
159 | this.iconRes = iconRes;
160 | this.onClickListener = onClickListener;
161 | }
162 |
163 | public MyCustomItem(int textRes, int subTextRes, int iconRes) {
164 | this.textRes = textRes;
165 | this.subTextRes = subTextRes;
166 | this.iconRes = iconRes;
167 | }
168 |
169 | public CharSequence getText() {
170 | return text;
171 | }
172 |
173 | public int getTextRes() {
174 | return textRes;
175 | }
176 |
177 | public CharSequence getSubText() {
178 | return subText;
179 | }
180 |
181 | public int getSubTextRes() {
182 | return subTextRes;
183 | }
184 |
185 | public Drawable getIcon() {
186 | return icon;
187 | }
188 |
189 | public int getIconRes() {
190 | return iconRes;
191 | }
192 |
193 | public boolean shouldShowIcon() {
194 | return showIcon;
195 | }
196 |
197 | @IconGravity
198 | public int getIconGravity() {
199 | return iconGravity;
200 | }
201 |
202 | public MyCustomItem.OnClickListener getOnClickListener() {
203 | return onClickListener;
204 | }
205 |
206 | public static void setupItem(MyCustomItemViewHolder holder, MyCustomItem item, Context context) {
207 | CharSequence text = item.getText();
208 | int textRes = item.getTextRes();
209 |
210 | holder.text.setVisibility(View.VISIBLE);
211 | if (text != null) {
212 | holder.text.setText(text);
213 | } else if (textRes != 0) {
214 | holder.text.setText(textRes);
215 | } else {
216 | holder.text.setVisibility(GONE);
217 | }
218 |
219 | CharSequence subText = item.getSubText();
220 | int subTextRes = item.getSubTextRes();
221 |
222 | holder.subText.setVisibility(View.VISIBLE);
223 | if (subText != null) {
224 | holder.subText.setText(subText);
225 | } else if (subTextRes != 0) {
226 | holder.subText.setText(subTextRes);
227 | } else {
228 | holder.subText.setVisibility(GONE);
229 | }
230 |
231 | if (item.shouldShowIcon()) {
232 | holder.icon.setVisibility(View.VISIBLE);
233 | Drawable drawable = item.getIcon();
234 | int drawableRes = item.getIconRes();
235 | if (drawable != null) {
236 | holder.icon.setImageDrawable(drawable);
237 | } else if (drawableRes != 0) {
238 | holder.icon.setImageResource(drawableRes);
239 | }
240 | } else {
241 | holder.icon.setVisibility(GONE);
242 | }
243 |
244 | LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.icon.getLayoutParams();
245 | switch (item.getIconGravity()) {
246 | case MyCustomItem.GRAVITY_TOP:
247 | params.gravity = Gravity.TOP;
248 | break;
249 | case MyCustomItem.GRAVITY_MIDDLE:
250 | params.gravity = Gravity.CENTER_VERTICAL;
251 | break;
252 | case MyCustomItem.GRAVITY_BOTTOM:
253 | params.gravity = Gravity.BOTTOM;
254 | break;
255 | }
256 | holder.icon.setLayoutParams(params);
257 |
258 | int pL = 0, pT = 0, pR = 0, pB = 0;
259 | if (Build.VERSION.SDK_INT < 21) {
260 | pL = holder.view.getPaddingLeft();
261 | pT = holder.view.getPaddingTop();
262 | pR = holder.view.getPaddingRight();
263 | pB = holder.view.getPaddingBottom();
264 | }
265 |
266 | if (item.getOnClickListener() != null) {
267 | TypedValue outValue = new TypedValue();
268 | context.getTheme().resolveAttribute(R.attr.selectableItemBackground, outValue, true);
269 | holder.view.setBackgroundResource(outValue.resourceId);
270 | holder.onClickListener = item.getOnClickListener();
271 | holder.view.setSoundEffectsEnabled(true);
272 | } else {
273 | TypedValue outValue = new TypedValue();
274 | context.getTheme().resolveAttribute(R.attr.selectableItemBackground, outValue, false);
275 | holder.view.setBackgroundResource(outValue.resourceId);
276 | holder.onClickListener = null;
277 | holder.view.setSoundEffectsEnabled(false);
278 | }
279 |
280 | if (Build.VERSION.SDK_INT < 21) {
281 | holder.view.setPadding(pL, pT, pR, pB);
282 | }
283 | }
284 |
285 | public static class Builder {
286 |
287 | private CharSequence text = null;
288 | @StringRes
289 | private int textRes = 0;
290 |
291 | private CharSequence subText = null;
292 | @StringRes
293 | private int subTextRes = 0;
294 |
295 | private Drawable icon = null;
296 | @DrawableRes
297 | private int iconRes = 0;
298 |
299 | private boolean showIcon = true;
300 |
301 | @IconGravity
302 | private int iconGravity = GRAVITY_MIDDLE;
303 |
304 | MyCustomItem.OnClickListener onClickListener;
305 |
306 | public Builder text(CharSequence text) {
307 | this.text = text;
308 | this.textRes = 0;
309 | return this;
310 | }
311 |
312 | public Builder text(@StringRes int text) {
313 | this.textRes = text;
314 | this.text = null;
315 | return this;
316 | }
317 |
318 | public Builder subText(CharSequence subText) {
319 | this.subText = subText;
320 | this.subTextRes = 0;
321 | return this;
322 | }
323 |
324 | public Builder subText(@StringRes int subTextRes) {
325 | this.subText = null;
326 | this.subTextRes = subTextRes;
327 | return this;
328 | }
329 |
330 | public Builder subTextHtml(String subTextHtml) {
331 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
332 | this.subText = Html.fromHtml(subTextHtml, Html.FROM_HTML_MODE_LEGACY);
333 | } else {
334 | //noinspection deprecation
335 | this.subText = Html.fromHtml(subTextHtml);
336 | }
337 | this.subTextRes = 0;
338 | return this;
339 | }
340 |
341 | public Builder icon(Drawable icon) {
342 | this.icon = icon;
343 | this.iconRes = 0;
344 | return this;
345 | }
346 |
347 | public Builder icon(@DrawableRes int iconRes) {
348 | this.icon = null;
349 | this.iconRes = iconRes;
350 | return this;
351 | }
352 |
353 | public Builder showIcon(boolean showIcon) {
354 | this.showIcon = showIcon;
355 | return this;
356 | }
357 |
358 | public Builder setIconGravity(@IconGravity int iconGravity) {
359 | this.iconGravity = iconGravity;
360 | return this;
361 | }
362 |
363 | public Builder setOnClickListener(MyCustomItem.OnClickListener onClickListener) {
364 | this.onClickListener = onClickListener;
365 | return this;
366 | }
367 |
368 | public MyCustomItem build() {
369 | return new MyCustomItem(this);
370 | }
371 | }
372 | }
373 |
--------------------------------------------------------------------------------
/app/src/main/java/com/francoisdexemple/materialpreferencedemo/custom/MyViewTypeManager.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreferencedemo.custom;
2 |
3 | import android.content.Context;
4 | import android.view.View;
5 |
6 |
7 | import com.francoisdexemple.materialpreference.holders.MaterialPreferenceItemViewHolder;
8 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceActionItem;
9 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceCheckBoxItem;
10 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceItem;
11 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceSwitchItem;
12 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceTitleItem;
13 | import com.francoisdexemple.materialpreference.util.ViewTypeManager;
14 | import com.francoisdexemple.materialpreferencedemo.R;
15 |
16 | import static com.francoisdexemple.materialpreferencedemo.custom.MyViewTypeManager.ItemLayout.ACTION_LAYOUT;
17 | import static com.francoisdexemple.materialpreferencedemo.custom.MyViewTypeManager.ItemLayout.CHECKBOX_LAYOUT;
18 | import static com.francoisdexemple.materialpreferencedemo.custom.MyViewTypeManager.ItemLayout.CUSTOM_LAYOUT;
19 | import static com.francoisdexemple.materialpreferencedemo.custom.MyViewTypeManager.ItemLayout.SWITCH_LAYOUT;
20 | import static com.francoisdexemple.materialpreferencedemo.custom.MyViewTypeManager.ItemLayout.TITLE_LAYOUT;
21 | import static com.francoisdexemple.materialpreferencedemo.custom.MyViewTypeManager.ItemType.ACTION_ITEM;
22 | import static com.francoisdexemple.materialpreferencedemo.custom.MyViewTypeManager.ItemType.CHECKBOX_ITEM;
23 | import static com.francoisdexemple.materialpreferencedemo.custom.MyViewTypeManager.ItemType.CUSTOM_ITEM;
24 | import static com.francoisdexemple.materialpreferencedemo.custom.MyViewTypeManager.ItemType.SWITCH_ITEM;
25 | import static com.francoisdexemple.materialpreferencedemo.custom.MyViewTypeManager.ItemType.TITLE_ITEM;
26 |
27 | public class MyViewTypeManager extends ViewTypeManager {
28 |
29 | public static final class ItemType {
30 | public static final int ACTION_ITEM = ViewTypeManager.ItemType.ACTION_ITEM;
31 | public static final int TITLE_ITEM = ViewTypeManager.ItemType.TITLE_ITEM;
32 | public static final int CUSTOM_ITEM = 10;
33 | public static final int SWITCH_ITEM = ViewTypeManager.ItemType.SWITCH_ITEM;
34 | public static final int CHECKBOX_ITEM = ViewTypeManager.ItemType.CHECKBOX_ITEM;
35 | }
36 |
37 | public static final class ItemLayout {
38 | public static final int ACTION_LAYOUT = ViewTypeManager.ItemLayout.ACTION_LAYOUT;
39 | public static final int TITLE_LAYOUT = ViewTypeManager.ItemLayout.TITLE_LAYOUT;
40 | public static final int CUSTOM_LAYOUT = R.layout.custom_item;
41 | public static final int SWITCH_LAYOUT = ViewTypeManager.ItemLayout.SWITCH_LAYOUT;
42 | public static final int CHECKBOX_LAYOUT = ViewTypeManager.ItemLayout.CHECKBOX_LAYOUT;
43 | }
44 |
45 |
46 | public int getLayout(int itemType) {
47 | switch (itemType) {
48 | case ACTION_ITEM:
49 | return ACTION_LAYOUT;
50 | case TITLE_ITEM:
51 | return TITLE_LAYOUT;
52 | case CUSTOM_ITEM:
53 | return CUSTOM_LAYOUT;
54 | case CHECKBOX_ITEM:
55 | return CHECKBOX_LAYOUT;
56 | case SWITCH_ITEM:
57 | return SWITCH_LAYOUT;
58 | default:
59 | return -1;
60 | }
61 | }
62 |
63 | public MaterialPreferenceItemViewHolder getViewHolder(int itemType, View view) {
64 | switch (itemType) {
65 | case ACTION_ITEM:
66 | return MaterialPreferenceActionItem.getViewHolder(view);
67 | case TITLE_ITEM:
68 | return MaterialPreferenceTitleItem.getViewHolder(view);
69 | case CUSTOM_ITEM:
70 | return MyCustomItem.getViewHolder(view);
71 | case SWITCH_ITEM:
72 | return MaterialPreferenceSwitchItem.getViewHolder(view);
73 | case CHECKBOX_ITEM:
74 | return MaterialPreferenceCheckBoxItem.getViewHolder(view);
75 | default:
76 | return null;
77 | }
78 | }
79 |
80 | public void setupItem(int itemType, MaterialPreferenceItemViewHolder holder, MaterialPreferenceItem item, Context context) {
81 | switch (itemType) {
82 | case ACTION_ITEM:
83 | MaterialPreferenceActionItem.setupItem((MaterialPreferenceActionItem.MaterialPreferenceActionItemViewHolder) holder, (MaterialPreferenceActionItem) item, context);
84 | break;
85 | case TITLE_ITEM:
86 | MaterialPreferenceTitleItem.setupItem((MaterialPreferenceTitleItem.MaterialPreferenceTitleItemViewHolder) holder, (MaterialPreferenceTitleItem) item, context);
87 | break;
88 | case CUSTOM_ITEM:
89 | MyCustomItem.setupItem((MyCustomItem.MyCustomItemViewHolder) holder, (MyCustomItem) item, context);
90 | break;
91 | case SWITCH_ITEM:
92 | MaterialPreferenceSwitchItem.setupItem((MaterialPreferenceSwitchItem.MaterialPreferenceSwitchItemViewHolder) holder, (MaterialPreferenceSwitchItem) item, context);
93 | break;
94 | case CHECKBOX_ITEM:
95 | MaterialPreferenceCheckBoxItem.setupItem((MaterialPreferenceCheckBoxItem.MaterialPreferenceCheckBoxItemViewHolder) holder, (MaterialPreferenceCheckBoxItem) item,context);
96 | }
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_example_material_preference_fragment.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/content_example_material_preference_fragment.xml:
--------------------------------------------------------------------------------
1 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/content_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
21 |
22 |
30 |
31 |
39 |
40 |
48 |
49 |
57 |
58 |
66 |
67 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/custom_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
21 |
22 |
29 |
30 |
37 |
38 |
39 |
40 |
53 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/mp_material_preference_action_item_overridefornoicons.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
19 |
20 |
32 |
33 |
39 |
40 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/mp_material_preference_list_card_overrideforlistlayout.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
10 |
11 |
24 |
25 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_example_material_preference.xml:
--------------------------------------------------------------------------------
1 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 | #FFFFFF
8 | #9E9E9E
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/app/src/main/res/values/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Material Preference Library Demo
3 | Preference
4 | Refresh
5 | ExampleMaterialPreferenceFragment
6 |
7 | Settings
8 | Launch Preference Activity
9 | Launch Preference Fragment
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
15 |
16 |
22 |
23 |
24 |
30 |
31 |
37 |
38 |
44 |
45 |
55 |
56 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/backup_descriptor.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/web_hi_res_512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/app/src/main/web_hi_res_512.png
--------------------------------------------------------------------------------
/app/src/test/java/com/francoisdexemple/materialpreferencedemo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreferencedemo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 |
5 | repositories {
6 | google()
7 | jcenter()
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:3.1.2'
11 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' // jitpack.io
12 |
13 |
14 | // NOTE: Do not place your application dependencies here; they belong
15 | // in the individual module build.gradle files
16 | }
17 | }
18 |
19 | allprojects {
20 | repositories {
21 | google()
22 | jcenter()
23 | }
24 | }
25 |
26 | ext {
27 | compileSdkVersion = 27
28 | buildToolsVersion = '27.0.3'
29 | targetSdkVersion = compileSdkVersion
30 | minSdkVersion = 14
31 |
32 | supportLibVersion = '27.1.1'
33 | leakCanaryVersion = '1.5.4'
34 | constraintLayoutLibVersion = '1.1.0'
35 | }
36 |
37 | task clean(type: Delete) {
38 | delete rootProject.buildDir
39 | }
40 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri May 04 14:14:08 EDT 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/materialpreference/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/materialpreference/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 |
4 | group='com.github.filol'
5 |
6 | android {
7 | compileSdkVersion 27
8 |
9 | defaultConfig {
10 | minSdkVersion 19
11 | targetSdkVersion 27
12 | versionCode 1
13 | versionName "1.0"
14 |
15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16 |
17 | }
18 |
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 |
26 | }
27 |
28 | dependencies {
29 | implementation fileTree(dir: 'libs', include: ['*.jar'])
30 |
31 | implementation 'com.android.support:appcompat-v7:27.1.1'
32 | testImplementation 'junit:junit:4.12'
33 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
34 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
35 | implementation "com.android.support:appcompat-v7:$supportLibVersion"
36 | implementation "com.android.support:cardview-v7:$supportLibVersion"
37 | implementation "com.android.support:design:$supportLibVersion"
38 | implementation "com.android.support.constraint:constraint-layout:$constraintLayoutLibVersion"
39 | }
40 |
--------------------------------------------------------------------------------
/materialpreference/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/materialpreference/src/androidTest/java/com/francoisdexemple/materialpreference/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.francoisdexemple.materialpreference.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/materialpreference/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/MaterialPreferenceActivity.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference;
2 |
3 |
4 | import android.content.Context;
5 | import android.os.AsyncTask;
6 | import android.os.Bundle;
7 | import android.support.annotation.NonNull;
8 | import android.support.annotation.Nullable;
9 | import android.support.design.widget.AppBarLayout;
10 | import android.support.v4.view.animation.FastOutSlowInInterpolator;
11 | import android.support.v7.app.ActionBar;
12 | import android.support.v7.app.AppCompatActivity;
13 | import android.support.v7.widget.LinearLayoutManager;
14 | import android.support.v7.widget.RecyclerView;
15 | import android.support.v7.widget.SimpleItemAnimator;
16 | import android.support.v7.widget.Toolbar;
17 | import android.util.TypedValue;
18 | import android.view.MenuItem;
19 |
20 |
21 | import com.francoisdexemple.materialpreference.adapters.MaterialPreferenceListAdapter;
22 | import com.francoisdexemple.materialpreference.model.MaterialPreferenceList;
23 | import com.francoisdexemple.materialpreference.util.DefaultViewTypeManager;
24 | import com.francoisdexemple.materialpreference.util.ViewTypeManager;
25 |
26 | import java.lang.ref.WeakReference;
27 |
28 | public abstract class MaterialPreferenceActivity extends AppCompatActivity {
29 |
30 | private MaterialPreferenceList list = new MaterialPreferenceList.Builder().build();
31 | private Toolbar toolbar;
32 | private RecyclerView recyclerView;
33 | private MaterialPreferenceListAdapter adapter;
34 |
35 | @NonNull
36 | protected abstract MaterialPreferenceList getMaterialPreferenceList(@NonNull Context context);
37 |
38 | @Nullable
39 | protected abstract CharSequence getActivityTitle();
40 |
41 | @Override
42 | protected void onCreate(@Nullable Bundle savedInstanceState) {
43 | super.onCreate(savedInstanceState);
44 | verifyAttributesExist();
45 |
46 | setContentView(R.layout.material_preference_activity);
47 |
48 | CharSequence title = getActivityTitle();
49 | if (title == null)
50 | setTitle(R.string.mp_title_preference);
51 | else
52 | setTitle(title);
53 |
54 |
55 | assignViews();
56 | initViews();
57 |
58 | ListTask task = new ListTask(this);
59 | task.execute();
60 | }
61 |
62 | private void verifyAttributesExist() {
63 | TypedValue typedValue = new TypedValue();
64 | boolean mpColorPrimaryExists =
65 | getTheme().resolveAttribute(R.attr.mp_color_primary, typedValue, true);
66 | boolean mpColorSecondaryExists =
67 | getTheme().resolveAttribute(R.attr.mp_color_secondary, typedValue, true);
68 | if (!mpColorPrimaryExists || !mpColorSecondaryExists) {
69 | throw new IllegalStateException(String.format("The current theme doesn't provide %s " +
70 | "and/or %s. Please use a theme provided by the library or an extension.",
71 | getResources().getResourceEntryName(R.attr.mp_color_primary),
72 | getResources().getResourceEntryName(R.attr.mp_color_secondary)));
73 | }
74 | }
75 |
76 | private void assignViews() {
77 | toolbar = (Toolbar) findViewById(R.id.mp_toolbar);
78 | recyclerView = (RecyclerView) findViewById(R.id.mp_recyclerview);
79 | recyclerView.setAlpha(0f);
80 | recyclerView.setTranslationY(20);
81 | }
82 |
83 | private void initViews() {
84 | setSupportActionBar(toolbar);
85 | ActionBar actionBar = getSupportActionBar();
86 | if (actionBar != null) {
87 | actionBar.setDisplayHomeAsUpEnabled(true);
88 | }
89 | adapter = new MaterialPreferenceListAdapter(getViewTypeManager());
90 | recyclerView.setLayoutManager(new LinearLayoutManager(this));
91 | recyclerView.setAdapter(adapter);
92 | RecyclerView.ItemAnimator animator = recyclerView.getItemAnimator();
93 | if (animator instanceof SimpleItemAnimator) {
94 | ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
95 | }
96 | }
97 |
98 | @NonNull
99 | protected ViewTypeManager getViewTypeManager() {
100 | return new DefaultViewTypeManager();
101 | }
102 |
103 | @NonNull
104 | protected MaterialPreferenceList getList() {
105 | return list;
106 | }
107 |
108 | protected boolean shouldAnimate() {
109 | return true;
110 | }
111 |
112 | protected void refreshMaterialPreferenceList() {
113 | setMaterialPreferenceList(list);
114 | }
115 |
116 | @Override
117 | public boolean onOptionsItemSelected(MenuItem item) {
118 | if (item.getItemId() == android.R.id.home) {
119 | onBackPressed();
120 | return true;
121 | }
122 | return super.onOptionsItemSelected(item);
123 | }
124 |
125 | private void onTaskFinished(@Nullable MaterialPreferenceList materialPreferenceList) {
126 | if (materialPreferenceList != null) {
127 | list = materialPreferenceList;
128 | adapter.setData(list.getCards());
129 |
130 | if (shouldAnimate()) {
131 | recyclerView.animate()
132 | .alpha(1f)
133 | .translationY(0f)
134 | .setDuration(600)
135 | .setInterpolator(new FastOutSlowInInterpolator()).start();
136 | } else {
137 | recyclerView.setAlpha(1f);
138 | recyclerView.setTranslationY(0f);
139 | }
140 | } else {
141 | finish();//?? why we remain here anyway?
142 | }
143 | }
144 |
145 | protected void setMaterialPreferenceList(MaterialPreferenceList materialPreferenceList) {
146 | list = materialPreferenceList;
147 | adapter.setData(list.getCards());
148 | }
149 |
150 | protected void setScrollToolbar(boolean scrollToolbar) {
151 | if (toolbar != null) {
152 | AppBarLayout.LayoutParams params =
153 | (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
154 | if (scrollToolbar) {
155 | params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
156 | | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
157 | } else {
158 | params.setScrollFlags(0);
159 | }
160 | }
161 | }
162 |
163 | private static class ListTask extends AsyncTask {
164 |
165 | private WeakReference context;
166 |
167 | ListTask(com.francoisdexemple.materialpreference.MaterialPreferenceActivity context) {
168 | this.context = new WeakReference<>(context);
169 | }
170 |
171 | @Override
172 | protected MaterialPreferenceList doInBackground(String... params) {
173 | return isCancelled() || context.get() == null ? null : context.get().getMaterialPreferenceList(context.get());
174 | }
175 |
176 | @Override
177 | protected void onPostExecute(MaterialPreferenceList materialPreferenceList) {
178 | super.onPostExecute(materialPreferenceList);
179 | if (context.get() != null) {
180 | if (!context.get().isFinishing()) {
181 | context.get().onTaskFinished(materialPreferenceList);
182 | }
183 | }
184 | context = null;
185 | }
186 | }
187 | }
188 |
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/MaterialPreferenceFragment.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference;
2 |
3 | import android.content.Context;
4 | import android.os.AsyncTask;
5 | import android.os.Bundle;
6 | import android.support.annotation.Nullable;
7 | import android.support.v4.app.Fragment;
8 | import android.support.v4.view.animation.FastOutSlowInInterpolator;
9 | import android.support.v7.widget.LinearLayoutManager;
10 | import android.support.v7.widget.RecyclerView;
11 | import android.support.v7.widget.SimpleItemAnimator;
12 | import android.view.LayoutInflater;
13 | import android.view.View;
14 | import android.view.ViewGroup;
15 |
16 | import com.francoisdexemple.materialpreference.adapters.MaterialPreferenceListAdapter;
17 | import com.francoisdexemple.materialpreference.model.MaterialPreferenceList;
18 | import com.francoisdexemple.materialpreference.util.DefaultViewTypeManager;
19 | import com.francoisdexemple.materialpreference.util.ViewTypeManager;
20 |
21 |
22 | public abstract class MaterialPreferenceFragment extends Fragment {
23 |
24 | private MaterialPreferenceList list = new MaterialPreferenceList.Builder().build();
25 | private RecyclerView recyclerView;
26 | private MaterialPreferenceListAdapter adapter;
27 |
28 | public static com.francoisdexemple.materialpreference.MaterialPreferenceFragment newInstance(com.francoisdexemple.materialpreference.MaterialPreferenceFragment fragment) {
29 | return fragment;
30 | }
31 |
32 | protected abstract MaterialPreferenceList getMaterialPreferenceList(Context activityContext);
33 |
34 | protected int getTheme() {
35 | return R.style.Theme_AppCompat;
36 | }
37 |
38 | protected boolean shouldAnimate() {
39 | return true;
40 | }
41 |
42 |
43 | @Nullable
44 | @Override
45 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
46 | int style = getTheme();
47 |
48 | // create ContextThemeWrapper from the original Activity Context with the custom theme
49 | final Context contextThemeWrapper = new android.view.ContextThemeWrapper(getActivity(), style);
50 | LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
51 | View rootView = localInflater.inflate(R.layout.material_preference_fragment, container, false);
52 |
53 | recyclerView = (RecyclerView) rootView.findViewById(R.id.mp_recyclerview);
54 | adapter = new MaterialPreferenceListAdapter(getViewTypeManager());
55 | recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
56 | recyclerView.setAdapter(adapter);
57 |
58 | RecyclerView.ItemAnimator animator = recyclerView.getItemAnimator();
59 | if (animator instanceof SimpleItemAnimator) {
60 | ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
61 | }
62 |
63 | recyclerView.setAlpha(0f);
64 | recyclerView.setTranslationY(20);
65 |
66 | ListTask task = new ListTask(getActivity());
67 | task.execute();
68 |
69 | return rootView;
70 | }
71 |
72 | protected ViewTypeManager getViewTypeManager() {
73 | return new DefaultViewTypeManager();
74 | }
75 |
76 | protected MaterialPreferenceList getList() {
77 | return list;
78 | }
79 |
80 | protected void setMaterialPreferenceList(MaterialPreferenceList materialPreferenceList) {
81 | list = materialPreferenceList;
82 | adapter.setData(list.getCards());
83 | }
84 |
85 | @Override
86 | public void onPause() {
87 | super.onPause();
88 | }
89 |
90 | protected void refreshMaterialPreferenceList() {
91 | setMaterialPreferenceList(list);
92 | }
93 |
94 | private class ListTask extends AsyncTask {
95 |
96 | Context fragmentContext;
97 |
98 | public ListTask(Context activityContext) {
99 | this.fragmentContext = activityContext;
100 | }
101 |
102 | @Override
103 | protected String doInBackground(String... params) {
104 | list = getMaterialPreferenceList(fragmentContext);
105 | return null;
106 | }
107 |
108 | @Override
109 | protected void onPostExecute(String s) {
110 | adapter.setData(list.getCards());
111 |
112 | if (shouldAnimate()) {
113 | recyclerView.animate()
114 | .alpha(1f)
115 | .translationY(0f)
116 | .setDuration(600)
117 | .setInterpolator(new FastOutSlowInInterpolator())
118 | .start();
119 | } else {
120 | recyclerView.setAlpha(1f);
121 | recyclerView.setTranslationY(0f);
122 | }
123 |
124 | super.onPostExecute(s);
125 | fragmentContext = null;
126 | }
127 | }
128 | }
129 |
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/adapters/MaterialPreferenceItemAdapter.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference.adapters;
2 |
3 | import android.content.Context;
4 | import android.support.v7.recyclerview.extensions.AsyncListDiffer;
5 | import android.support.v7.util.DiffUtil;
6 | import android.support.v7.widget.RecyclerView;
7 | import android.util.Log;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 | import android.view.ViewGroup;
11 |
12 | import com.francoisdexemple.materialpreference.holders.MaterialPreferenceItemViewHolder;
13 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceItem;
14 | import com.francoisdexemple.materialpreference.util.DefaultViewTypeManager;
15 | import com.francoisdexemple.materialpreference.util.ViewTypeManager;
16 |
17 | import java.util.ArrayList;
18 | import java.util.Collections;
19 | import java.util.List;
20 | import java.util.UUID;
21 |
22 |
23 | public class MaterialPreferenceItemAdapter extends RecyclerView.Adapter {
24 |
25 | private final AsyncListDiffer differ = new AsyncListDiffer(this, DIFF_CALLBACK);
26 |
27 | private ViewTypeManager viewTypeManager;
28 |
29 | private Context context;
30 |
31 | MaterialPreferenceItemAdapter() {
32 | setHasStableIds(true);
33 | this.viewTypeManager = new DefaultViewTypeManager();
34 | }
35 |
36 | MaterialPreferenceItemAdapter(ViewTypeManager customViewTypeManager) {
37 | setHasStableIds(true);
38 | this.viewTypeManager = customViewTypeManager;
39 | }
40 |
41 | @Override
42 | public MaterialPreferenceItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
43 | context = viewGroup.getContext();
44 | if (!(viewGroup instanceof RecyclerView)) {
45 | throw new RuntimeException("Not bound to RecyclerView");
46 | }
47 |
48 | int layoutId = viewTypeManager.getLayout(viewType);
49 |
50 | View view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutId, viewGroup, false);
51 | view.setFocusable(true);
52 |
53 | return viewTypeManager.getViewHolder(viewType, view);
54 | }
55 |
56 | @Override
57 | public void onBindViewHolder(MaterialPreferenceItemViewHolder holder, int position) {
58 | viewTypeManager.setupItem(getItemViewType(position), holder, differ.getCurrentList().get(position), context);
59 | }
60 |
61 |
62 | @Override
63 | public long getItemId(int position) {
64 | return UUID.fromString(differ.getCurrentList().get(position).getId()).getMostSignificantBits() & Long.MAX_VALUE;
65 | }
66 |
67 | @Override
68 | public int getItemCount() {
69 | return differ.getCurrentList().size();
70 | }
71 |
72 | @Override
73 | public int getItemViewType(int position) {
74 | return differ.getCurrentList().get(position).getType();
75 | }
76 |
77 | public void setData(ArrayList newData) {
78 | List data = new ArrayList<>();
79 | for (MaterialPreferenceItem item : newData) {
80 | data.add(item.clone());
81 | }
82 | differ.submitList(data);
83 | }
84 |
85 | public List getData() {
86 | return differ.getCurrentList();
87 | }
88 |
89 |
90 | public static final DiffUtil.ItemCallback DIFF_CALLBACK = new DiffUtil.ItemCallback() {
91 | @Override
92 | public boolean areItemsTheSame(MaterialPreferenceItem oldItem, MaterialPreferenceItem newItem) {
93 | return oldItem.getId().equals(newItem.getId());
94 | }
95 |
96 | @Override
97 | public boolean areContentsTheSame(MaterialPreferenceItem oldItem, MaterialPreferenceItem newItem) {
98 | return oldItem.getDetailString().equals(newItem.getDetailString());
99 | }
100 | };
101 |
102 | }
103 |
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/adapters/MaterialPreferenceListAdapter.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference.adapters;
2 |
3 | import android.content.Context;
4 | import android.support.v7.recyclerview.extensions.AsyncListDiffer;
5 | import android.support.v7.util.DiffUtil;
6 | import android.support.v7.widget.CardView;
7 | import android.support.v7.widget.LinearLayoutManager;
8 | import android.support.v7.widget.RecyclerView;
9 | import android.support.v7.widget.SimpleItemAnimator;
10 | import android.view.LayoutInflater;
11 | import android.view.View;
12 | import android.view.ViewGroup;
13 | import android.widget.TextView;
14 |
15 | import com.francoisdexemple.materialpreference.R;
16 | import com.francoisdexemple.materialpreference.model.MaterialPreferenceCard;
17 | import com.francoisdexemple.materialpreference.util.DefaultViewTypeManager;
18 | import com.francoisdexemple.materialpreference.util.ViewTypeManager;
19 |
20 | import java.util.ArrayList;
21 | import java.util.List;
22 | import java.util.UUID;
23 |
24 |
25 | public class MaterialPreferenceListAdapter extends RecyclerView.Adapter {
26 |
27 |
28 | private final AsyncListDiffer differ = new AsyncListDiffer(this, DIFF_CALLBACK);
29 |
30 | private Context context;
31 |
32 | private ViewTypeManager viewTypeManager;
33 |
34 | public MaterialPreferenceListAdapter() {
35 | setHasStableIds(true);
36 | this.viewTypeManager = new DefaultViewTypeManager();
37 | }
38 |
39 | public MaterialPreferenceListAdapter(ViewTypeManager customViewTypeManager) {
40 | setHasStableIds(true);
41 | this.viewTypeManager = customViewTypeManager;
42 | }
43 |
44 | @Override
45 | public MaterialPreferenceListViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
46 | context = viewGroup.getContext();
47 | if (viewGroup instanceof RecyclerView) {
48 | View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.material_preference_list_card, viewGroup, false);
49 | view.setFocusable(true);
50 | return new MaterialPreferenceListViewHolder(view);
51 | } else {
52 | throw new RuntimeException("Not bound to RecyclerView");
53 | }
54 | }
55 |
56 | @Override
57 | public void onBindViewHolder(MaterialPreferenceListViewHolder holder, int position) {
58 | MaterialPreferenceCard card = differ.getCurrentList().get(position);
59 |
60 | if (holder.cardView instanceof CardView) {
61 | CardView cardView = (CardView) holder.cardView;
62 | int cardColor = card.getCardColor();
63 | if (cardColor != 0) {
64 | cardView.setBackgroundColor(cardColor);
65 | } else {
66 | cardView.setBackgroundColor(cardView.getCardBackgroundColor().getDefaultColor());
67 | }
68 | }
69 |
70 | CharSequence title = card.getTitle();
71 | int titleRes = card.getTitleRes();
72 |
73 | holder.title.setVisibility(View.VISIBLE);
74 | if (title != null) {
75 | holder.title.setText(title);
76 | } else if (titleRes != 0) {
77 | holder.title.setText(titleRes);
78 | } else {
79 | holder.title.setVisibility(View.GONE);
80 | }
81 |
82 | int titleColor = card.getTitleColor();
83 |
84 | if (holder.title.getVisibility() == View.VISIBLE) {
85 | if (titleColor != 0) {
86 | holder.title.setTextColor(titleColor);
87 | } else {
88 | holder.title.setTextColor(holder.title.getTextColors().getDefaultColor());
89 | }
90 | }
91 |
92 | holder.adapter.setData(card.getItems());
93 | }
94 |
95 | @Override
96 | public long getItemId(int position) {
97 | return UUID.fromString(differ.getCurrentList().get(position).getId()).getMostSignificantBits() & Long.MAX_VALUE;
98 | }
99 |
100 | @Override
101 | public int getItemCount() {
102 | return differ.getCurrentList().size();
103 | }
104 |
105 | public void setData(ArrayList newData) {
106 | List data = new ArrayList<>();
107 | for (MaterialPreferenceCard card : newData) {
108 | data.add(card.clone());
109 | }
110 | differ.submitList(data);
111 | }
112 |
113 |
114 | List getData() {
115 | return differ.getCurrentList();
116 | }
117 |
118 | class MaterialPreferenceListViewHolder extends RecyclerView.ViewHolder {
119 |
120 | final View cardView;
121 | final TextView title;
122 | final RecyclerView recyclerView;
123 | MaterialPreferenceItemAdapter adapter;
124 |
125 | MaterialPreferenceListViewHolder(View view) {
126 | super(view);
127 | cardView = view.findViewById(R.id.mp_list_card);
128 | title = (TextView) view.findViewById(R.id.mp_list_card_title);
129 | recyclerView = (RecyclerView) view.findViewById(R.id.mp_card_recyclerview);
130 | adapter = new MaterialPreferenceItemAdapter(viewTypeManager);
131 | recyclerView.setLayoutManager(new LinearLayoutManager(context));
132 | recyclerView.setAdapter(adapter);
133 | recyclerView.setNestedScrollingEnabled(false);
134 | }
135 | }
136 |
137 |
138 | public static final DiffUtil.ItemCallback DIFF_CALLBACK = new DiffUtil.ItemCallback() {
139 | @Override
140 | public boolean areItemsTheSame(MaterialPreferenceCard oldItem, MaterialPreferenceCard newItem) {
141 | return oldItem.getId().equals(newItem.getId());
142 | }
143 |
144 | @Override
145 | public boolean areContentsTheSame(MaterialPreferenceCard oldItem, MaterialPreferenceCard newItem) {
146 | boolean result;
147 | result = oldItem.toString().equals(newItem.toString());
148 | if (oldItem.getItems().size() != newItem.getItems().size()) return false;
149 | for (int i = 0; i < oldItem.getItems().size(); i++) {
150 | if (!oldItem.getItems().get(i).getDetailString().equals(newItem.getItems().get(i).getDetailString())) return false;
151 | }
152 | return result;
153 | }
154 | };
155 | }
156 |
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/holders/MaterialPreferenceItemViewHolder.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference.holders;
2 |
3 | import android.support.v7.widget.RecyclerView;
4 | import android.view.View;
5 |
6 | public abstract class MaterialPreferenceItemViewHolder extends RecyclerView.ViewHolder {
7 |
8 | public MaterialPreferenceItemViewHolder(View itemView) {
9 | super(itemView);
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/items/MaterialPreferenceCheckBoxItem.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference.items;
2 |
3 | import android.content.Context;
4 | import android.graphics.drawable.Drawable;
5 | import android.os.Build;
6 | import android.support.annotation.DrawableRes;
7 | import android.support.annotation.IntDef;
8 | import android.support.annotation.StringRes;
9 | import android.text.Html;
10 | import android.util.TypedValue;
11 | import android.view.View;
12 | import android.widget.CompoundButton;
13 | import android.widget.ImageView;
14 | import android.widget.CheckBox;
15 | import android.widget.TextView;
16 |
17 | import com.francoisdexemple.materialpreference.R;
18 | import com.francoisdexemple.materialpreference.holders.MaterialPreferenceItemViewHolder;
19 | import com.francoisdexemple.materialpreference.util.ViewTypeManager;
20 |
21 | import java.lang.annotation.Retention;
22 | import java.lang.annotation.RetentionPolicy;
23 |
24 | import static android.view.View.GONE;
25 |
26 | /**
27 | * Created by François Dexemple on 04/05/2018 for com.francoisdexemple.materialpreference.items.
28 | */
29 | public class MaterialPreferenceCheckBoxItem extends MaterialPreferenceItem{
30 | public static final int GRAVITY_TOP = 0;
31 | public static final int GRAVITY_MIDDLE = 1;
32 | public static final int GRAVITY_BOTTOM = 2;
33 | private CharSequence text = null;
34 | private int textRes = 0;
35 | private CharSequence subText = null;
36 | private int subTextRes = 0;
37 | private Drawable icon = null;
38 | private int iconRes = 0;
39 | private boolean showIcon = true;
40 | private int iconGravity = GRAVITY_MIDDLE;
41 | private MaterialPreferenceOnCheckedChangedListener onCheckedChanged = null;
42 | private boolean checked =false;
43 |
44 | private MaterialPreferenceCheckBoxItem(MaterialPreferenceCheckBoxItem.Builder builder) {
45 | super();
46 | this.text = builder.text;
47 | this.textRes = builder.textRes;
48 |
49 | this.subText = builder.subText;
50 | this.subTextRes = builder.subTextRes;
51 |
52 | this.icon = builder.icon;
53 | this.iconRes = builder.iconRes;
54 |
55 | this.showIcon = builder.showIcon;
56 |
57 | this.iconGravity = builder.iconGravity;
58 |
59 | this.onCheckedChanged = builder.onCheckedChanged;
60 | this.checked = builder.checked;
61 | }
62 |
63 | public MaterialPreferenceCheckBoxItem(CharSequence text, CharSequence subText, Drawable icon, boolean checked, MaterialPreferenceOnCheckedChangedListener onCheckedChanged) {
64 | this.text = text;
65 | this.subText = subText;
66 | this.icon = icon;
67 | this.onCheckedChanged = onCheckedChanged;
68 | this.checked=checked;
69 | }
70 |
71 | public MaterialPreferenceCheckBoxItem(int textRes, int subTextRes, int iconRes, boolean checked, MaterialPreferenceOnCheckedChangedListener onCheckedChanged) {
72 | this.textRes = textRes;
73 | this.subTextRes = subTextRes;
74 | this.iconRes = iconRes;
75 | this.onCheckedChanged = onCheckedChanged;
76 | this.checked=checked;
77 | }
78 |
79 |
80 | public static MaterialPreferenceItemViewHolder getViewHolder(View view) {
81 | return new MaterialPreferenceCheckBoxItemViewHolder(view);
82 | }
83 |
84 | public static void setupItem(MaterialPreferenceCheckBoxItemViewHolder holder, MaterialPreferenceCheckBoxItem item, Context context) {
85 | CharSequence text = item.getText();
86 | int textRes = item.getTextRes();
87 |
88 | holder.text.setVisibility(View.VISIBLE);
89 | if (text != null) {
90 | holder.text.setText(text);
91 | } else if (textRes != 0) {
92 | holder.text.setText(textRes);
93 | } else {
94 | holder.text.setVisibility(GONE);
95 | }
96 |
97 | CharSequence subText = item.getSubText();
98 | int subTextRes = item.getSubTextRes();
99 |
100 | holder.subText.setVisibility(View.VISIBLE);
101 | if (subText != null) {
102 | holder.subText.setText(subText);
103 | } else if (subTextRes != 0) {
104 | holder.subText.setText(subTextRes);
105 | } else {
106 | holder.subText.setVisibility(GONE);
107 | }
108 |
109 | if (item.shouldShowIcon()) {
110 | holder.icon.setVisibility(View.VISIBLE);
111 | Drawable drawable = item.getIcon();
112 | int drawableRes = item.getIconRes();
113 | if (drawable != null) {
114 | holder.icon.setImageDrawable(drawable);
115 | } else if (drawableRes != 0) {
116 | holder.icon.setImageResource(drawableRes);
117 | }
118 | } else {
119 | holder.icon.setVisibility(GONE);
120 | }
121 |
122 | int pL = 0, pT = 0, pR = 0, pB = 0;
123 | if (Build.VERSION.SDK_INT < 21) {
124 | pL = holder.view.getPaddingLeft();
125 | pT = holder.view.getPaddingTop();
126 | pR = holder.view.getPaddingRight();
127 | pB = holder.view.getPaddingBottom();
128 | }
129 |
130 | if (item.getOnCheckedChanged() != null) {
131 | TypedValue outValue = new TypedValue();
132 | context.getTheme().resolveAttribute(R.attr.selectableItemBackground, outValue, true);
133 | holder.view.setBackgroundResource(outValue.resourceId);
134 | } else {
135 | holder.view.setBackgroundResource(0);
136 | }
137 | holder.setOnCheckedChanged(item.getOnCheckedChanged());
138 |
139 | if (Build.VERSION.SDK_INT < 21) {
140 | holder.view.setPadding(pL, pT, pR, pB);
141 | }
142 |
143 | holder.aCheckBox.setChecked(item.isChecked());
144 | }
145 |
146 | @Override
147 | public int getType() {
148 | return ViewTypeManager.ItemType.CHECKBOX_ITEM;
149 | }
150 |
151 | @Override
152 | public String getDetailString() {
153 | return "MaterialPreferenceCheckBoxItem{" +
154 | "text=" + text +
155 | ", textRes=" + textRes +
156 | ", subText=" + subText +
157 | ", subTextRes=" + subTextRes +
158 | ", icon=" + icon +
159 | ", iconRes=" + iconRes +
160 | ", showIcon=" + showIcon +
161 | ", iconGravity=" + iconGravity +
162 | ", onCheckedChanged=" + onCheckedChanged +
163 | '}';
164 | }
165 |
166 | public MaterialPreferenceCheckBoxItem(MaterialPreferenceCheckBoxItem item) {
167 | this.id = item.getId();
168 | this.text = item.getText();
169 | this.textRes = item.getTextRes();
170 | this.subText = item.getSubText();
171 | this.subTextRes = item.getSubTextRes();
172 | this.icon = item.getIcon();
173 | this.iconRes = item.getIconRes();
174 | this.showIcon = item.showIcon;
175 | this.iconGravity = item.iconGravity;
176 | this.onCheckedChanged = item.onCheckedChanged;
177 | this.checked = item.checked;
178 | }
179 |
180 | @Override
181 | public MaterialPreferenceItem clone() {
182 | return new MaterialPreferenceCheckBoxItem(this);
183 | }
184 |
185 | public CharSequence getText() {
186 | return text;
187 | }
188 |
189 | public MaterialPreferenceCheckBoxItem setText(CharSequence text) {
190 | this.textRes = 0;
191 | this.text = text;
192 | return this;
193 | }
194 |
195 | public int getTextRes() {
196 | return textRes;
197 | }
198 |
199 | public MaterialPreferenceCheckBoxItem setTextRes(int textRes) {
200 | this.text = null;
201 | this.textRes = textRes;
202 | return this;
203 | }
204 |
205 | public CharSequence getSubText() {
206 | return subText;
207 | }
208 |
209 | public MaterialPreferenceCheckBoxItem setSubText(CharSequence subText) {
210 | this.subTextRes = 0;
211 | this.subText = subText;
212 | return this;
213 | }
214 |
215 | public int getSubTextRes() {
216 | return subTextRes;
217 | }
218 |
219 | public MaterialPreferenceCheckBoxItem setSubTextRes(int subTextRes) {
220 | this.subText = null;
221 | this.subTextRes = subTextRes;
222 | return this;
223 | }
224 |
225 | public Drawable getIcon() {
226 | return icon;
227 | }
228 |
229 | public MaterialPreferenceCheckBoxItem setIcon(Drawable icon) {
230 | this.iconRes = 0;
231 | this.icon = icon;
232 | return this;
233 | }
234 |
235 | public int getIconRes() {
236 | return iconRes;
237 | }
238 |
239 | public MaterialPreferenceCheckBoxItem setIconRes(int iconRes) {
240 | this.icon = null;
241 | this.iconRes = iconRes;
242 | return this;
243 | }
244 |
245 | public boolean shouldShowIcon() {
246 | return showIcon;
247 | }
248 |
249 | public MaterialPreferenceCheckBoxItem setShouldShowIcon(boolean showIcon) {
250 | this.showIcon = showIcon;
251 | return this;
252 | }
253 |
254 | @MaterialPreferenceCheckBoxItem.IconGravity
255 | public int getIconGravity() {
256 | return iconGravity;
257 | }
258 |
259 | public MaterialPreferenceCheckBoxItem setIconGravity(int iconGravity) {
260 | this.iconGravity = iconGravity;
261 | return this;
262 | }
263 |
264 | public MaterialPreferenceOnCheckedChangedListener getOnCheckedChanged() {
265 | return onCheckedChanged;
266 | }
267 |
268 | public void setChecked(boolean checked) {
269 | this.checked = checked;
270 | }
271 |
272 | public boolean isChecked() {
273 | return checked;
274 | }
275 |
276 | public MaterialPreferenceCheckBoxItem setOnCheckedChanged(MaterialPreferenceOnCheckedChangedListener onCheckedChanged) {
277 | this.onCheckedChanged = onCheckedChanged;
278 | return this;
279 | }
280 |
281 | @Retention(RetentionPolicy.SOURCE)
282 | @IntDef({GRAVITY_TOP, GRAVITY_MIDDLE, GRAVITY_BOTTOM})
283 | public @interface IconGravity {
284 | }
285 |
286 | public static class MaterialPreferenceCheckBoxItemViewHolder extends MaterialPreferenceItemViewHolder implements CompoundButton.OnCheckedChangeListener {
287 | public final View view;
288 | public final ImageView icon;
289 | public final TextView text;
290 | public final TextView subText;
291 | public final CheckBox aCheckBox;
292 | private MaterialPreferenceOnCheckedChangedListener onCheckedChanged;
293 |
294 | MaterialPreferenceCheckBoxItemViewHolder(View view) {
295 | super(view);
296 | this.view = view;
297 | icon = (ImageView) view.findViewById(R.id.mp_checkbox_image);
298 | text = (TextView) view.findViewById(R.id.mp_checkbox_text);
299 | subText = (TextView) view.findViewById(R.id.mp_checkbox_subtext);
300 | aCheckBox = (CheckBox) view.findViewById(R.id.mp_checkbox);
301 | }
302 |
303 | public void setOnCheckedChanged(MaterialPreferenceOnCheckedChangedListener onCheckedChanged) {
304 | this.onCheckedChanged = onCheckedChanged;
305 | this.aCheckBox.setOnCheckedChangeListener(onCheckedChanged != null ? this : null);
306 | }
307 |
308 | @Override
309 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
310 | if (onCheckedChanged != null) {
311 | onCheckedChanged.onCheckedChanged(buttonView, isChecked);
312 | }
313 | }
314 | }
315 |
316 | public static class Builder {
317 |
318 | MaterialPreferenceOnCheckedChangedListener onCheckedChanged = null;
319 | private CharSequence text = null;
320 | @StringRes
321 | private int textRes = 0;
322 | private CharSequence subText = null;
323 | @StringRes
324 | private int subTextRes = 0;
325 | private Drawable icon = null;
326 | @DrawableRes
327 | private int iconRes = 0;
328 | private boolean showIcon = true;
329 | @IconGravity
330 | private int iconGravity = GRAVITY_MIDDLE;
331 | boolean checked = false;
332 |
333 | public Builder text(CharSequence text) {
334 | this.text = text;
335 | this.textRes = 0;
336 | return this;
337 | }
338 |
339 | public Builder text(@StringRes int text) {
340 | this.textRes = text;
341 | this.text = null;
342 | return this;
343 | }
344 |
345 | public Builder subText(CharSequence subText) {
346 | this.subText = subText;
347 | this.subTextRes = 0;
348 | return this;
349 | }
350 |
351 | public Builder subText(@StringRes int subTextRes) {
352 | this.subText = null;
353 | this.subTextRes = subTextRes;
354 | return this;
355 | }
356 |
357 | public Builder subTextHtml(String subTextHtml) {
358 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
359 | this.subText = Html.fromHtml(subTextHtml, Html.FROM_HTML_MODE_LEGACY);
360 | } else {
361 | //noinspection deprecation
362 | this.subText = Html.fromHtml(subTextHtml);
363 | }
364 | this.subTextRes = 0;
365 | return this;
366 | }
367 |
368 | public Builder icon(Drawable icon) {
369 | this.icon = icon;
370 | this.iconRes = 0;
371 | return this;
372 | }
373 |
374 | public Builder icon(@DrawableRes int iconRes) {
375 | this.icon = null;
376 | this.iconRes = iconRes;
377 | return this;
378 | }
379 |
380 | public Builder showIcon(boolean showIcon) {
381 | this.showIcon = showIcon;
382 | return this;
383 | }
384 |
385 | public Builder setIconGravity(@MaterialPreferenceCheckBoxItem.IconGravity int iconGravity) {
386 | this.iconGravity = iconGravity;
387 | return this;
388 | }
389 |
390 | public Builder setOnCheckedChanged(MaterialPreferenceOnCheckedChangedListener onCheckedChanged) {
391 | this.onCheckedChanged = onCheckedChanged;
392 | return this;
393 | }
394 |
395 | public Builder setChecked(boolean checked){
396 | this.checked=checked;
397 | return this;
398 | }
399 |
400 | public MaterialPreferenceCheckBoxItem build() {
401 | return new MaterialPreferenceCheckBoxItem(this);
402 | }
403 | }
404 | }
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/items/MaterialPreferenceItem.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference.items;
2 |
3 | import java.util.UUID;
4 |
5 | public abstract class MaterialPreferenceItem {
6 |
7 | public String id = "NO-UUID";
8 |
9 | public MaterialPreferenceItem() {
10 | this.id = UUID.randomUUID().toString();
11 | }
12 |
13 | public String getId() {
14 | return id;
15 | }
16 |
17 | public abstract int getType();
18 |
19 | public abstract String getDetailString();
20 |
21 | public abstract MaterialPreferenceItem clone();
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/items/MaterialPreferenceItemOnClickAction.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference.items;
2 |
3 | public interface MaterialPreferenceItemOnClickAction {
4 | void onClick();
5 | }
6 |
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/items/MaterialPreferenceOnCheckedChangedListener.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference.items;
2 |
3 | import android.widget.CompoundButton;
4 |
5 | /**
6 | * Created by François Dexemple on 04/05/2018 for com.francoisdexemple.materialpreference.items.
7 | */
8 | public interface MaterialPreferenceOnCheckedChangedListener {
9 | void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
10 | }
11 |
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/items/MaterialPreferenceSwitchItem.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference.items;
2 |
3 | import android.content.Context;
4 | import android.graphics.drawable.Drawable;
5 | import android.os.Build;
6 | import android.support.annotation.DrawableRes;
7 | import android.support.annotation.IntDef;
8 | import android.support.annotation.StringRes;
9 | import android.text.Html;
10 | import android.util.TypedValue;
11 | import android.view.View;
12 | import android.widget.CompoundButton;
13 | import android.widget.ImageView;
14 | import android.widget.Switch;
15 | import android.widget.TextView;
16 |
17 | import com.francoisdexemple.materialpreference.R;
18 | import com.francoisdexemple.materialpreference.holders.MaterialPreferenceItemViewHolder;
19 | import com.francoisdexemple.materialpreference.util.ViewTypeManager;
20 |
21 | import java.lang.annotation.Retention;
22 | import java.lang.annotation.RetentionPolicy;
23 |
24 | import static android.view.View.GONE;
25 |
26 | /**
27 | * Created by François Dexemple on 04/05/2018 for com.francoisdexemple.materialpreference.items.
28 | */
29 | public class MaterialPreferenceSwitchItem extends MaterialPreferenceItem {
30 | public static final int GRAVITY_TOP = 0;
31 | public static final int GRAVITY_MIDDLE = 1;
32 | public static final int GRAVITY_BOTTOM = 2;
33 | private CharSequence text = null;
34 | private int textRes = 0;
35 | private CharSequence subText = null;
36 | private int subTextRes = 0;
37 | private Drawable icon = null;
38 | private int iconRes = 0;
39 | private boolean showIcon = true;
40 | private int iconGravity = GRAVITY_MIDDLE;
41 | private MaterialPreferenceOnCheckedChangedListener onCheckedChanged = null;
42 | private boolean checked = false;
43 |
44 | private MaterialPreferenceSwitchItem(MaterialPreferenceSwitchItem.Builder builder) {
45 | super();
46 | this.text = builder.text;
47 | this.textRes = builder.textRes;
48 |
49 | this.subText = builder.subText;
50 | this.subTextRes = builder.subTextRes;
51 |
52 | this.icon = builder.icon;
53 | this.iconRes = builder.iconRes;
54 |
55 | this.showIcon = builder.showIcon;
56 |
57 | this.iconGravity = builder.iconGravity;
58 |
59 | this.onCheckedChanged = builder.onCheckedChanged;
60 | this.checked=builder.checked;
61 | }
62 |
63 | public MaterialPreferenceSwitchItem(CharSequence text, CharSequence subText, Drawable icon, boolean checked, MaterialPreferenceOnCheckedChangedListener onCheckedChanged) {
64 | this.text = text;
65 | this.subText = subText;
66 | this.icon = icon;
67 | this.onCheckedChanged = onCheckedChanged;
68 | this.checked=checked;
69 | }
70 |
71 | public MaterialPreferenceSwitchItem(int textRes, int subTextRes, int iconRes, boolean checked, MaterialPreferenceOnCheckedChangedListener onCheckedChanged) {
72 | this.textRes = textRes;
73 | this.subTextRes = subTextRes;
74 | this.iconRes = iconRes;
75 | this.onCheckedChanged = onCheckedChanged;
76 | this.checked=checked;
77 | }
78 |
79 | public static MaterialPreferenceItemViewHolder getViewHolder(View view) {
80 | return new MaterialPreferenceSwitchItemViewHolder(view);
81 | }
82 |
83 | public static void setupItem(MaterialPreferenceSwitchItemViewHolder holder, MaterialPreferenceSwitchItem item, Context context) {
84 | CharSequence text = item.getText();
85 | int textRes = item.getTextRes();
86 |
87 | holder.text.setVisibility(View.VISIBLE);
88 | if (text != null) {
89 | holder.text.setText(text);
90 | } else if (textRes != 0) {
91 | holder.text.setText(textRes);
92 | } else {
93 | holder.text.setVisibility(GONE);
94 | }
95 |
96 | CharSequence subText = item.getSubText();
97 | int subTextRes = item.getSubTextRes();
98 |
99 | holder.subText.setVisibility(View.VISIBLE);
100 | if (subText != null) {
101 | holder.subText.setText(subText);
102 | } else if (subTextRes != 0) {
103 | holder.subText.setText(subTextRes);
104 | } else {
105 | holder.subText.setVisibility(GONE);
106 | }
107 |
108 | if (item.shouldShowIcon()) {
109 | holder.icon.setVisibility(View.VISIBLE);
110 | Drawable drawable = item.getIcon();
111 | int drawableRes = item.getIconRes();
112 | if (drawable != null) {
113 | holder.icon.setImageDrawable(drawable);
114 | } else if (drawableRes != 0) {
115 | holder.icon.setImageResource(drawableRes);
116 | }
117 | } else {
118 | holder.icon.setVisibility(GONE);
119 | }
120 |
121 | int pL = 0, pT = 0, pR = 0, pB = 0;
122 | if (Build.VERSION.SDK_INT < 21) {
123 | pL = holder.view.getPaddingLeft();
124 | pT = holder.view.getPaddingTop();
125 | pR = holder.view.getPaddingRight();
126 | pB = holder.view.getPaddingBottom();
127 | }
128 |
129 | if (item.getOnCheckedChanged() != null) {
130 | TypedValue outValue = new TypedValue();
131 | context.getTheme().resolveAttribute(R.attr.selectableItemBackground, outValue, true);
132 | holder.view.setBackgroundResource(outValue.resourceId);
133 | } else {
134 | holder.view.setBackgroundResource(0);
135 | }
136 | holder.setOnCheckedChanged(item.getOnCheckedChanged());
137 |
138 | if (Build.VERSION.SDK_INT < 21) {
139 | holder.view.setPadding(pL, pT, pR, pB);
140 | }
141 |
142 | holder.aswitch.setChecked(item.isChecked());
143 | }
144 |
145 | @Override
146 | public int getType() {
147 | return ViewTypeManager.ItemType.SWITCH_ITEM;
148 | }
149 |
150 | @Override
151 | public String getDetailString() {
152 | return "MaterialPreferenceSwitchItem{" +
153 | "text=" + text +
154 | ", textRes=" + textRes +
155 | ", subText=" + subText +
156 | ", subTextRes=" + subTextRes +
157 | ", icon=" + icon +
158 | ", iconRes=" + iconRes +
159 | ", showIcon=" + showIcon +
160 | ", iconGravity=" + iconGravity +
161 | ", onCheckedChanged=" + onCheckedChanged +
162 | '}';
163 | }
164 |
165 | public MaterialPreferenceSwitchItem(MaterialPreferenceSwitchItem item) {
166 | this.id = item.getId();
167 | this.text = item.getText();
168 | this.textRes = item.getTextRes();
169 | this.subText = item.getSubText();
170 | this.subTextRes = item.getSubTextRes();
171 | this.icon = item.getIcon();
172 | this.iconRes = item.getIconRes();
173 | this.showIcon = item.showIcon;
174 | this.iconGravity = item.iconGravity;
175 | this.onCheckedChanged = item.onCheckedChanged;
176 | this.checked = item.checked;
177 | }
178 |
179 | @Override
180 | public MaterialPreferenceItem clone() {
181 | return new MaterialPreferenceSwitchItem(this);
182 | }
183 |
184 | public CharSequence getText() {
185 | return text;
186 | }
187 | public MaterialPreferenceSwitchItem setText(CharSequence text) {
188 | this.textRes = 0;
189 | this.text = text;
190 | return this;
191 | }
192 |
193 | public int getTextRes() {
194 | return textRes;
195 | }
196 |
197 | public MaterialPreferenceSwitchItem setTextRes(int textRes) {
198 | this.text = null;
199 | this.textRes = textRes;
200 | return this;
201 | }
202 |
203 | public CharSequence getSubText() {
204 | return subText;
205 | }
206 |
207 | public MaterialPreferenceSwitchItem setSubText(CharSequence subText) {
208 | this.subTextRes = 0;
209 | this.subText = subText;
210 | return this;
211 | }
212 |
213 | public int getSubTextRes() {
214 | return subTextRes;
215 | }
216 |
217 | public MaterialPreferenceSwitchItem setSubTextRes(int subTextRes) {
218 | this.subText = null;
219 | this.subTextRes = subTextRes;
220 | return this;
221 | }
222 |
223 | public Drawable getIcon() {
224 | return icon;
225 | }
226 |
227 | public MaterialPreferenceSwitchItem setIcon(Drawable icon) {
228 | this.iconRes = 0;
229 | this.icon = icon;
230 | return this;
231 | }
232 |
233 | public int getIconRes() {
234 | return iconRes;
235 | }
236 |
237 | public MaterialPreferenceSwitchItem setIconRes(int iconRes) {
238 | this.icon = null;
239 | this.iconRes = iconRes;
240 | return this;
241 | }
242 |
243 | public boolean shouldShowIcon() {
244 | return showIcon;
245 | }
246 |
247 | public MaterialPreferenceSwitchItem setShouldShowIcon(boolean showIcon) {
248 | this.showIcon = showIcon;
249 | return this;
250 | }
251 |
252 | @MaterialPreferenceSwitchItem.IconGravity
253 | public int getIconGravity() {
254 | return iconGravity;
255 | }
256 |
257 | public MaterialPreferenceSwitchItem setIconGravity(int iconGravity) {
258 | this.iconGravity = iconGravity;
259 | return this;
260 | }
261 |
262 | public void setChecked(boolean checked) {
263 | this.checked = checked;
264 | }
265 |
266 | public boolean isChecked() {
267 | return checked;
268 | }
269 |
270 | public MaterialPreferenceOnCheckedChangedListener getOnCheckedChanged() {
271 | return onCheckedChanged;
272 | }
273 |
274 | public MaterialPreferenceSwitchItem setOnCheckedChanged(MaterialPreferenceOnCheckedChangedListener onCheckedChanged) {
275 | this.onCheckedChanged = onCheckedChanged;
276 | return this;
277 | }
278 |
279 | @Retention(RetentionPolicy.SOURCE)
280 | @IntDef({GRAVITY_TOP, GRAVITY_MIDDLE, GRAVITY_BOTTOM})
281 | public @interface IconGravity {
282 | }
283 |
284 | public static class MaterialPreferenceSwitchItemViewHolder extends MaterialPreferenceItemViewHolder implements CompoundButton.OnCheckedChangeListener{
285 | public final View view;
286 | public final ImageView icon;
287 | public final TextView text;
288 | public final TextView subText;
289 | public final Switch aswitch;
290 | private MaterialPreferenceOnCheckedChangedListener onCheckedChanged;
291 |
292 | MaterialPreferenceSwitchItemViewHolder(View view) {
293 | super(view);
294 | this.view = view;
295 | icon = (ImageView) view.findViewById(R.id.mp_switch_image);
296 | text = (TextView) view.findViewById(R.id.mp_switch_text);
297 | subText = (TextView) view.findViewById(R.id.mp_switch_subtext);
298 | aswitch = (Switch) view.findViewById(R.id.mp_switch);
299 | }
300 |
301 | public void setOnCheckedChanged(MaterialPreferenceOnCheckedChangedListener onCheckedChanged) {
302 | this.onCheckedChanged = onCheckedChanged;
303 | this.aswitch.setOnCheckedChangeListener(onCheckedChanged != null ? this : null);
304 | }
305 |
306 | @Override
307 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
308 | if (onCheckedChanged != null) {
309 | onCheckedChanged.onCheckedChanged(buttonView,isChecked);
310 | }
311 | }
312 | }
313 |
314 | public static class Builder {
315 |
316 | MaterialPreferenceOnCheckedChangedListener onCheckedChanged = null;
317 | private CharSequence text = null;
318 | @StringRes
319 | private int textRes = 0;
320 | private CharSequence subText = null;
321 | @StringRes
322 | private int subTextRes = 0;
323 | private Drawable icon = null;
324 | @DrawableRes
325 | private int iconRes = 0;
326 | private boolean showIcon = true;
327 | @IconGravity
328 | private int iconGravity = GRAVITY_MIDDLE;
329 | private boolean checked = false;
330 |
331 | public Builder text(CharSequence text) {
332 | this.text = text;
333 | this.textRes = 0;
334 | return this;
335 | }
336 |
337 | public Builder text(@StringRes int text) {
338 | this.textRes = text;
339 | this.text = null;
340 | return this;
341 | }
342 |
343 | public Builder subText(CharSequence subText) {
344 | this.subText = subText;
345 | this.subTextRes = 0;
346 | return this;
347 | }
348 |
349 | public Builder subText(@StringRes int subTextRes) {
350 | this.subText = null;
351 | this.subTextRes = subTextRes;
352 | return this;
353 | }
354 |
355 | public Builder subTextHtml(String subTextHtml) {
356 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
357 | this.subText = Html.fromHtml(subTextHtml, Html.FROM_HTML_MODE_LEGACY);
358 | } else {
359 | //noinspection deprecation
360 | this.subText = Html.fromHtml(subTextHtml);
361 | }
362 | this.subTextRes = 0;
363 | return this;
364 | }
365 |
366 | public Builder icon(Drawable icon) {
367 | this.icon = icon;
368 | this.iconRes = 0;
369 | return this;
370 | }
371 |
372 | public Builder icon(@DrawableRes int iconRes) {
373 | this.icon = null;
374 | this.iconRes = iconRes;
375 | return this;
376 | }
377 |
378 | public Builder showIcon(boolean showIcon) {
379 | this.showIcon = showIcon;
380 | return this;
381 | }
382 |
383 | public Builder setIconGravity(@MaterialPreferenceSwitchItem.IconGravity int iconGravity) {
384 | this.iconGravity = iconGravity;
385 | return this;
386 | }
387 |
388 | public Builder setOnCheckedChanged(MaterialPreferenceOnCheckedChangedListener onCheckedChanged) {
389 | this.onCheckedChanged = onCheckedChanged;
390 | return this;
391 | }
392 |
393 | public Builder setChecked(boolean checked) {
394 | this.checked = checked;
395 | return this;
396 | }
397 |
398 | public MaterialPreferenceSwitchItem build() {
399 | return new MaterialPreferenceSwitchItem(this);
400 | }
401 | }
402 | }
403 |
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/items/MaterialPreferenceTitleItem.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference.items;
2 |
3 |
4 | import android.content.Context;
5 | import android.graphics.drawable.Drawable;
6 | import android.os.Build;
7 | import android.support.annotation.DrawableRes;
8 | import android.support.annotation.StringRes;
9 | import android.util.TypedValue;
10 | import android.view.View;
11 | import android.widget.ImageView;
12 | import android.widget.TextView;
13 |
14 |
15 | import com.francoisdexemple.materialpreference.R;
16 | import com.francoisdexemple.materialpreference.holders.MaterialPreferenceItemViewHolder;
17 | import com.francoisdexemple.materialpreference.util.ViewTypeManager;
18 |
19 | import static android.view.View.GONE;
20 |
21 | public class MaterialPreferenceTitleItem extends MaterialPreferenceItem {
22 |
23 | private CharSequence text = null;
24 | private int textRes = 0;
25 | private CharSequence desc = null;
26 | private int descRes = 0;
27 | private Drawable icon = null;
28 | private int iconRes = 0;
29 | private MaterialPreferenceItemOnClickAction onClickAction = null;
30 | private MaterialPreferenceItemOnClickAction onLongClickAction = null;
31 |
32 | private MaterialPreferenceTitleItem(MaterialPreferenceTitleItem.Builder builder) {
33 | super();
34 | this.text = builder.text;
35 | this.textRes = builder.textRes;
36 |
37 | this.desc = builder.desc;
38 | this.descRes = builder.descRes;
39 |
40 | this.icon = builder.icon;
41 | this.iconRes = builder.iconRes;
42 |
43 | this.onClickAction = builder.onClickAction;
44 | this.onLongClickAction = builder.onLongClickAction;
45 | }
46 |
47 | public MaterialPreferenceTitleItem(CharSequence text, CharSequence desc, Drawable icon) {
48 | this.text = text;
49 | this.desc = desc;
50 | this.icon = icon;
51 | }
52 |
53 | public MaterialPreferenceTitleItem(int textRes, int descRes, int iconRes) {
54 | this.textRes = textRes;
55 | this.descRes = descRes;
56 | this.iconRes = iconRes;
57 | }
58 |
59 | public static MaterialPreferenceItemViewHolder getViewHolder(View view) {
60 | return new MaterialPreferenceTitleItem.MaterialPreferenceTitleItemViewHolder(view);
61 | }
62 |
63 | public static void setupItem(MaterialPreferenceTitleItemViewHolder holder, MaterialPreferenceTitleItem item, Context context) {
64 |
65 | CharSequence text = item.getText();
66 | int textRes = item.getTextRes();
67 |
68 | holder.text.setVisibility(View.VISIBLE);
69 | if (text != null) {
70 | holder.text.setText(text);
71 | } else if (textRes != 0) {
72 | holder.text.setText(textRes);
73 | } else {
74 | holder.text.setVisibility(GONE);
75 | }
76 |
77 | CharSequence desc = item.getDesc();
78 | int descRes = item.getDescRes();
79 |
80 | holder.desc.setVisibility(View.VISIBLE);
81 | if (desc != null) {
82 | holder.desc.setText(desc);
83 | } else if (descRes != 0) {
84 | holder.desc.setText(descRes);
85 | } else {
86 | holder.desc.setVisibility(GONE);
87 | }
88 |
89 | Drawable drawable = item.getIcon();
90 | int drawableRes = item.getIconRes();
91 | if (drawable != null) {
92 | holder.icon.setImageDrawable(drawable);
93 | } else if (drawableRes != 0) {
94 | holder.icon.setImageResource(drawableRes);
95 | }
96 |
97 | int pL = 0, pT = 0, pR = 0, pB = 0;
98 | if (Build.VERSION.SDK_INT < 21) {
99 | pL = holder.view.getPaddingLeft();
100 | pT = holder.view.getPaddingTop();
101 | pR = holder.view.getPaddingRight();
102 | pB = holder.view.getPaddingBottom();
103 | }
104 |
105 | if (item.getOnClickAction() != null || item.getOnLongClickAction() != null) {
106 | TypedValue outValue = new TypedValue();
107 | context.getTheme().resolveAttribute(R.attr.selectableItemBackground, outValue, true);
108 | holder.view.setBackgroundResource(outValue.resourceId);
109 | } else {
110 | holder.view.setBackgroundResource(0);
111 | }
112 | holder.setOnClickAction(item.getOnClickAction());
113 | holder.setOnLongClickAction(item.getOnLongClickAction());
114 |
115 | if (Build.VERSION.SDK_INT < 21) {
116 | holder.view.setPadding(pL, pT, pR, pB);
117 | }
118 | }
119 |
120 | @Override
121 | public int getType() {
122 | return ViewTypeManager.ItemType.TITLE_ITEM;
123 | }
124 |
125 | @Override
126 | public String getDetailString() {
127 | return "MaterialPreferenceTitleItem{" +
128 | "text=" + text +
129 | ", textRes=" + textRes +
130 | ", desc=" + desc +
131 | ", descRes=" + descRes +
132 | ", icon=" + icon +
133 | ", iconRes=" + iconRes +
134 | ", onClickAction=" + onClickAction +
135 | ", onLongClickAction=" + onLongClickAction +
136 | '}';
137 | }
138 |
139 | public MaterialPreferenceTitleItem(MaterialPreferenceTitleItem item) {
140 | this.id = item.getId();
141 | this.text = item.getText();
142 | this.textRes = item.getTextRes();
143 | this.desc = item.getDesc();
144 | this.descRes = item.getDescRes();
145 | this.icon = item.getIcon();
146 | this.iconRes = item.getIconRes();
147 | this.onClickAction = item.getOnClickAction();
148 | this.onLongClickAction = item.getOnLongClickAction();
149 | }
150 |
151 | @Override
152 | public MaterialPreferenceTitleItem clone() {
153 | return new MaterialPreferenceTitleItem(this);
154 | }
155 |
156 | public CharSequence getText() {
157 | return text;
158 | }
159 |
160 | public MaterialPreferenceTitleItem setText(CharSequence text) {
161 | this.textRes = 0;
162 | this.text = text;
163 | return this;
164 | }
165 |
166 | public int getTextRes() {
167 | return textRes;
168 | }
169 |
170 | public MaterialPreferenceTitleItem setTextRes(int textRes) {
171 | this.text = null;
172 | this.textRes = textRes;
173 | return this;
174 | }
175 |
176 | public CharSequence getDesc() {
177 | return desc;
178 | }
179 |
180 | public MaterialPreferenceTitleItem setDesc(CharSequence desc) {
181 | this.descRes = 0;
182 | this.desc = desc;
183 | return this;
184 | }
185 |
186 | public int getDescRes() {
187 | return descRes;
188 | }
189 |
190 | public MaterialPreferenceTitleItem setDescRes(int descRes) {
191 | this.desc = null;
192 | this.descRes = textRes;
193 | return this;
194 | }
195 |
196 | public Drawable getIcon() {
197 | return icon;
198 | }
199 |
200 | public MaterialPreferenceTitleItem setIcon(Drawable icon) {
201 | this.iconRes = 0;
202 | this.icon = icon;
203 | return this;
204 | }
205 |
206 | public int getIconRes() {
207 | return iconRes;
208 | }
209 |
210 | public MaterialPreferenceTitleItem setIconRes(int iconRes) {
211 | this.icon = null;
212 | this.iconRes = iconRes;
213 | return this;
214 | }
215 |
216 | public MaterialPreferenceItemOnClickAction getOnClickAction() {
217 | return onClickAction;
218 | }
219 |
220 | public MaterialPreferenceTitleItem setOnClickAction(MaterialPreferenceItemOnClickAction onClickAction) {
221 | this.onClickAction = onClickAction;
222 | return this;
223 | }
224 |
225 | public MaterialPreferenceItemOnClickAction getOnLongClickAction() {
226 | return onLongClickAction;
227 | }
228 |
229 | public MaterialPreferenceTitleItem setOnLongClickAction(MaterialPreferenceItemOnClickAction onLongClickAction) {
230 | this.onLongClickAction = onLongClickAction;
231 | return this;
232 | }
233 | public static class MaterialPreferenceTitleItemViewHolder extends MaterialPreferenceItemViewHolder implements View.OnClickListener, View.OnLongClickListener {
234 | public final View view;
235 | public final ImageView icon;
236 | public final TextView text;
237 | public final TextView desc;
238 | private MaterialPreferenceItemOnClickAction onClickAction;
239 | private MaterialPreferenceItemOnClickAction onLongClickAction;
240 |
241 | MaterialPreferenceTitleItemViewHolder(View view) {
242 | super(view);
243 | this.view = view;
244 | icon = (ImageView) view.findViewById(R.id.mp_item_image);
245 | text = (TextView) view.findViewById(R.id.mp_item_text);
246 | desc = (TextView) view.findViewById(R.id.mp_item_desc);
247 | }
248 |
249 | public void setOnClickAction(MaterialPreferenceItemOnClickAction onClickAction) {
250 | this.onClickAction = onClickAction;
251 | if (onClickAction != null) {
252 | view.setOnClickListener(this);
253 | } else {
254 | view.setClickable(false);
255 | }
256 | }
257 |
258 | public void setOnLongClickAction(MaterialPreferenceItemOnClickAction onLongClickAction) {
259 | this.onLongClickAction = onLongClickAction;
260 | if (onLongClickAction != null) {
261 | view.setOnLongClickListener(this);
262 | } else {
263 | view.setLongClickable(false);
264 | }
265 | }
266 |
267 | @Override
268 | public void onClick(View v) {
269 | if (onClickAction != null) {
270 | onClickAction.onClick();
271 | }
272 | }
273 |
274 | @Override
275 | public boolean onLongClick(View v) {
276 | if (onLongClickAction != null) {
277 | onLongClickAction.onClick();
278 | return true;
279 | }
280 | return false;
281 | }
282 | }
283 |
284 | public static class Builder {
285 |
286 | MaterialPreferenceItemOnClickAction onClickAction = null;
287 | MaterialPreferenceItemOnClickAction onLongClickAction = null;
288 | private CharSequence text = null;
289 | @StringRes
290 | private int textRes = 0;
291 | private CharSequence desc = null;
292 | @StringRes
293 | private int descRes = 0;
294 | private Drawable icon = null;
295 | @DrawableRes
296 | private int iconRes = 0;
297 |
298 | public MaterialPreferenceTitleItem.Builder text(CharSequence text) {
299 | this.text = text;
300 | this.textRes = 0;
301 | return this;
302 | }
303 |
304 |
305 | public MaterialPreferenceTitleItem.Builder text(@StringRes int text) {
306 | this.textRes = text;
307 | this.text = null;
308 | return this;
309 | }
310 |
311 | public MaterialPreferenceTitleItem.Builder desc(CharSequence desc) {
312 | this.desc = desc;
313 | this.descRes = 0;
314 | return this;
315 | }
316 |
317 |
318 | public MaterialPreferenceTitleItem.Builder desc(@StringRes int desc) {
319 | this.descRes = desc;
320 | this.desc = null;
321 | return this;
322 | }
323 |
324 | public MaterialPreferenceTitleItem.Builder icon(Drawable icon) {
325 | this.icon = icon;
326 | this.iconRes = 0;
327 | return this;
328 | }
329 |
330 |
331 | public MaterialPreferenceTitleItem.Builder icon(@DrawableRes int iconRes) {
332 | this.icon = null;
333 | this.iconRes = iconRes;
334 | return this;
335 | }
336 |
337 | public MaterialPreferenceTitleItem.Builder setOnClickAction(MaterialPreferenceItemOnClickAction onClickAction) {
338 | this.onClickAction = onClickAction;
339 | return this;
340 | }
341 |
342 | public MaterialPreferenceTitleItem.Builder setOnLongClickAction(MaterialPreferenceItemOnClickAction onLongClickAction) {
343 | this.onLongClickAction = onLongClickAction;
344 | return this;
345 | }
346 |
347 | public MaterialPreferenceTitleItem build() {
348 | return new MaterialPreferenceTitleItem(this);
349 | }
350 | }
351 | }
352 |
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/model/MaterialPreferenceCard.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference.model;
2 |
3 |
4 | import android.support.annotation.ColorInt;
5 | import android.support.annotation.StringRes;
6 |
7 |
8 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceItem;
9 |
10 | import java.util.ArrayList;
11 | import java.util.Collections;
12 | import java.util.UUID;
13 |
14 | public class MaterialPreferenceCard {
15 |
16 | private String id = "NO-UUID";
17 |
18 | private CharSequence title = null;
19 | private int titleRes = 0;
20 |
21 | private int titleColor = 0;
22 | private int cardColor = 0;
23 |
24 | private ArrayList items = new ArrayList<>();
25 |
26 |
27 | private MaterialPreferenceCard(Builder builder) {
28 | this.id = UUID.randomUUID().toString();
29 | this.title = builder.title;
30 | this.titleRes = builder.titleRes;
31 | this.titleColor = builder.titleColor;
32 | this.cardColor = builder.cardColor;
33 | this.items = builder.items;
34 | }
35 |
36 | public MaterialPreferenceCard(CharSequence title, MaterialPreferenceItem... materialPreferenceItems) {
37 | this.title = title;
38 | Collections.addAll(items, materialPreferenceItems);
39 | }
40 |
41 | public MaterialPreferenceCard(int titleRes, MaterialPreferenceItem... materialPreferenceItems) {
42 | this.titleRes = titleRes;
43 | Collections.addAll(items, materialPreferenceItems);
44 | }
45 |
46 | public CharSequence getTitle() {
47 | return title;
48 | }
49 |
50 | public int getTitleRes() {
51 | return titleRes;
52 | }
53 |
54 | public int getTitleColor() {
55 | return titleColor;
56 | }
57 |
58 | public int getCardColor() {
59 | return cardColor;
60 | }
61 |
62 | public ArrayList getItems() {
63 | return items;
64 | }
65 |
66 | public static class Builder {
67 | private CharSequence title = null;
68 | @StringRes
69 | private int titleRes = 0;
70 |
71 | @ColorInt
72 | private int titleColor = 0;
73 |
74 | @ColorInt
75 | private int cardColor = 0;
76 |
77 | private ArrayList items = new ArrayList<>();
78 |
79 | public Builder title(CharSequence title) {
80 | this.title = title;
81 | this.titleRes = 0;
82 | return this;
83 | }
84 |
85 | public Builder title(@StringRes int titleRes) {
86 | this.titleRes = titleRes;
87 | this.title = null;
88 | return this;
89 | }
90 |
91 | public Builder titleColor(@ColorInt int color) {
92 | this.titleColor = color;
93 | return this;
94 | }
95 |
96 | public Builder cardColor(@ColorInt int cardColor) {
97 | this.cardColor = cardColor;
98 | return this;
99 | }
100 |
101 | public Builder addItem(MaterialPreferenceItem item) {
102 | this.items.add(item);
103 | return this;
104 | }
105 |
106 | public MaterialPreferenceCard build() {
107 | return new MaterialPreferenceCard(this);
108 | }
109 | }
110 |
111 | public String getId() {
112 | return id;
113 | }
114 |
115 | @Override
116 | public String toString() {
117 | String result = "MaterialPreferenceCard{" +
118 | "id='" + id + '\'' +
119 | ", title=" + title +
120 | ", titleRes=" + titleRes +
121 | ", titleColor=" + titleColor +
122 | ", cardColor=" + cardColor + '}';
123 | return result;
124 | }
125 |
126 | public MaterialPreferenceCard(MaterialPreferenceCard card) {
127 | this.id = card.getId();
128 | this.title = card.getTitle();
129 | this.titleRes = card.getTitleRes();
130 | this.titleColor = card.getTitleColor();
131 | this.cardColor = card.getCardColor();
132 | this.items = new ArrayList<>();
133 | for (MaterialPreferenceItem item : card.items) {
134 | this.items.add(item.clone());
135 | }
136 | }
137 |
138 | public MaterialPreferenceCard clone() {
139 | return new MaterialPreferenceCard(this);
140 | }
141 |
142 | }
143 |
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/model/MaterialPreferenceList.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference.model;
2 |
3 |
4 | import java.util.ArrayList;
5 | import java.util.Collections;
6 |
7 | public class MaterialPreferenceList {
8 |
9 | private ArrayList cards = new ArrayList<>();
10 |
11 | private MaterialPreferenceList(Builder builder) {
12 | this.cards = builder.cards;
13 | }
14 |
15 | public MaterialPreferenceList(MaterialPreferenceCard... materialPreferenceCards) {
16 | Collections.addAll(cards, materialPreferenceCards);
17 | }
18 |
19 | public MaterialPreferenceList addCard(MaterialPreferenceCard card) {
20 | cards.add(card);
21 | return this;
22 | }
23 |
24 | public MaterialPreferenceList clearCards(MaterialPreferenceCard card) {
25 | cards.clear();
26 | return this;
27 | }
28 |
29 | public ArrayList getCards() {
30 | return cards;
31 | }
32 |
33 | public static class Builder {
34 | private ArrayList cards = new ArrayList<>();
35 |
36 | public Builder addCard(MaterialPreferenceCard card) {
37 | this.cards.add(card);
38 | return this;
39 | }
40 |
41 | public MaterialPreferenceList build() {
42 | return new MaterialPreferenceList(this);
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/util/DefaultViewTypeManager.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference.util;
2 |
3 | import android.content.Context;
4 | import android.view.View;
5 |
6 | import com.francoisdexemple.materialpreference.holders.MaterialPreferenceItemViewHolder;
7 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceActionItem;
8 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceCheckBoxItem;
9 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceItem;
10 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceSwitchItem;
11 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceTitleItem;
12 |
13 | import static com.francoisdexemple.materialpreference.util.ViewTypeManager.ItemLayout.ACTION_LAYOUT;
14 | import static com.francoisdexemple.materialpreference.util.ViewTypeManager.ItemLayout.CHECKBOX_LAYOUT;
15 | import static com.francoisdexemple.materialpreference.util.ViewTypeManager.ItemLayout.SWITCH_LAYOUT;
16 | import static com.francoisdexemple.materialpreference.util.ViewTypeManager.ItemLayout.TITLE_LAYOUT;
17 | import static com.francoisdexemple.materialpreference.util.ViewTypeManager.ItemType.ACTION_ITEM;
18 | import static com.francoisdexemple.materialpreference.util.ViewTypeManager.ItemType.CHECKBOX_ITEM;
19 | import static com.francoisdexemple.materialpreference.util.ViewTypeManager.ItemType.SWITCH_ITEM;
20 | import static com.francoisdexemple.materialpreference.util.ViewTypeManager.ItemType.TITLE_ITEM;
21 |
22 |
23 | public class DefaultViewTypeManager extends ViewTypeManager {
24 |
25 | public static final class ItemType {
26 | public static final int ACTION_ITEM = ViewTypeManager.ItemType.ACTION_ITEM;
27 | public static final int SWITCH_ITEM = ViewTypeManager.ItemType.SWITCH_ITEM;
28 | public static final int TITLE_ITEM = ViewTypeManager.ItemType.TITLE_ITEM;
29 | public static final int CHECKBOX_ITEM = ViewTypeManager.ItemType.CHECKBOX_ITEM;
30 | }
31 |
32 | public static final class ItemLayout {
33 | public static final int ACTION_LAYOUT = ViewTypeManager.ItemLayout.ACTION_LAYOUT;
34 | public static final int TITLE_LAYOUT = ViewTypeManager.ItemLayout.TITLE_LAYOUT;
35 | public static final int SWITCH_LAYOUT = ViewTypeManager.ItemLayout.SWITCH_LAYOUT;
36 | public static final int CHECKBOX_LAYOUT = ViewTypeManager.ItemLayout.CHECKBOX_LAYOUT;
37 | }
38 |
39 | public int getLayout(int itemType) {
40 | switch (itemType) {
41 | case ACTION_ITEM:
42 | return ACTION_LAYOUT;
43 | case TITLE_ITEM:
44 | return TITLE_LAYOUT;
45 | case SWITCH_ITEM:
46 | return SWITCH_LAYOUT;
47 | case CHECKBOX_ITEM:
48 | return CHECKBOX_LAYOUT;
49 | default:
50 | return -1;
51 | }
52 | }
53 |
54 | public MaterialPreferenceItemViewHolder getViewHolder(int itemType, View view) {
55 | switch (itemType) {
56 | case ACTION_ITEM:
57 | return MaterialPreferenceActionItem.getViewHolder(view);
58 | case TITLE_ITEM:
59 | return MaterialPreferenceTitleItem.getViewHolder(view);
60 | case SWITCH_ITEM:
61 | return MaterialPreferenceSwitchItem.getViewHolder(view);
62 | case CHECKBOX_ITEM:
63 | return MaterialPreferenceCheckBoxItem.getViewHolder(view);
64 | default:
65 | return null;
66 | }
67 | }
68 |
69 | public void setupItem(int itemType, MaterialPreferenceItemViewHolder holder, MaterialPreferenceItem item, Context context) {
70 | switch (itemType) {
71 | case ACTION_ITEM:
72 | MaterialPreferenceActionItem.setupItem((MaterialPreferenceActionItem.MaterialPreferenceActionItemViewHolder) holder, (MaterialPreferenceActionItem) item, context);
73 | break;
74 | case TITLE_ITEM:
75 | MaterialPreferenceTitleItem.setupItem((MaterialPreferenceTitleItem.MaterialPreferenceTitleItemViewHolder) holder, (MaterialPreferenceTitleItem) item, context);
76 | break;
77 | case SWITCH_ITEM:
78 | MaterialPreferenceSwitchItem.setupItem((MaterialPreferenceSwitchItem.MaterialPreferenceSwitchItemViewHolder) holder,(MaterialPreferenceSwitchItem) item, context);
79 | break;
80 | case CHECKBOX_ITEM:
81 | MaterialPreferenceCheckBoxItem.setupItem((MaterialPreferenceCheckBoxItem.MaterialPreferenceCheckBoxItemViewHolder) holder, (MaterialPreferenceCheckBoxItem) item, context);
82 | }
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/util/OpenSourceLicense.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference.util;
2 |
3 |
4 | import com.francoisdexemple.materialpreference.R;
5 |
6 | public enum OpenSourceLicense {
7 | APACHE_2, MIT, GNU_GPL_3, BSD;
8 |
9 | public int getResourceId() {
10 | switch (this) {
11 | case APACHE_2:
12 | return R.string.license_apache2;
13 | case MIT:
14 | return R.string.license_mit;
15 | case GNU_GPL_3:
16 | return R.string.license_gpl;
17 | case BSD:
18 | return R.string.license_bsd;
19 | default:
20 | return -1;
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/materialpreference/src/main/java/com/francoisdexemple/materialpreference/util/ViewTypeManager.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference.util;
2 |
3 | import android.content.Context;
4 | import android.view.View;
5 |
6 | import com.francoisdexemple.materialpreference.R;
7 | import com.francoisdexemple.materialpreference.holders.MaterialPreferenceItemViewHolder;
8 | import com.francoisdexemple.materialpreference.items.MaterialPreferenceItem;
9 |
10 |
11 | public abstract class ViewTypeManager {
12 |
13 | public static final class ItemType {
14 | public static final int ACTION_ITEM = 0;
15 | public static final int TITLE_ITEM = 1;
16 | public static final int SWITCH_ITEM = 2;
17 | public static final int CHECKBOX_ITEM = 3;
18 | }
19 |
20 | public static final class ItemLayout {
21 | public static final int ACTION_LAYOUT = R.layout.material_preference_action_item;
22 | public static final int TITLE_LAYOUT = R.layout.material_preference_title_item;
23 | public static final int SWITCH_LAYOUT = R.layout.material_preference_switch_item;
24 | public static final int CHECKBOX_LAYOUT = R.layout.material_preference_checkbox_item;
25 | }
26 |
27 | public abstract int getLayout(int itemType);
28 |
29 | public abstract MaterialPreferenceItemViewHolder getViewHolder(int itemType, View view);
30 |
31 | public abstract void setupItem(int itemType, MaterialPreferenceItemViewHolder holder, MaterialPreferenceItem item, Context context);
32 | }
33 |
--------------------------------------------------------------------------------
/materialpreference/src/main/res/layout/material_preference_action_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
25 |
26 |
38 |
39 |
46 |
47 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/materialpreference/src/main/res/layout/material_preference_activity.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
15 |
16 |
23 |
24 |
25 |
26 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/materialpreference/src/main/res/layout/material_preference_checkbox_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
28 |
29 |
41 |
42 |
54 |
55 |
69 |
70 |
76 |
77 |
--------------------------------------------------------------------------------
/materialpreference/src/main/res/layout/material_preference_fragment.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
--------------------------------------------------------------------------------
/materialpreference/src/main/res/layout/material_preference_list_card.xml:
--------------------------------------------------------------------------------
1 |
2 |
26 |
27 |
31 |
32 |
45 |
46 |
53 |
54 |
55 |
56 |
--------------------------------------------------------------------------------
/materialpreference/src/main/res/layout/material_preference_switch_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
28 |
29 |
41 |
42 |
54 |
55 |
69 |
70 |
76 |
77 |
--------------------------------------------------------------------------------
/materialpreference/src/main/res/layout/material_preference_title_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
14 |
15 |
29 |
30 |
36 |
37 |
46 |
47 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/materialpreference/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/materialpreference/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | #de000000
5 | #8a000000
6 |
7 | #FFFFFF
8 | #b4ffffff
9 |
10 | #616161
11 | #E0E0E0
12 |
--------------------------------------------------------------------------------
/materialpreference/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 16dp
6 | 8dp
7 | 4dp
8 |
9 | 24dp
10 | 32dp
11 | 40dp
12 |
13 | 2dp
14 | 2dp
15 |
16 |
17 |
--------------------------------------------------------------------------------
/materialpreference/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | MaterialPreference
3 |
4 | Licenses
5 | Material Preference
6 |
7 | Close
8 | Send email
9 |
10 | No apps to handle action
11 |
12 |
13 | "\nCopyright %1$s %2$s\n"
14 |
15 | "\nLicensed under the Apache License, Version 2.0 (the \"License\");"
16 | "you may not use this file except in compliance with the License."
17 | "You may obtain a copy of the License at\n"
18 |
19 | "\nhttp://www.apache.org/licenses/LICENSE-2.0\n"
20 |
21 | "\nUnless required by applicable law or agreed to in writing, software"
22 | "distributed under the License is distributed on an \"AS IS\" BASIS,"
23 | "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied."
24 | "See the License for the specific language governing permissions and"
25 | "limitations under the License."
26 |
27 |
28 |
29 | "\nMIT License\n"
30 |
31 | "\nCopyright © %1$s %2$s\n"
32 |
33 | "\nPermission is hereby granted, free of charge, to any person obtaining a copy"
34 | "of this software and associated documentation files (the "Software"), to deal"
35 | "in the Software without restriction, including without limitation the rights"
36 | "to use, copy, modify, merge, publish, distribute, sublicense, and/or sell"
37 | "copies of the Software, and to permit persons to whom the Software is"
38 | "furnished to do so, subject to the following conditions:\n"
39 |
40 | "\nThe above copyright notice and this permission notice shall be included in all"
41 | "copies or substantial portions of the Software.\n"
42 |
43 | "\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR"
44 | "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,"
45 | "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE"
46 | "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER"
47 | "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,"
48 | "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE"
49 | "SOFTWARE."
50 |
51 |
52 |
53 | "\nCopyright © %1$s %2$s\n"
54 |
55 | "\nThis program is free software: you can redistribute it and/or modify"
56 | "it under the terms of the GNU General Public License as published by"
57 | "the Free Software Foundation, either version 3 of the License, or"
58 | "(at your option) any later version.\n"
59 |
60 | "\nThis program is distributed in the hope that it will be useful,"
61 | "but WITHOUT ANY WARRANTY; without even the implied warranty of"
62 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
63 | "GNU General Public License for more details.\n"
64 |
65 | "\nYou should have received a copy of the GNU General Public License"
66 | "along with this program. If not, see http://www.gnu.org/licenses.\n"
67 |
68 |
69 |
70 | "\nCopyright © %1$s %2$s\n"
71 |
72 | "\nRedistribution and use in source and binary forms, with or without"
73 | "modification, are permitted provided that the following conditions are met:\n"
74 |
75 | "\n1. Redistributions of source code must retain the above copyright notice, this"
76 | " list of conditions and the following disclaimer.\n"
77 | "2. Redistributions in binary form must reproduce the above copyright notice,"
78 | " this list of conditions and the following disclaimer in the documentation"
79 | " and/or other materials provided with the distribution.\n"
80 |
81 | "\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND"
82 | "ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED"
83 | "WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE"
84 | "DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR"
85 | "ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES"
86 | "(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;"
87 | "LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND"
88 | "ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT"
89 | "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS"
90 | "SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
91 |
92 | "\nThe views and conclusions contained in the software and documentation are those"
93 | "of the authors and should not be interpreted as representing official policies,"
94 | "either expressed or implied, of the FreeBSD Project.\n"
95 |
96 |
97 |
--------------------------------------------------------------------------------
/materialpreference/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
19 |
20 |
23 |
24 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/materialpreference/src/test/java/com/francoisdexemple/materialpreference/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.francoisdexemple.materialpreference;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/screenshots/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/screenshots/1.png
--------------------------------------------------------------------------------
/screenshots/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/screenshots/2.png
--------------------------------------------------------------------------------
/screenshots/2_mini.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/screenshots/2_mini.png
--------------------------------------------------------------------------------
/screenshots/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/screenshots/3.png
--------------------------------------------------------------------------------
/screenshots/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/screenshots/4.png
--------------------------------------------------------------------------------
/screenshots/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/screenshots/5.png
--------------------------------------------------------------------------------
/screenshots/6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/filol/material-preference-library/335bdb2a3731a2becaf95f2a35259e6a23458b22/screenshots/6.png
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':materialpreference'
2 |
--------------------------------------------------------------------------------