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