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