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 17 package android.support.v7.widget; 18 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.v4.content.ContextCompat; 26 import android.support.v4.widget.TintableCompoundButton; 27 import android.support.v7.appcompat.R; 28 import android.support.v7.internal.widget.TintManager; 29 import android.util.AttributeSet; 30 import android.widget.CheckBox; 31 32 /** 33 * A {@link CheckBox} which supports compatible features on older version of the platform, 34 * including: 35 * <ul> 36 * <li>Allows dynamic tint of it background via the background tint methods in 37 * {@link android.support.v4.widget.CompoundButtonCompat}.</li> 38 * <li>Allows setting of the background tint using {@link R.attr#buttonTint} and 39 * {@link R.attr#buttonTintMode}.</li> 40 * </ul> 41 * 42 * <p>This will automatically be used when you use {@link CheckBox} in your layouts. 43 * You should only need to manually use this class when writing custom views.</p> 44 */ 45 public class AppCompatCheckBox extends CheckBox implements TintableCompoundButton { 46 47 private TintManager mTintManager; 48 private AppCompatCompoundButtonHelper mCompoundButtonHelper; 49 AppCompatCheckBox(Context context)50 public AppCompatCheckBox(Context context) { 51 this(context, null); 52 } 53 AppCompatCheckBox(Context context, AttributeSet attrs)54 public AppCompatCheckBox(Context context, AttributeSet attrs) { 55 this(context, attrs, R.attr.checkboxStyle); 56 } 57 AppCompatCheckBox(Context context, AttributeSet attrs, int defStyleAttr)58 public AppCompatCheckBox(Context context, AttributeSet attrs, int defStyleAttr) { 59 super(context, attrs, defStyleAttr); 60 mTintManager = TintManager.get(context); 61 mCompoundButtonHelper = new AppCompatCompoundButtonHelper(this, mTintManager); 62 mCompoundButtonHelper.loadFromAttributes(attrs, defStyleAttr); 63 } 64 65 @Override setButtonDrawable(Drawable buttonDrawable)66 public void setButtonDrawable(Drawable buttonDrawable) { 67 super.setButtonDrawable(buttonDrawable); 68 if (mCompoundButtonHelper != null) { 69 mCompoundButtonHelper.onSetButtonDrawable(); 70 } 71 } 72 73 @Override setButtonDrawable(@rawableRes int resId)74 public void setButtonDrawable(@DrawableRes int resId) { 75 setButtonDrawable(mTintManager != null 76 ? mTintManager.getDrawable(resId) 77 : ContextCompat.getDrawable(getContext(), resId)); 78 } 79 80 @Override getCompoundPaddingLeft()81 public int getCompoundPaddingLeft() { 82 final int value = super.getCompoundPaddingLeft(); 83 return mCompoundButtonHelper != null 84 ? mCompoundButtonHelper.getCompoundPaddingLeft(value) 85 : value; 86 } 87 88 /** 89 * This should be accessed from {@link android.support.v4.widget.CompoundButtonCompat} 90 * @hide 91 */ 92 @Override setSupportButtonTintList(@ullable ColorStateList tint)93 public void setSupportButtonTintList(@Nullable ColorStateList tint) { 94 if (mCompoundButtonHelper != null) { 95 mCompoundButtonHelper.setSupportButtonTintList(tint); 96 } 97 } 98 99 /** 100 * This should be accessed from {@link android.support.v4.widget.CompoundButtonCompat} 101 * @hide 102 */ 103 @Nullable 104 @Override getSupportButtonTintList()105 public ColorStateList getSupportButtonTintList() { 106 return mCompoundButtonHelper != null 107 ? mCompoundButtonHelper.getSupportButtonTintList() 108 : null; 109 } 110 111 /** 112 * This should be accessed from {@link android.support.v4.widget.CompoundButtonCompat} 113 * @hide 114 */ 115 @Override setSupportButtonTintMode(@ullable PorterDuff.Mode tintMode)116 public void setSupportButtonTintMode(@Nullable PorterDuff.Mode tintMode) { 117 if (mCompoundButtonHelper != null) { 118 mCompoundButtonHelper.setSupportButtonTintMode(tintMode); 119 } 120 } 121 122 /** 123 * This should be accessed from {@link android.support.v4.widget.CompoundButtonCompat} 124 * @hide 125 */ 126 @Nullable 127 @Override getSupportButtonTintMode()128 public PorterDuff.Mode getSupportButtonTintMode() { 129 return mCompoundButtonHelper != null 130 ? mCompoundButtonHelper.getSupportButtonTintMode() 131 : null; 132 } 133 } 134