• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.android.settings.widget;
18 
19 import android.content.Context;
20 import android.support.v4.content.res.TypedArrayUtils;
21 import android.support.v7.preference.CheckBoxPreference;
22 import android.support.v7.preference.PreferenceViewHolder;
23 import android.util.AttributeSet;
24 import android.widget.TextView;
25 
26 import com.android.settings.R;
27 
28 /**
29  * Check box preference with check box replaced by radio button.
30  *
31  * Functionally speaking, it's actually a CheckBoxPreference. We only modified
32  * the widget to RadioButton to make it "look like" a RadioButtonPreference.
33  *
34  * In other words, there's no "RadioButtonPreferenceGroup" in this
35  * implementation. When you check one RadioButtonPreference, if you want to
36  * uncheck all the other preferences, you should do that by code yourself.
37  */
38 public class RadioButtonPreference extends CheckBoxPreference {
39     public interface OnClickListener {
onRadioButtonClicked(RadioButtonPreference emiter)40        void onRadioButtonClicked(RadioButtonPreference emiter);
41     }
42 
43     private OnClickListener mListener = null;
44 
RadioButtonPreference(Context context, AttributeSet attrs, int defStyle)45     public RadioButtonPreference(Context context, AttributeSet attrs, int defStyle) {
46         super(context, attrs, defStyle);
47         setWidgetLayoutResource(R.layout.preference_widget_radiobutton);
48     }
49 
RadioButtonPreference(Context context, AttributeSet attrs)50     public RadioButtonPreference(Context context, AttributeSet attrs) {
51         this(context, attrs, TypedArrayUtils.getAttr(context,
52                 android.support.v7.preference.R.attr.preferenceStyle,
53                 android.R.attr.preferenceStyle));
54     }
55 
RadioButtonPreference(Context context)56     public RadioButtonPreference(Context context) {
57         this(context, null);
58     }
59 
setOnClickListener(OnClickListener listener)60     public void setOnClickListener(OnClickListener listener) {
61         mListener = listener;
62     }
63 
64     @Override
onClick()65     public void onClick() {
66         if (mListener != null) {
67             mListener.onRadioButtonClicked(this);
68         }
69     }
70 
71     @Override
onBindViewHolder(PreferenceViewHolder view)72     public void onBindViewHolder(PreferenceViewHolder view) {
73         super.onBindViewHolder(view);
74 
75         TextView title = (TextView) view.findViewById(android.R.id.title);
76         if (title != null) {
77             title.setSingleLine(false);
78             title.setMaxLines(3);
79         }
80     }
81 }
82