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