• 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.widget;
18 
19 import com.android.internal.R;
20 
21 import android.content.Context;
22 import android.content.res.TypedArray;
23 import android.graphics.Canvas;
24 import android.graphics.drawable.Drawable;
25 import android.util.AttributeSet;
26 import android.view.Gravity;
27 import android.view.accessibility.AccessibilityEvent;
28 
29 
30 /**
31  * An extension to TextView that supports the {@link android.widget.Checkable} interface.
32  * This is useful when used in a {@link android.widget.ListView ListView} where the it's
33  * {@link android.widget.ListView#setChoiceMode(int) setChoiceMode} has been set to
34  * something other than {@link android.widget.ListView#CHOICE_MODE_NONE CHOICE_MODE_NONE}.
35  *
36  */
37 public class CheckedTextView extends TextView implements Checkable {
38     private boolean mChecked;
39     private int mCheckMarkResource;
40     private Drawable mCheckMarkDrawable;
41     private int mBasePaddingRight;
42     private int mCheckMarkWidth;
43 
44     private static final int[] CHECKED_STATE_SET = {
45         R.attr.state_checked
46     };
47 
CheckedTextView(Context context)48     public CheckedTextView(Context context) {
49         this(context, null);
50     }
51 
CheckedTextView(Context context, AttributeSet attrs)52     public CheckedTextView(Context context, AttributeSet attrs) {
53         this(context, attrs, 0);
54     }
55 
CheckedTextView(Context context, AttributeSet attrs, int defStyle)56     public CheckedTextView(Context context, AttributeSet attrs, int defStyle) {
57         super(context, attrs, defStyle);
58 
59         TypedArray a = context.obtainStyledAttributes(attrs,
60                 R.styleable.CheckedTextView, defStyle, 0);
61 
62         Drawable d = a.getDrawable(R.styleable.CheckedTextView_checkMark);
63         if (d != null) {
64             setCheckMarkDrawable(d);
65         }
66 
67         boolean checked = a.getBoolean(R.styleable.CheckedTextView_checked, false);
68         setChecked(checked);
69 
70         a.recycle();
71     }
72 
toggle()73     public void toggle() {
74         setChecked(!mChecked);
75     }
76 
isChecked()77     public boolean isChecked() {
78         return mChecked;
79     }
80 
81     /**
82      * <p>Changes the checked state of this text view.</p>
83      *
84      * @param checked true to check the text, false to uncheck it
85      */
setChecked(boolean checked)86     public void setChecked(boolean checked) {
87         if (mChecked != checked) {
88             mChecked = checked;
89             refreshDrawableState();
90         }
91     }
92 
93 
94     /**
95      * Set the checkmark to a given Drawable, identified by its resourece id. This will be drawn
96      * when {@link #isChecked()} is true.
97      *
98      * @param resid The Drawable to use for the checkmark.
99      */
setCheckMarkDrawable(int resid)100     public void setCheckMarkDrawable(int resid) {
101         if (resid != 0 && resid == mCheckMarkResource) {
102             return;
103         }
104 
105         mCheckMarkResource = resid;
106 
107         Drawable d = null;
108         if (mCheckMarkResource != 0) {
109             d = getResources().getDrawable(mCheckMarkResource);
110         }
111         setCheckMarkDrawable(d);
112     }
113 
114     /**
115      * Set the checkmark to a given Drawable. This will be drawn when {@link #isChecked()} is true.
116      *
117      * @param d The Drawable to use for the checkmark.
118      */
setCheckMarkDrawable(Drawable d)119     public void setCheckMarkDrawable(Drawable d) {
120         if (d != null) {
121             if (mCheckMarkDrawable != null) {
122                 mCheckMarkDrawable.setCallback(null);
123                 unscheduleDrawable(mCheckMarkDrawable);
124             }
125             d.setCallback(this);
126             d.setVisible(getVisibility() == VISIBLE, false);
127             d.setState(CHECKED_STATE_SET);
128             setMinHeight(d.getIntrinsicHeight());
129 
130             mCheckMarkWidth = d.getIntrinsicWidth();
131             mPaddingRight = mCheckMarkWidth + mBasePaddingRight;
132             d.setState(getDrawableState());
133             mCheckMarkDrawable = d;
134         } else {
135             mPaddingRight = mBasePaddingRight;
136         }
137         requestLayout();
138     }
139 
140     @Override
setPadding(int left, int top, int right, int bottom)141     public void setPadding(int left, int top, int right, int bottom) {
142         super.setPadding(left, top, right, bottom);
143         mBasePaddingRight = mPaddingRight;
144     }
145 
146     @Override
onDraw(Canvas canvas)147     protected void onDraw(Canvas canvas) {
148         super.onDraw(canvas);
149 
150         final Drawable checkMarkDrawable = mCheckMarkDrawable;
151         if (checkMarkDrawable != null) {
152             final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
153             final int height = checkMarkDrawable.getIntrinsicHeight();
154 
155             int y = 0;
156 
157             switch (verticalGravity) {
158                 case Gravity.BOTTOM:
159                     y = getHeight() - height;
160                     break;
161                 case Gravity.CENTER_VERTICAL:
162                     y = (getHeight() - height) / 2;
163                     break;
164             }
165 
166             int right = getWidth();
167             checkMarkDrawable.setBounds(
168                     right - mCheckMarkWidth - mBasePaddingRight,
169                     y,
170                     right - mBasePaddingRight,
171                     y + height);
172             checkMarkDrawable.draw(canvas);
173         }
174     }
175 
176     @Override
onCreateDrawableState(int extraSpace)177     protected int[] onCreateDrawableState(int extraSpace) {
178         final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
179         if (isChecked()) {
180             mergeDrawableStates(drawableState, CHECKED_STATE_SET);
181         }
182         return drawableState;
183     }
184 
185     @Override
drawableStateChanged()186     protected void drawableStateChanged() {
187         super.drawableStateChanged();
188 
189         if (mCheckMarkDrawable != null) {
190             int[] myDrawableState = getDrawableState();
191 
192             // Set the state of the Drawable
193             mCheckMarkDrawable.setState(myDrawableState);
194 
195             invalidate();
196         }
197     }
198 
199     @Override
dispatchPopulateAccessibilityEvent(AccessibilityEvent event)200     public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
201         boolean populated = super.dispatchPopulateAccessibilityEvent(event);
202         if (!populated) {
203             event.setChecked(mChecked);
204         }
205         return populated;
206     }
207 }
208