CheckBoxPreference.java revision 84765eaea7da18d0576db557959129e9d0db8e8c
1/*
2 * Copyright (C) 2015 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
17package android.support.v7.preference;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.support.v4.content.res.TypedArrayUtils;
22import android.util.AttributeSet;
23import android.view.View;
24import android.widget.Checkable;
25
26/**
27 * A {@link Preference} that provides checkbox widget
28 * functionality.
29 * <p>
30 * This preference will store a boolean into the SharedPreferences.
31 *
32 * @attr ref android.R.styleable#CheckBoxPreference_summaryOff
33 * @attr ref android.R.styleable#CheckBoxPreference_summaryOn
34 * @attr ref android.R.styleable#CheckBoxPreference_disableDependentsState
35 */
36public class CheckBoxPreference extends TwoStatePreference {
37
38    public CheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr) {
39        this(context, attrs, defStyleAttr, 0);
40    }
41
42    public CheckBoxPreference(
43            Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
44        super(context, attrs, defStyleAttr, defStyleRes);
45
46        final TypedArray a = context.obtainStyledAttributes(attrs,
47                R.styleable.CheckBoxPreference, defStyleAttr, defStyleRes);
48
49        setSummaryOn(TypedArrayUtils.getString(a, R.styleable.CheckBoxPreference_summaryOn,
50                R.styleable.CheckBoxPreference_android_summaryOn));
51
52        setSummaryOff(TypedArrayUtils.getString(a, R.styleable.CheckBoxPreference_summaryOff,
53                R.styleable.CheckBoxPreference_android_summaryOff));
54
55        setDisableDependentsState(TypedArrayUtils.getBoolean(a,
56                R.styleable.CheckBoxPreference_disableDependentsState,
57                R.styleable.CheckBoxPreference_android_disableDependentsState, false));
58
59        a.recycle();
60    }
61
62    public CheckBoxPreference(Context context, AttributeSet attrs) {
63        this(context, attrs, R.attr.checkBoxPreferenceStyle);
64    }
65
66    public CheckBoxPreference(Context context) {
67        this(context, null);
68    }
69
70    @Override
71    protected void onBindViewHolder(PreferenceViewHolder holder) {
72        super.onBindViewHolder(holder);
73
74        View checkboxView = holder.findViewById(R.id.checkbox);
75        if (checkboxView != null && checkboxView instanceof Checkable) {
76            ((Checkable) checkboxView).setChecked(mChecked);
77        }
78
79        syncSummaryView(holder);
80    }
81}
82