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