Home | History | Annotate | Download | only in widget
      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.RadioButton;
     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 RadioButton} 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 RadioButton} 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 AppCompatRadioButton extends RadioButton implements TintableCompoundButton {
     51 
     52     private final AppCompatCompoundButtonHelper mCompoundButtonHelper;
     53     private final AppCompatTextHelper mTextHelper;
     54 
     55     public AppCompatRadioButton(Context context) {
     56         this(context, null);
     57     }
     58 
     59     public AppCompatRadioButton(Context context, AttributeSet attrs) {
     60         this(context, attrs, R.attr.radioButtonStyle);
     61     }
     62 
     63     public AppCompatRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
     64         super(TintContextWrapper.wrap(context), attrs, defStyleAttr);
     65         mCompoundButtonHelper = new AppCompatCompoundButtonHelper(this);
     66         mCompoundButtonHelper.loadFromAttributes(attrs, defStyleAttr);
     67         mTextHelper = new AppCompatTextHelper(this);
     68         mTextHelper.loadFromAttributes(attrs, defStyleAttr);
     69     }
     70 
     71     @Override
     72     public void setButtonDrawable(Drawable buttonDrawable) {
     73         super.setButtonDrawable(buttonDrawable);
     74         if (mCompoundButtonHelper != null) {
     75             mCompoundButtonHelper.onSetButtonDrawable();
     76         }
     77     }
     78 
     79     @Override
     80     public void setButtonDrawable(@DrawableRes int resId) {
     81         setButtonDrawable(AppCompatResources.getDrawable(getContext(), resId));
     82     }
     83 
     84     @Override
     85     public int getCompoundPaddingLeft() {
     86         final int value = super.getCompoundPaddingLeft();
     87         return mCompoundButtonHelper != null
     88                 ? mCompoundButtonHelper.getCompoundPaddingLeft(value)
     89                 : value;
     90     }
     91 
     92     /**
     93      * This should be accessed from {@link androidx.core.widget.CompoundButtonCompat}
     94      * @hide
     95      */
     96     @RestrictTo(LIBRARY_GROUP)
     97     @Override
     98     public void setSupportButtonTintList(@Nullable ColorStateList tint) {
     99         if (mCompoundButtonHelper != null) {
    100             mCompoundButtonHelper.setSupportButtonTintList(tint);
    101         }
    102     }
    103 
    104     /**
    105      * This should be accessed from {@link androidx.core.widget.CompoundButtonCompat}
    106      * @hide
    107      */
    108     @RestrictTo(LIBRARY_GROUP)
    109     @Nullable
    110     @Override
    111     public ColorStateList getSupportButtonTintList() {
    112         return mCompoundButtonHelper != null
    113                 ? mCompoundButtonHelper.getSupportButtonTintList()
    114                 : null;
    115     }
    116 
    117     /**
    118      * This should be accessed from {@link androidx.core.widget.CompoundButtonCompat}
    119      * @hide
    120      */
    121     @RestrictTo(LIBRARY_GROUP)
    122     @Override
    123     public void setSupportButtonTintMode(@Nullable PorterDuff.Mode tintMode) {
    124         if (mCompoundButtonHelper != null) {
    125             mCompoundButtonHelper.setSupportButtonTintMode(tintMode);
    126         }
    127     }
    128 
    129     /**
    130      * This should be accessed from {@link androidx.core.widget.CompoundButtonCompat}
    131      * @hide
    132      */
    133     @RestrictTo(LIBRARY_GROUP)
    134     @Nullable
    135     @Override
    136     public PorterDuff.Mode getSupportButtonTintMode() {
    137         return mCompoundButtonHelper != null
    138                 ? mCompoundButtonHelper.getSupportButtonTintMode()
    139                 : null;
    140     }
    141 }
    142