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