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