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.util.AttributeSet; 31import android.widget.EditText; 32 33/** 34 * A {@link EditText} which supports compatible features on older version of the platform, 35 * including: 36 * <ul> 37 * <li>Supports {@link R.attr#textAllCaps} style attribute which works back to 38 * {@link android.os.Build.VERSION_CODES#GINGERBREAD Gingerbread}.</li> 39 * <li>Allows dynamic tint of its 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 android.widget.EditText} in your 46 * layouts. You should only need to manually use this class when writing custom views.</p> 47 */ 48public class AppCompatEditText extends EditText implements TintableBackgroundView { 49 50 private final AppCompatBackgroundHelper mBackgroundTintHelper; 51 private final AppCompatTextHelper mTextHelper; 52 53 public AppCompatEditText(Context context) { 54 this(context, null); 55 } 56 57 public AppCompatEditText(Context context, AttributeSet attrs) { 58 this(context, attrs, R.attr.editTextStyle); 59 } 60 61 public AppCompatEditText(Context context, AttributeSet attrs, int defStyleAttr) { 62 super(TintContextWrapper.wrap(context), attrs, defStyleAttr); 63 64 mBackgroundTintHelper = new AppCompatBackgroundHelper(this); 65 mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr); 66 67 mTextHelper = AppCompatTextHelper.create(this); 68 mTextHelper.loadFromAttributes(attrs, defStyleAttr); 69 mTextHelper.applyCompoundDrawablesTints(); 70 } 71 72 @Override 73 public void setBackgroundResource(@DrawableRes int resId) { 74 super.setBackgroundResource(resId); 75 if (mBackgroundTintHelper != null) { 76 mBackgroundTintHelper.onSetBackgroundResource(resId); 77 } 78 } 79 80 @Override 81 public void setBackgroundDrawable(Drawable background) { 82 super.setBackgroundDrawable(background); 83 if (mBackgroundTintHelper != null) { 84 mBackgroundTintHelper.onSetBackgroundDrawable(background); 85 } 86 } 87 88 /** 89 * This should be accessed via 90 * {@link android.support.v4.view.ViewCompat#setBackgroundTintList(android.view.View, ColorStateList)} 91 * 92 * @hide 93 */ 94 @RestrictTo(LIBRARY_GROUP) 95 @Override 96 public void setSupportBackgroundTintList(@Nullable ColorStateList tint) { 97 if (mBackgroundTintHelper != null) { 98 mBackgroundTintHelper.setSupportBackgroundTintList(tint); 99 } 100 } 101 102 /** 103 * This should be accessed via 104 * {@link android.support.v4.view.ViewCompat#getBackgroundTintList(android.view.View)} 105 * 106 * @hide 107 */ 108 @RestrictTo(LIBRARY_GROUP) 109 @Override 110 @Nullable 111 public ColorStateList getSupportBackgroundTintList() { 112 return mBackgroundTintHelper != null 113 ? mBackgroundTintHelper.getSupportBackgroundTintList() : null; 114 } 115 116 /** 117 * This should be accessed via 118 * {@link android.support.v4.view.ViewCompat#setBackgroundTintMode(android.view.View, PorterDuff.Mode)} 119 * 120 * @hide 121 */ 122 @RestrictTo(LIBRARY_GROUP) 123 @Override 124 public void setSupportBackgroundTintMode(@Nullable PorterDuff.Mode tintMode) { 125 if (mBackgroundTintHelper != null) { 126 mBackgroundTintHelper.setSupportBackgroundTintMode(tintMode); 127 } 128 } 129 130 /** 131 * This should be accessed via 132 * {@link android.support.v4.view.ViewCompat#getBackgroundTintMode(android.view.View)} 133 * 134 * @hide 135 */ 136 @RestrictTo(LIBRARY_GROUP) 137 @Override 138 @Nullable 139 public PorterDuff.Mode getSupportBackgroundTintMode() { 140 return mBackgroundTintHelper != null 141 ? mBackgroundTintHelper.getSupportBackgroundTintMode() : null; 142 } 143 144 @Override 145 protected void drawableStateChanged() { 146 super.drawableStateChanged(); 147 if (mBackgroundTintHelper != null) { 148 mBackgroundTintHelper.applySupportBackgroundTint(); 149 } 150 if (mTextHelper != null) { 151 mTextHelper.applyCompoundDrawablesTints(); 152 } 153 } 154 155 @Override 156 public void setTextAppearance(Context context, int resId) { 157 super.setTextAppearance(context, resId); 158 if (mTextHelper != null) { 159 mTextHelper.onSetTextAppearance(context, resId); 160 } 161 } 162} 163