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.util.AttributeSet; 28import android.view.accessibility.AccessibilityEvent; 29import android.view.accessibility.AccessibilityNodeInfo; 30import android.widget.Button; 31 32/** 33 * A {@link Button} which supports compatible features on older version of the platform, 34 * including: 35 * <ul> 36 * <li>Supports {@link R.attr#textAllCaps} style attribute which works back to 37 * {@link android.os.Build.VERSION_CODES#ECLAIR_MR1 Eclair MR1}.</li> 38 * <li>Allows dynamic tint of it background via the background tint methods in 39 * {@link android.support.v4.view.ViewCompat}.</li> 40 * <li>Allows setting of the background tint using {@link R.attr#backgroundTint} and 41 * {@link R.attr#backgroundTintMode}.</li> 42 * </ul> 43 * 44 * <p>This will automatically be used when you use {@link android.widget.Button} in your 45 * layouts. You should only need to manually use this class when writing custom views.</p> 46 */ 47public class AppCompatButton extends Button implements TintableBackgroundView { 48 49 private final AppCompatDrawableManager mDrawableManager; 50 private final AppCompatBackgroundHelper mBackgroundTintHelper; 51 private final AppCompatTextHelper mTextHelper; 52 53 public AppCompatButton(Context context) { 54 this(context, null); 55 } 56 57 public AppCompatButton(Context context, AttributeSet attrs) { 58 this(context, attrs, R.attr.buttonStyle); 59 } 60 61 public AppCompatButton(Context context, AttributeSet attrs, int defStyleAttr) { 62 super(TintContextWrapper.wrap(context), attrs, defStyleAttr); 63 64 mDrawableManager = AppCompatDrawableManager.get(); 65 mBackgroundTintHelper = new AppCompatBackgroundHelper(this, mDrawableManager); 66 mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr); 67 68 mTextHelper = AppCompatTextHelper.create(this); 69 mTextHelper.loadFromAttributes(attrs, defStyleAttr); 70 mTextHelper.applyCompoundDrawablesTints(); 71 } 72 73 @Override 74 public void setBackgroundResource(@DrawableRes int resId) { 75 super.setBackgroundResource(resId); 76 if (mBackgroundTintHelper != null) { 77 mBackgroundTintHelper.onSetBackgroundResource(resId); 78 } 79 } 80 81 @Override 82 public void setBackgroundDrawable(Drawable background) { 83 super.setBackgroundDrawable(background); 84 if (mBackgroundTintHelper != null) { 85 mBackgroundTintHelper.onSetBackgroundDrawable(background); 86 } 87 } 88 89 /** 90 * This should be accessed via 91 * {@link android.support.v4.view.ViewCompat#setBackgroundTintList(android.view.View, ColorStateList)} 92 * 93 * @hide 94 */ 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 @Override 109 @Nullable 110 public ColorStateList getSupportBackgroundTintList() { 111 return mBackgroundTintHelper != null 112 ? mBackgroundTintHelper.getSupportBackgroundTintList() : null; 113 } 114 115 /** 116 * This should be accessed via 117 * {@link android.support.v4.view.ViewCompat#setBackgroundTintMode(android.view.View, PorterDuff.Mode)} 118 * 119 * @hide 120 */ 121 @Override 122 public void setSupportBackgroundTintMode(@Nullable PorterDuff.Mode tintMode) { 123 if (mBackgroundTintHelper != null) { 124 mBackgroundTintHelper.setSupportBackgroundTintMode(tintMode); 125 } 126 } 127 128 /** 129 * This should be accessed via 130 * {@link android.support.v4.view.ViewCompat#getBackgroundTintMode(android.view.View)} 131 * 132 * @hide 133 */ 134 @Override 135 @Nullable 136 public PorterDuff.Mode getSupportBackgroundTintMode() { 137 return mBackgroundTintHelper != null 138 ? mBackgroundTintHelper.getSupportBackgroundTintMode() : null; 139 } 140 141 @Override 142 protected void drawableStateChanged() { 143 super.drawableStateChanged(); 144 if (mBackgroundTintHelper != null) { 145 mBackgroundTintHelper.applySupportBackgroundTint(); 146 } 147 if (mTextHelper != null) { 148 mTextHelper.applyCompoundDrawablesTints(); 149 } 150 } 151 152 @Override 153 public void setTextAppearance(Context context, int resId) { 154 super.setTextAppearance(context, resId); 155 if (mTextHelper != null) { 156 mTextHelper.onSetTextAppearance(context, resId); 157 } 158 } 159 160 @Override 161 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 162 super.onInitializeAccessibilityEvent(event); 163 event.setClassName(Button.class.getName()); 164 } 165 166 @Override 167 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 168 super.onInitializeAccessibilityNodeInfo(info); 169 info.setClassName(Button.class.getName()); 170 } 171 172 /** 173 * Sets the properties of this field to transform input to ALL CAPS 174 * display. This may use a "small caps" formatting if available. 175 * This setting will be ignored if this field is editable or selectable. 176 * 177 * This call replaces the current transformation method. Disabling this 178 * will not necessarily restore the previous behavior from before this 179 * was enabled. 180 */ 181 public void setSupportAllCaps(boolean allCaps) { 182 if (mTextHelper != null) { 183 mTextHelper.setAllCaps(allCaps); 184 } 185 } 186} 187