• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.preference;
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 /**
26  * A {@link Preference} that provides checkbox widget
27  * functionality.
28  * <p>
29  * This preference will store a boolean into the SharedPreferences.
30  *
31  * @attr ref android.R.styleable#CheckBoxPreference_summaryOff
32  * @attr ref android.R.styleable#CheckBoxPreference_summaryOn
33  * @attr ref android.R.styleable#CheckBoxPreference_disableDependentsState
34  *
35  * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
36  *      <a href="{@docRoot}reference/androidx/preference/package-summary.html">
37  *      Preference Library</a> for consistent behavior across all devices. For more information on
38  *      using the AndroidX Preference Library see
39  *      <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
40  */
41 @Deprecated
42 public class CheckBoxPreference extends TwoStatePreference {
43 
CheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr)44     public CheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr) {
45         this(context, attrs, defStyleAttr, 0);
46     }
47 
CheckBoxPreference( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)48     public CheckBoxPreference(
49             Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
50         super(context, attrs, defStyleAttr, defStyleRes);
51 
52         final TypedArray a = context.obtainStyledAttributes(attrs,
53                 com.android.internal.R.styleable.CheckBoxPreference, defStyleAttr, defStyleRes);
54         setSummaryOn(a.getString(com.android.internal.R.styleable.CheckBoxPreference_summaryOn));
55         setSummaryOff(a.getString(com.android.internal.R.styleable.CheckBoxPreference_summaryOff));
56         setDisableDependentsState(a.getBoolean(
57                 com.android.internal.R.styleable.CheckBoxPreference_disableDependentsState, false));
58         a.recycle();
59     }
60 
CheckBoxPreference(Context context, AttributeSet attrs)61     public CheckBoxPreference(Context context, AttributeSet attrs) {
62         this(context, attrs, com.android.internal.R.attr.checkBoxPreferenceStyle);
63     }
64 
CheckBoxPreference(Context context)65     public CheckBoxPreference(Context context) {
66         this(context, null);
67     }
68 
69     @Override
onBindView(View view)70     protected void onBindView(View view) {
71         super.onBindView(view);
72 
73         View checkboxView = view.findViewById(com.android.internal.R.id.checkbox);
74         if (checkboxView != null && checkboxView instanceof Checkable) {
75             ((Checkable) checkboxView).setChecked(mChecked);
76         }
77 
78         syncSummaryView(view);
79     }
80 }
81