AppCompatAutoCompleteTextView.java revision 091b0f935e68ce9bfecc2422e60eada33fa3b09c
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.content.res.ColorStateList; 21import android.graphics.PorterDuff; 22import android.graphics.drawable.Drawable; 23import android.support.annotation.DrawableRes; 24import android.support.annotation.Nullable; 25import android.support.v4.view.TintableBackgroundView; 26import android.support.v7.appcompat.R; 27import android.support.v7.internal.widget.TintContextWrapper; 28import android.support.v7.internal.widget.TintManager; 29import android.support.v7.internal.widget.TintTypedArray; 30import android.util.AttributeSet; 31import android.widget.AutoCompleteTextView; 32 33/** 34 * A {@link AutoCompleteTextView} which supports compatible features on older version of the 35 * platform, including: 36 * <ul> 37 * <li>Supports {@link R.attr#textAllCaps} style attribute which works back to 38 * {@link android.os.Build.VERSION_CODES#ECLAIR_MR1 Eclair MR1}.</li> 39 * <li>Allows dynamic tint of it background via the background tint methods in 40 * {@link android.support.v4.view.ViewCompat}.</li> 41 * <li>Allows setting of the background tint using {@link R.attr#backgroundTint} and 42 * {@link R.attr#backgroundTintMode}.</li> 43 * </ul> 44 * 45 * <p>This will automatically be used when you use {@link AutoCompleteTextView} in your layouts. 46 * You should only need to manually use this class when writing custom views.</p> 47 */ 48public class AppCompatAutoCompleteTextView extends AutoCompleteTextView implements 49 TintableBackgroundView { 50 51 private static final int[] TINT_ATTRS = { 52 android.R.attr.popupBackground 53 }; 54 55 private TintManager mTintManager; 56 private AppCompatBackgroundHelper mBackgroundTintHelper; 57 private AppCompatTextHelper mTextHelper; 58 59 public AppCompatAutoCompleteTextView(Context context) { 60 this(context, null); 61 } 62 63 public AppCompatAutoCompleteTextView(Context context, AttributeSet attrs) { 64 this(context, attrs, R.attr.autoCompleteTextViewStyle); 65 } 66 67 public AppCompatAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) { 68 super(TintContextWrapper.wrap(context), attrs, defStyleAttr); 69 70 TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs, 71 TINT_ATTRS, defStyleAttr, 0); 72 mTintManager = a.getTintManager(); 73 if (a.hasValue(0)) { 74 setDropDownBackgroundDrawable(a.getDrawable(0)); 75 } 76 a.recycle(); 77 78 mBackgroundTintHelper = new AppCompatBackgroundHelper(this, mTintManager); 79 mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr); 80 81 mTextHelper = new AppCompatTextHelper(this); 82 mTextHelper.loadFromAttributes(attrs, defStyleAttr); 83 } 84 85 @Override 86 public void setDropDownBackgroundResource(@DrawableRes int resId) { 87 if (mTintManager != null) { 88 setDropDownBackgroundDrawable(mTintManager.getDrawable(resId)); 89 } else { 90 super.setDropDownBackgroundResource(resId); 91 } 92 } 93 94 @Override 95 public void setBackgroundResource(@DrawableRes int resId) { 96 super.setBackgroundResource(resId); 97 if (mBackgroundTintHelper != null) { 98 mBackgroundTintHelper.onSetBackgroundResource(resId); 99 } 100 } 101 102 @Override 103 public void setBackgroundDrawable(Drawable background) { 104 super.setBackgroundDrawable(background); 105 if (mBackgroundTintHelper != null) { 106 mBackgroundTintHelper.onSetBackgroundDrawable(background); 107 } 108 } 109 110 /** 111 * This should be accessed via 112 * {@link android.support.v4.view.ViewCompat#setBackgroundTintList(android.view.View, ColorStateList)} 113 * 114 * @hide 115 */ 116 @Override 117 public void setSupportBackgroundTintList(@Nullable ColorStateList tint) { 118 if (mBackgroundTintHelper != null) { 119 mBackgroundTintHelper.setSupportBackgroundTintList(tint); 120 } 121 } 122 123 /** 124 * This should be accessed via 125 * {@link android.support.v4.view.ViewCompat#getBackgroundTintList(android.view.View)} 126 * 127 * @hide 128 */ 129 @Override 130 @Nullable 131 public ColorStateList getSupportBackgroundTintList() { 132 return mBackgroundTintHelper != null 133 ? mBackgroundTintHelper.getSupportBackgroundTintList() : null; 134 } 135 136 /** 137 * This should be accessed via 138 * {@link android.support.v4.view.ViewCompat#setBackgroundTintMode(android.view.View, PorterDuff.Mode)} 139 * 140 * @hide 141 */ 142 @Override 143 public void setSupportBackgroundTintMode(@Nullable PorterDuff.Mode tintMode) { 144 if (mBackgroundTintHelper != null) { 145 mBackgroundTintHelper.setSupportBackgroundTintMode(tintMode); 146 } 147 } 148 149 /** 150 * This should be accessed via 151 * {@link android.support.v4.view.ViewCompat#getBackgroundTintMode(android.view.View)} 152 * 153 * @hide 154 */ 155 @Override 156 @Nullable 157 public PorterDuff.Mode getSupportBackgroundTintMode() { 158 return mBackgroundTintHelper != null 159 ? mBackgroundTintHelper.getSupportBackgroundTintMode() : null; 160 } 161 162 @Override 163 protected void drawableStateChanged() { 164 super.drawableStateChanged(); 165 if (mBackgroundTintHelper != null) { 166 mBackgroundTintHelper.applySupportBackgroundTint(); 167 } 168 } 169 170 @Override 171 public void setTextAppearance(Context context, int resId) { 172 super.setTextAppearance(context, resId); 173 if (mTextHelper != null) { 174 mTextHelper.onSetTextAppearance(context, resId); 175 } 176 } 177} 178