Skip to content
This repository was archived by the owner on May 1, 2021. It is now read-only.

Commit 1aaa175

Browse files
added support for CheckBoxPreference
1 parent 2f70b92 commit 1aaa175

File tree

5 files changed

+139
-4
lines changed

5 files changed

+139
-4
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/res/xml/settings_example.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@
4848
android:title="SwitchPreference title"
4949
app:summaryOff="summaryOff"
5050
app:summaryOn="summaryOn"/>
51+
52+
<com.lb.material_preferences_library.custom_preferences.CheckBoxPreference
53+
android:defaultValue="true"
54+
android:key="checkboxPref"
55+
android:summary="CheckBoxPreference summary"
56+
android:title="CheckBoxPreference title"
57+
/>
5158
</PreferenceCategory>
5259

5360
<PreferenceCategory
@@ -85,6 +92,7 @@
8592
android:positiveButtonText="@null"
8693
android:summary="%s"
8794
android:title="ListPreference title"/>
95+
8896
</PreferenceCategory>
8997

9098

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (C) 2007 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.lb.material_preferences_library.custom_preferences;
18+
19+
import android.content.Context;
20+
import android.content.res.TypedArray;
21+
import android.util.AttributeSet;
22+
import android.view.View;
23+
import android.widget.Checkable;
24+
25+
import com.lb.material_preferences_library.R;
26+
27+
/**
28+
* A {@link android.preference.Preference} that provides checkbox widget
29+
* functionality.
30+
* <p/>
31+
* This preference will store a boolean into the SharedPreferences.
32+
*
33+
* @attr ref android.R.styleable#CheckBoxPreference_summaryOff
34+
* @attr ref android.R.styleable#CheckBoxPreference_summaryOn
35+
* @attr ref android.R.styleable#CheckBoxPreference_disableDependentsState
36+
*/
37+
public class CheckBoxPreference extends TwoStatePreference
38+
{
39+
40+
public CheckBoxPreference(Context context,AttributeSet attrs,int defStyleAttr)
41+
{
42+
super(context,attrs,defStyleAttr);
43+
}
44+
45+
public CheckBoxPreference(Context context,AttributeSet attrs,int defStyleAttr,int defStyleRes)
46+
{
47+
super(context,attrs,defStyleAttr,defStyleRes);
48+
}
49+
50+
@Override
51+
protected void init(final Context context,final AttributeSet attrs,final int defStyleAttr,final int defStyleRes)
52+
{
53+
super.init(context,attrs,defStyleAttr,defStyleRes);
54+
setWidgetLayoutResource(R.layout.mpl__preference_widget_checkbox);
55+
final TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.CheckBoxPreference,defStyleAttr,defStyleRes);
56+
setSummaryOn(a.getString(R.styleable.CheckBoxPreference_summaryOn));
57+
setSummaryOff(a.getString(R.styleable.CheckBoxPreference_summaryOff));
58+
setDisableDependentsState(a.getBoolean(R.styleable.CheckBoxPreference_disableDependentsState,false));
59+
a.recycle();
60+
}
61+
62+
public CheckBoxPreference(Context context,AttributeSet attrs)
63+
{
64+
this(context,attrs,R.attr.checkBoxPreferenceStyle);
65+
}
66+
67+
public CheckBoxPreference(Context context)
68+
{
69+
this(context,null);
70+
}
71+
72+
@Override
73+
protected void onBindView(View view)
74+
{
75+
super.onBindView(view);
76+
View checkboxView=view.findViewById(android.R.id.checkbox);
77+
if(checkboxView!=null&&checkboxView instanceof Checkable)
78+
((Checkable)checkboxView).setChecked(mChecked);
79+
syncSummaryView(view);
80+
}
81+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright (C) 2006 The Android Open Source Project
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
<!-- Layout used by CheckBoxPreference for the checkbox style. This is inflated
18+
inside android.R.layout.preference. -->
19+
<android.support.v7.widget.AppCompatCheckBox
20+
android:id="@+android:id/checkbox"
21+
xmlns:android="http://schemas.android.com/apk/res/android"
22+
android:layout_width="wrap_content"
23+
android:layout_height="wrap_content"
24+
android:background="@null"
25+
android:clickable="false"
26+
android:focusable="false"/>

material_preferences_library/src/main/res/values/mpl__attrs.xml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<attr name="editTextPreferenceStyle" format="reference"/>
88
<!-- Default style for switch preferences. -->
99
<attr name="switchPreferenceStyle" format="reference"/>
10+
<!-- Default style for CheckBoxPreference. -->
11+
<attr name="checkBoxPreferenceStyle" format="reference"/>
1012

1113
<!-- Base attributes available to ListPreference. -->
1214
<declare-styleable name="ListPreference">
@@ -38,15 +40,30 @@
3840
<attr name="dialogLayout" format="reference"/>
3941
</declare-styleable>
4042

43+
<!-- Base attributes available to CheckBoxPreference. -->
44+
<declare-styleable name="CheckBoxPreference">
45+
<!-- The summary for the Preference in a PreferenceActivity screen when the
46+
CheckBoxPreference is checked. If separate on/off summaries are not
47+
needed, the summary attribute can be used instead. -->
48+
<attr name="summaryOn" format="string"/>
49+
<!-- The summary for the Preference in a PreferenceActivity screen when the
50+
CheckBoxPreference is unchecked. If separate on/off summaries are not
51+
needed, the summary attribute can be used instead. -->
52+
<attr name="summaryOff" format="string"/>
53+
<!-- The state (true for on, or false for off) that causes dependents to be disabled. By default,
54+
dependents will be disabled when this is unchecked, so the value of this preference is false. -->
55+
<attr name="disableDependentsState" format="boolean"/>
56+
</declare-styleable>
57+
4158
<declare-styleable name="SwitchPreference">
4259
<!-- The summary for the Preference in a PreferenceActivity screen when the
4360
SwitchPreference is checked. If separate on/off summaries are not
4461
needed, the summary attribute can be used instead. -->
45-
<attr name="summaryOn" format="string"/>
62+
<attr name="summaryOn"/>
4663
<!-- The summary for the Preference in a PreferenceActivity screen when the
4764
SwitchPreference is unchecked. If separate on/off summaries are not
4865
needed, the summary attribute can be used instead. -->
49-
<attr name="summaryOff" format="string"/>
66+
<attr name="summaryOff" />
5067
<!-- The text used on the switch itself when in the "on" state.
5168
This should be a very SHORT string, as it appears in a small space. -->
5269
<attr name="switchTextOn" format="string"/>
@@ -55,6 +72,9 @@
5572
<attr name="switchTextOff" format="string"/>
5673
<!-- The state (true for on, or false for off) that causes dependents to be disabled. By default,
5774
dependents will be disabled when this is unchecked, so the value of this preference is false. -->
58-
<attr name="disableDependentsState" format="boolean"/>
75+
<attr name="disableDependentsState" />
5976
</declare-styleable>
77+
78+
79+
6080
</resources>

0 commit comments

Comments
 (0)