2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android
.support
.v7
.widget
;
19 import android
.content
.Context
;
20 import android
.content
.res
.ColorStateList
;
21 import android
.graphics
.PorterDuff
;
22 import android
.graphics
.drawable
.Drawable
;
23 import android
.support
.annotation
.DrawableRes
;
24 import android
.support
.annotation
.Nullable
;
25 import android
.support
.annotation
.RestrictTo
;
26 import android
.support
.v4
.view
.TintableBackgroundView
;
27 import android
.support
.v7
.appcompat
.R
;
28 import android
.support
.v7
.content
.res
.AppCompatResources
;
29 import android
.util
.AttributeSet
;
30 import android
.widget
.MultiAutoCompleteTextView
;
32 import static android
.support
.annotation
.RestrictTo
.Scope
.GROUP_ID
;
35 * A {@link MultiAutoCompleteTextView} which supports compatible features on older version of the
36 * platform, including:
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 it 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>
46 * <p>This will automatically be used when you use {@link MultiAutoCompleteTextView} in your layouts.
47 * You should only need to manually use this class when writing custom views.</p>
49 public class AppCompatMultiAutoCompleteTextView
extends MultiAutoCompleteTextView
50 implements TintableBackgroundView
{
52 private static final int[] TINT_ATTRS
= {
53 android
.R
.attr
.popupBackground
56 private AppCompatBackgroundHelper mBackgroundTintHelper
;
57 private AppCompatTextHelper mTextHelper
;
59 public AppCompatMultiAutoCompleteTextView(Context context
) {
63 public AppCompatMultiAutoCompleteTextView(Context context
, AttributeSet attrs
) {
64 this(context
, attrs
, R
.attr
.autoCompleteTextViewStyle
);
67 public AppCompatMultiAutoCompleteTextView(Context context
, AttributeSet attrs
, int defStyleAttr
) {
68 super(TintContextWrapper
.wrap(context
), attrs
, defStyleAttr
);
70 TintTypedArray a
= TintTypedArray
.obtainStyledAttributes(getContext(), attrs
,
71 TINT_ATTRS
, defStyleAttr
, 0);
73 setDropDownBackgroundDrawable(a
.getDrawable(0));
77 mBackgroundTintHelper
= new AppCompatBackgroundHelper(this);
78 mBackgroundTintHelper
.loadFromAttributes(attrs
, defStyleAttr
);
80 mTextHelper
= AppCompatTextHelper
.create(this);
81 mTextHelper
.loadFromAttributes(attrs
, defStyleAttr
);
82 mTextHelper
.applyCompoundDrawablesTints();
86 public void setDropDownBackgroundResource(@DrawableRes int resId
) {
87 setDropDownBackgroundDrawable(AppCompatResources
.getDrawable(getContext(), resId
));
91 public void setBackgroundResource(@DrawableRes int resId
) {
92 super.setBackgroundResource(resId
);
93 if (mBackgroundTintHelper
!= null) {
94 mBackgroundTintHelper
.onSetBackgroundResource(resId
);
99 public void setBackgroundDrawable(Drawable background
) {
100 super.setBackgroundDrawable(background
);
101 if (mBackgroundTintHelper
!= null) {
102 mBackgroundTintHelper
.onSetBackgroundDrawable(background
);
107 * This should be accessed via
108 * {@link android.support.v4.view.ViewCompat#setBackgroundTintList(android.view.View, ColorStateList)}
112 @RestrictTo(GROUP_ID
)
114 public void setSupportBackgroundTintList(@Nullable ColorStateList tint
) {
115 if (mBackgroundTintHelper
!= null) {
116 mBackgroundTintHelper
.setSupportBackgroundTintList(tint
);
121 * This should be accessed via
122 * {@link android.support.v4.view.ViewCompat#getBackgroundTintList(android.view.View)}
126 @RestrictTo(GROUP_ID
)
129 public ColorStateList
getSupportBackgroundTintList() {
130 return mBackgroundTintHelper
!= null
131 ? mBackgroundTintHelper
.getSupportBackgroundTintList() : null;
135 * This should be accessed via
136 * {@link android.support.v4.view.ViewCompat#setBackgroundTintMode(android.view.View, PorterDuff.Mode)}
140 @RestrictTo(GROUP_ID
)
142 public void setSupportBackgroundTintMode(@Nullable PorterDuff
.Mode tintMode
) {
143 if (mBackgroundTintHelper
!= null) {
144 mBackgroundTintHelper
.setSupportBackgroundTintMode(tintMode
);
149 * This should be accessed via
150 * {@link android.support.v4.view.ViewCompat#getBackgroundTintMode(android.view.View)}
154 @RestrictTo(GROUP_ID
)
157 public PorterDuff
.Mode
getSupportBackgroundTintMode() {
158 return mBackgroundTintHelper
!= null
159 ? mBackgroundTintHelper
.getSupportBackgroundTintMode() : null;
163 protected void drawableStateChanged() {
164 super.drawableStateChanged();
165 if (mBackgroundTintHelper
!= null) {
166 mBackgroundTintHelper
.applySupportBackgroundTint();
168 if (mTextHelper
!= null) {
169 mTextHelper
.applyCompoundDrawablesTints();
174 public void setTextAppearance(Context context
, int resId
) {
175 super.setTextAppearance(context
, resId
);
176 if (mTextHelper
!= null) {
177 mTextHelper
.onSetTextAppearance(context
, resId
);