1 /* 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 17 package android.support.v7.widget; 18 19 import android.content.Context; 20 import android.support.annotation.DrawableRes; 21 import android.support.v7.internal.widget.TintManager; 22 import android.support.v7.internal.widget.TintTypedArray; 23 import android.util.AttributeSet; 24 import android.widget.CheckedTextView; 25 26 /** 27 * A {@link CheckedTextView} which supports compatible features on older version of the platform. 28 * 29 * <p>This will automatically be used when you use {@link CheckedTextView} in your layouts. 30 * You should only need to manually use this class when writing custom views.</p> 31 */ 32 public class AppCompatCheckedTextView extends CheckedTextView { 33 34 private static final int[] TINT_ATTRS = { 35 android.R.attr.checkMark 36 }; 37 38 private TintManager mTintManager; 39 AppCompatCheckedTextView(Context context)40 public AppCompatCheckedTextView(Context context) { 41 this(context, null); 42 } 43 AppCompatCheckedTextView(Context context, AttributeSet attrs)44 public AppCompatCheckedTextView(Context context, AttributeSet attrs) { 45 this(context, attrs, android.R.attr.checkedTextViewStyle); 46 } 47 AppCompatCheckedTextView(Context context, AttributeSet attrs, int defStyleAttr)48 public AppCompatCheckedTextView(Context context, AttributeSet attrs, int defStyleAttr) { 49 super(context, attrs, defStyleAttr); 50 51 if (TintManager.SHOULD_BE_USED) { 52 TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs, 53 TINT_ATTRS, defStyleAttr, 0); 54 setCheckMarkDrawable(a.getDrawable(0)); 55 a.recycle(); 56 57 mTintManager = a.getTintManager(); 58 } 59 } 60 61 @Override setCheckMarkDrawable(@rawableRes int resId)62 public void setCheckMarkDrawable(@DrawableRes int resId) { 63 if (mTintManager != null) { 64 setCheckMarkDrawable(mTintManager.getDrawable(resId)); 65 } else { 66 super.setCheckMarkDrawable(resId); 67 } 68 } 69 70 } 71