AppCompatAutoCompleteTextView.java revision 7e4e8b664820f773bc96e37ee1d2bbf500d64e69
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.AppCompatDrawableManager;
28import android.support.v7.internal.widget.TintContextWrapper;
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 AppCompatDrawableManager mDrawableManager;
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        mDrawableManager = AppCompatDrawableManager.get();
71
72        TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
73                TINT_ATTRS, defStyleAttr, 0);
74        if (a.hasValue(0)) {
75            setDropDownBackgroundDrawable(a.getDrawable(0));
76        }
77        a.recycle();
78
79        mBackgroundTintHelper = new AppCompatBackgroundHelper(this, mDrawableManager);
80        mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr);
81
82        mTextHelper = AppCompatTextHelper.create(this);
83        mTextHelper.loadFromAttributes(attrs, defStyleAttr);
84        mTextHelper.applyCompoundDrawablesTints();
85    }
86
87    @Override
88    public void setDropDownBackgroundResource(@DrawableRes int resId) {
89        if (mDrawableManager != null) {
90            setDropDownBackgroundDrawable(mDrawableManager.getDrawable(getContext(), resId));
91        } else {
92            super.setDropDownBackgroundResource(resId);
93        }
94    }
95
96    @Override
97    public void setBackgroundResource(@DrawableRes int resId) {
98        super.setBackgroundResource(resId);
99        if (mBackgroundTintHelper != null) {
100            mBackgroundTintHelper.onSetBackgroundResource(resId);
101        }
102    }
103
104    @Override
105    public void setBackgroundDrawable(Drawable background) {
106        super.setBackgroundDrawable(background);
107        if (mBackgroundTintHelper != null) {
108            mBackgroundTintHelper.onSetBackgroundDrawable(background);
109        }
110    }
111
112    /**
113     * This should be accessed via
114     * {@link android.support.v4.view.ViewCompat#setBackgroundTintList(android.view.View, ColorStateList)}
115     *
116     * @hide
117     */
118    @Override
119    public void setSupportBackgroundTintList(@Nullable ColorStateList tint) {
120        if (mBackgroundTintHelper != null) {
121            mBackgroundTintHelper.setSupportBackgroundTintList(tint);
122        }
123    }
124
125    /**
126     * This should be accessed via
127     * {@link android.support.v4.view.ViewCompat#getBackgroundTintList(android.view.View)}
128     *
129     * @hide
130     */
131    @Override
132    @Nullable
133    public ColorStateList getSupportBackgroundTintList() {
134        return mBackgroundTintHelper != null
135                ? mBackgroundTintHelper.getSupportBackgroundTintList() : null;
136    }
137
138    /**
139     * This should be accessed via
140     * {@link android.support.v4.view.ViewCompat#setBackgroundTintMode(android.view.View, PorterDuff.Mode)}
141     *
142     * @hide
143     */
144    @Override
145    public void setSupportBackgroundTintMode(@Nullable PorterDuff.Mode tintMode) {
146        if (mBackgroundTintHelper != null) {
147            mBackgroundTintHelper.setSupportBackgroundTintMode(tintMode);
148        }
149    }
150
151    /**
152     * This should be accessed via
153     * {@link android.support.v4.view.ViewCompat#getBackgroundTintMode(android.view.View)}
154     *
155     * @hide
156     */
157    @Override
158    @Nullable
159    public PorterDuff.Mode getSupportBackgroundTintMode() {
160        return mBackgroundTintHelper != null
161                ? mBackgroundTintHelper.getSupportBackgroundTintMode() : null;
162    }
163
164    @Override
165    protected void drawableStateChanged() {
166        super.drawableStateChanged();
167        if (mBackgroundTintHelper != null) {
168            mBackgroundTintHelper.applySupportBackgroundTint();
169        }
170        if (mTextHelper != null) {
171            mTextHelper.applyCompoundDrawablesTints();
172        }
173    }
174
175    @Override
176    public void setTextAppearance(Context context, int resId) {
177        super.setTextAppearance(context, resId);
178        if (mTextHelper != null) {
179            mTextHelper.onSetTextAppearance(context, resId);
180        }
181    }
182}
183