Skip to content

Commit 47d515c

Browse files
committed
Added app:useStockLayout attribute for preferences
1 parent 3a6d213 commit 47d515c

File tree

7 files changed

+34
-6
lines changed

7 files changed

+34
-6
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,13 @@ with user input that's received through typing or selection. Material Dialogs in
12051205
`MaterialListPreference`, and `MaterialMultiSelectListPreference` classes that can be used in your preferences XML to automatically use Material-themed
12061206
dialogs. See the sample project for details.
12071207

1208+
By default, all of these preference classes will set their layout to `R.layout.md_preference_custom`. If you
1209+
don't want a default layout to be set, you can provide an attribute on the preferences in your XML:
1210+
1211+
```
1212+
app:useStockLayout="true"
1213+
```
1214+
12081215
---
12091216

12101217
# File Selector Dialogs

commons/src/main/java/com/afollestad/materialdialogs/prefs/MaterialDialogPreference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public MaterialDialogPreference(Context context, AttributeSet attrs, int defStyl
4949

5050
private void init(Context context, AttributeSet attrs) {
5151
this.context = context;
52-
PrefUtil.setLayoutResource(this, attrs);
52+
PrefUtil.setLayoutResource(context, this, attrs);
5353
}
5454

5555
@Override

commons/src/main/java/com/afollestad/materialdialogs/prefs/MaterialEditTextPreference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public MaterialEditTextPreference(Context context, AttributeSet attrs, int defSt
6666

6767

6868
private void init(Context context, AttributeSet attrs) {
69-
PrefUtil.setLayoutResource(this, attrs);
69+
PrefUtil.setLayoutResource(context, this, attrs);
7070
int fallback;
7171
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
7272
fallback = DialogUtils.resolveColor(context, android.R.attr.colorAccent);

commons/src/main/java/com/afollestad/materialdialogs/prefs/MaterialListPreference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public MaterialListPreference(Context context, AttributeSet attrs, int defStyleA
5555

5656
private void init(Context context, AttributeSet attrs) {
5757
this.context = context;
58-
PrefUtil.setLayoutResource(this, attrs);
58+
PrefUtil.setLayoutResource(context, this, attrs);
5959
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1)
6060
setWidgetLayoutResource(0);
6161
}

commons/src/main/java/com/afollestad/materialdialogs/prefs/MaterialMultiSelectListPreference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void setEntries(CharSequence[] entries) {
6363

6464
private void init(Context context, AttributeSet attrs) {
6565
this.context = context;
66-
PrefUtil.setLayoutResource(this, attrs);
66+
PrefUtil.setLayoutResource(context, this, attrs);
6767
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1)
6868
setWidgetLayoutResource(0);
6969
}

commons/src/main/java/com/afollestad/materialdialogs/prefs/PrefUtil.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.afollestad.materialdialogs.prefs;
22

3+
import android.content.Context;
4+
import android.content.res.TypedArray;
35
import android.content.res.XmlResourceParser;
46
import android.preference.Preference;
57
import android.preference.PreferenceManager;
@@ -19,7 +21,7 @@ class PrefUtil {
1921
private PrefUtil() {
2022
}
2123

22-
public static void setLayoutResource(@NonNull Preference preference, @Nullable AttributeSet attrs) {
24+
public static void setLayoutResource(@NonNull Context context, @NonNull Preference preference, @Nullable AttributeSet attrs) {
2325
boolean foundLayout = false;
2426
if (attrs != null) {
2527
for (int i = 0; i < attrs.getAttributeCount(); i++) {
@@ -31,7 +33,18 @@ public static void setLayoutResource(@NonNull Preference preference, @Nullable A
3133
}
3234
}
3335
}
34-
if (!foundLayout)
36+
37+
boolean useStockLayout = false;
38+
if (attrs != null) {
39+
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Preference, 0, 0);
40+
try {
41+
useStockLayout = a.getBoolean(R.styleable.Preference_useStockLayout, false);
42+
} finally {
43+
a.recycle();
44+
}
45+
}
46+
47+
if (!foundLayout && !useStockLayout)
3548
preference.setLayoutResource(R.layout.md_preference_custom);
3649
}
3750

commons/src/main/res/values/attrs.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<declare-styleable name="Preference">
5+
<attr name="useStockLayout" format="boolean" />
6+
</declare-styleable>
7+
8+
</resources>

0 commit comments

Comments
 (0)