blob: 43c08431cbf155997f9c0c5f460569e3a26d30bd [file] [log] [blame]
Fabrice Di Megliobd9152f2013-10-01 11:21:31 -07001/*
2 * Copyright (C) 2014 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 android.content.Context;
20import android.content.res.Resources;
21import android.content.res.TypedArray;
22import android.graphics.Canvas;
23import android.graphics.Paint;
24import android.graphics.Typeface;
25import android.util.AttributeSet;
26
27import com.android.internal.R;
28
29class TextViewWithCircularIndicator extends TextView {
30
31 private static final int SELECTED_CIRCLE_ALPHA = 60;
32
33 private final Paint mCirclePaint = new Paint();
34
35 private final String mItemIsSelectedText;
36 private int mCircleColor;
37 private boolean mDrawIndicator;
38
39 public TextViewWithCircularIndicator(Context context) {
40 this(context, null);
41 }
42
43 public TextViewWithCircularIndicator(Context context, AttributeSet attrs) {
44 this(context, attrs, 0);
45 }
46
47 public TextViewWithCircularIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
48 this(context, attrs, defStyleAttr, 0);
49 }
50
51 public TextViewWithCircularIndicator(Context context, AttributeSet attrs,
52 int defStyleAttr, int defStyleRes) {
Fabrice Di Megliobd9152f2013-10-01 11:21:31 -070053 super(context, attrs);
Alan Viverette60727e02014-07-28 16:56:32 -070054
Fabrice Di Megliobd9152f2013-10-01 11:21:31 -070055
56 // Use Theme attributes if possible
57 final TypedArray a = mContext.obtainStyledAttributes(attrs,
58 R.styleable.DatePicker, defStyleAttr, defStyleRes);
Alan Viverette60727e02014-07-28 16:56:32 -070059 final int resId = a.getResourceId(R.styleable.DatePicker_yearListItemTextAppearance, -1);
Fabrice Di Megliobd9152f2013-10-01 11:21:31 -070060 if (resId != -1) {
61 setTextAppearance(context, resId);
62 }
63
Alan Viverette60727e02014-07-28 16:56:32 -070064 final Resources res = context.getResources();
Fabrice Di Megliobd9152f2013-10-01 11:21:31 -070065 mItemIsSelectedText = res.getString(R.string.item_is_selected);
66
67 a.recycle();
68
69 init();
70 }
71
72 private void init() {
73 mCirclePaint.setTypeface(Typeface.create(mCirclePaint.getTypeface(), Typeface.BOLD));
74 mCirclePaint.setAntiAlias(true);
75 mCirclePaint.setTextAlign(Paint.Align.CENTER);
76 mCirclePaint.setStyle(Paint.Style.FILL);
77 }
78
79 public void setCircleColor(int color) {
80 if (color != mCircleColor) {
81 mCircleColor = color;
82 mCirclePaint.setColor(mCircleColor);
83 mCirclePaint.setAlpha(SELECTED_CIRCLE_ALPHA);
84 requestLayout();
85 }
86 }
87
88 public void setDrawIndicator(boolean drawIndicator) {
89 mDrawIndicator = drawIndicator;
90 }
91
92 @Override
93 public void onDraw(Canvas canvas) {
94 super.onDraw(canvas);
95 if (mDrawIndicator) {
96 final int width = getWidth();
97 final int height = getHeight();
98 int radius = Math.min(width, height) / 2;
99 canvas.drawCircle(width / 2, height / 2, radius, mCirclePaint);
100 }
101 }
102
103 @Override
104 public CharSequence getContentDescription() {
105 CharSequence itemText = getText();
106 if (mDrawIndicator) {
107 return String.format(mItemIsSelectedText, itemText);
108 } else {
109 return itemText;
110 }
111 }
112}