CheckedTextView.java revision 2fb40285c78db71f41774650e4da47dda191e787
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
17package android.widget;
18
19import com.android.internal.R;
20
21import android.content.Context;
22import android.content.res.TypedArray;
23import android.graphics.Canvas;
24import android.graphics.drawable.Drawable;
25import android.util.AttributeSet;
26import android.view.Gravity;
27import android.view.ViewDebug;
28import android.view.accessibility.AccessibilityEvent;
29import android.view.accessibility.AccessibilityNodeInfo;
30
31
32/**
33 * An extension to TextView that supports the {@link android.widget.Checkable} interface.
34 * This is useful when used in a {@link android.widget.ListView ListView} where the it's
35 * {@link android.widget.ListView#setChoiceMode(int) setChoiceMode} has been set to
36 * something other than {@link android.widget.ListView#CHOICE_MODE_NONE CHOICE_MODE_NONE}.
37 *
38 * @attr ref android.R.styleable#CheckedTextView_checked
39 * @attr ref android.R.styleable#CheckedTextView_checkMark
40 */
41public class CheckedTextView extends TextView implements Checkable {
42    private boolean mChecked;
43    private int mCheckMarkResource;
44    private Drawable mCheckMarkDrawable;
45    private int mBasePadding;
46    private int mCheckMarkWidth;
47    private boolean mNeedRequestlayout;
48
49    private static final int[] CHECKED_STATE_SET = {
50        R.attr.state_checked
51    };
52
53    public CheckedTextView(Context context) {
54        this(context, null);
55    }
56
57    public CheckedTextView(Context context, AttributeSet attrs) {
58        this(context, attrs, 0);
59    }
60
61    public CheckedTextView(Context context, AttributeSet attrs, int defStyle) {
62        super(context, attrs, defStyle);
63
64        TypedArray a = context.obtainStyledAttributes(attrs,
65                R.styleable.CheckedTextView, defStyle, 0);
66
67        Drawable d = a.getDrawable(R.styleable.CheckedTextView_checkMark);
68        if (d != null) {
69            setCheckMarkDrawable(d);
70        }
71
72        boolean checked = a.getBoolean(R.styleable.CheckedTextView_checked, false);
73        setChecked(checked);
74
75        a.recycle();
76    }
77
78    public void toggle() {
79        setChecked(!mChecked);
80    }
81
82    @ViewDebug.ExportedProperty
83    public boolean isChecked() {
84        return mChecked;
85    }
86
87    /**
88     * <p>Changes the checked state of this text view.</p>
89     *
90     * @param checked true to check the text, false to uncheck it
91     */
92    public void setChecked(boolean checked) {
93        if (mChecked != checked) {
94            mChecked = checked;
95            refreshDrawableState();
96        }
97    }
98
99
100    /**
101     * Set the checkmark to a given Drawable, identified by its resourece id. This will be drawn
102     * when {@link #isChecked()} is true.
103     *
104     * @param resid The Drawable to use for the checkmark.
105     *
106     * @see #setCheckMarkDrawable(Drawable)
107     * @see #getCheckMarkDrawable()
108     *
109     * @attr ref android.R.styleable#CheckedTextView_checkMark
110     */
111    public void setCheckMarkDrawable(int resid) {
112        if (resid != 0 && resid == mCheckMarkResource) {
113            return;
114        }
115
116        mCheckMarkResource = resid;
117
118        Drawable d = null;
119        if (mCheckMarkResource != 0) {
120            d = getResources().getDrawable(mCheckMarkResource);
121        }
122        setCheckMarkDrawable(d);
123    }
124
125    /**
126     * Set the checkmark to a given Drawable. This will be drawn when {@link #isChecked()} is true.
127     *
128     * @param d The Drawable to use for the checkmark.
129     *
130     * @see #setCheckMarkDrawable(int)
131     * @see #getCheckMarkDrawable()
132     *
133     * @attr ref android.R.styleable#CheckedTextView_checkMark
134     */
135    public void setCheckMarkDrawable(Drawable d) {
136        if (mCheckMarkDrawable != null) {
137            mCheckMarkDrawable.setCallback(null);
138            unscheduleDrawable(mCheckMarkDrawable);
139        }
140        mNeedRequestlayout = (d != mCheckMarkDrawable);
141        if (d != null) {
142            d.setCallback(this);
143            d.setVisible(getVisibility() == VISIBLE, false);
144            d.setState(CHECKED_STATE_SET);
145            setMinHeight(d.getIntrinsicHeight());
146
147            mCheckMarkWidth = d.getIntrinsicWidth();
148            d.setState(getDrawableState());
149        } else {
150            mCheckMarkWidth = 0;
151        }
152        mCheckMarkDrawable = d;
153        // Do padding resolution. This will call setPadding() and do a requestLayout() if needed.
154        resolvePadding();
155    }
156
157    /**
158     * Gets the checkmark drawable
159     *
160     * @return The drawable use to represent the checkmark, if any.
161     *
162     * @see #setCheckMarkDrawable(Drawable)
163     * @see #setCheckMarkDrawable(int)
164     *
165     * @attr ref android.R.styleable#CheckedTextView_checkMark
166     */
167    public Drawable getCheckMarkDrawable() {
168        return mCheckMarkDrawable;
169    }
170
171    @Override
172    public void onPaddingChanged(int layoutDirection) {
173        int newPadding = (mCheckMarkDrawable != null) ?
174                mCheckMarkWidth + mBasePadding : mBasePadding;
175        mNeedRequestlayout |= (mPaddingRight != newPadding);
176        mPaddingRight = newPadding;
177        if (mNeedRequestlayout) {
178            requestLayout();
179            mNeedRequestlayout = false;
180        }
181    }
182
183    @Override
184    public void setPadding(int left, int top, int right, int bottom) {
185        super.setPadding(left, top, right, bottom);
186        mBasePadding = mPaddingRight;
187    }
188
189    @Override
190    public void setPaddingRelative(int start, int top, int end, int bottom) {
191        super.setPaddingRelative(start, top, end, bottom);
192        mBasePadding = getPaddingEnd();
193    }
194
195    @Override
196    protected void onDraw(Canvas canvas) {
197        super.onDraw(canvas);
198
199        final Drawable checkMarkDrawable = mCheckMarkDrawable;
200        if (checkMarkDrawable != null) {
201            final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
202            final int height = checkMarkDrawable.getIntrinsicHeight();
203
204            int y = 0;
205
206            switch (verticalGravity) {
207                case Gravity.BOTTOM:
208                    y = getHeight() - height;
209                    break;
210                case Gravity.CENTER_VERTICAL:
211                    y = (getHeight() - height) / 2;
212                    break;
213            }
214
215            int right = getWidth();
216            checkMarkDrawable.setBounds(
217                    right - mPaddingRight,
218                    y,
219                    right - mPaddingRight + mCheckMarkWidth,
220                    y + height);
221            checkMarkDrawable.draw(canvas);
222        }
223    }
224
225    @Override
226    protected int[] onCreateDrawableState(int extraSpace) {
227        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
228        if (isChecked()) {
229            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
230        }
231        return drawableState;
232    }
233
234    @Override
235    protected void drawableStateChanged() {
236        super.drawableStateChanged();
237
238        if (mCheckMarkDrawable != null) {
239            int[] myDrawableState = getDrawableState();
240
241            // Set the state of the Drawable
242            mCheckMarkDrawable.setState(myDrawableState);
243
244            invalidate();
245        }
246    }
247
248    @Override
249    public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
250        super.onInitializeAccessibilityEvent(event);
251        event.setClassName(CheckedTextView.class.getName());
252        event.setChecked(mChecked);
253    }
254
255    @Override
256    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
257        super.onInitializeAccessibilityNodeInfo(info);
258        info.setClassName(CheckedTextView.class.getName());
259        info.setCheckable(true);
260        info.setChecked(mChecked);
261    }
262}
263