AppCompatTextView.java revision 2cccf609662389d6a23dbc0711d5fb2e826e8c63
1/* 2 * Copyright (C) 2015 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.content.res.TypedArray; 21import android.support.v7.appcompat.R; 22import android.support.v7.internal.text.AllCapsTransformationMethod; 23import android.util.AttributeSet; 24import android.widget.TextView; 25 26/** 27 * A {@link android.widget.TextView} which supports compatible features on older version of the 28 * platform. 29 * <p> 30 * This will automatically be used when you use {@link android.widget.TextView} in your 31 * layouts. You should only need to manually use this class when writing custom views. 32 */ 33public class AppCompatTextView extends TextView { 34 35 public AppCompatTextView(Context context) { 36 this(context, null); 37 } 38 39 public AppCompatTextView(Context context, AttributeSet attrs) { 40 this(context, attrs, 0); 41 } 42 43 public AppCompatTextView(Context context, AttributeSet attrs, int defStyle) { 44 super(context, attrs, defStyle); 45 46 // First read the TextAppearance style id 47 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppCompatTextView, 48 defStyle, 0); 49 final int ap = a.getResourceId(R.styleable.AppCompatTextView_android_textAppearance, -1); 50 a.recycle(); 51 52 // Now check TextAppearance's textAllCaps value 53 if (ap != -1) { 54 TypedArray appearance = context.obtainStyledAttributes(ap, R.styleable.TextAppearance); 55 if (appearance.hasValue(R.styleable.TextAppearance_textAllCaps)) { 56 setAllCaps(appearance.getBoolean(R.styleable.TextAppearance_textAllCaps, false)); 57 } 58 appearance.recycle(); 59 } 60 61 // Now read the style's value 62 a = context.obtainStyledAttributes(attrs, R.styleable.AppCompatTextView, defStyle, 0); 63 if (a.hasValue(R.styleable.AppCompatTextView_textAllCaps)) { 64 setAllCaps(a.getBoolean(R.styleable.AppCompatTextView_textAllCaps, false)); 65 } 66 a.recycle(); 67 } 68 69 public void setAllCaps(boolean allCaps) { 70 setTransformationMethod(allCaps ? new AllCapsTransformationMethod(getContext()) : null); 71 } 72 73 @Override 74 public void setTextAppearance(Context context, int resId) { 75 super.setTextAppearance(context, resId); 76 77 TypedArray appearance = context.obtainStyledAttributes(resId, R.styleable.TextAppearance); 78 if (appearance.hasValue(R.styleable.TextAppearance_textAllCaps)) { 79 setAllCaps(appearance.getBoolean(R.styleable.TextAppearance_textAllCaps, false)); 80 } 81 appearance.recycle(); 82 } 83} 84