blob: fee3f0f17dbcdf0581d6c70f5723afbce899ce6f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.preference;
18
19import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.util.AttributeSet;
22import android.view.View;
23import android.widget.Checkable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
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 */
Adam Powellbe0a4532010-11-29 17:47:48 -080035public class CheckBoxPreference extends TwoStatePreference {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
Alan Viverette617feb92013-09-09 18:09:13 -070037 public CheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr) {
38 this(context, attrs, defStyleAttr, 0);
39 }
40
41 public CheckBoxPreference(
42 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
43 super(context, attrs, defStyleAttr, defStyleRes);
44
45 final TypedArray a = context.obtainStyledAttributes(attrs,
46 com.android.internal.R.styleable.CheckBoxPreference, defStyleAttr, defStyleRes);
Adam Powellbe0a4532010-11-29 17:47:48 -080047 setSummaryOn(a.getString(com.android.internal.R.styleable.CheckBoxPreference_summaryOn));
48 setSummaryOff(a.getString(com.android.internal.R.styleable.CheckBoxPreference_summaryOff));
49 setDisableDependentsState(a.getBoolean(
50 com.android.internal.R.styleable.CheckBoxPreference_disableDependentsState, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 a.recycle();
52 }
53
54 public CheckBoxPreference(Context context, AttributeSet attrs) {
55 this(context, attrs, com.android.internal.R.attr.checkBoxPreferenceStyle);
56 }
57
58 public CheckBoxPreference(Context context) {
59 this(context, null);
60 }
svetoslavganov75986cf2009-05-14 22:28:01 -070061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 @Override
63 protected void onBindView(View view) {
64 super.onBindView(view);
svetoslavganov75986cf2009-05-14 22:28:01 -070065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 View checkboxView = view.findViewById(com.android.internal.R.id.checkbox);
67 if (checkboxView != null && checkboxView instanceof Checkable) {
68 ((Checkable) checkboxView).setChecked(mChecked);
69 }
70
Adam Powellbe0a4532010-11-29 17:47:48 -080071 syncSummaryView(view);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073}