Change color of a radio button

前端 未结 11 1839
死守一世寂寞
死守一世寂寞 2020-12-10 02:11

I am developing an quiz based app. There will be 1 question and 4 option(radio buttons). If user select any wrong answer then I want to turn that radio button color to Red.

相关标签:
11条回答
  • 2020-12-10 02:36

    Create an image !like this and place it in your drawable folders.. call it by,

     RadioButton rb=(RadioButton) findViewById(R.id.radioButton1);
            rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub
                    rb.setButtonDrawable(R.drawable.'you image here');
                }
            });
        }
    
    0 讨论(0)
  • 2020-12-10 02:37

    To change RadioButton button colour programmatically, and works on api level < 21, should use AppCompatRadioButton instead of RadioButton:

    (otherwise will warn setbuttontintlist requrie api level 21)

    import android.support.v7.widget.AppCompatRadioButton;
    
    AppCompatRadioButton radioButton = new AppCompatRadioButton(getActivity());
    radioButton.setSupportButtonTintList(
            ContextCompat.getColorStateList(getActivity(),
            R.color.single_choice_state_list));
    

    single_choice_state_list.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:color="@color/single_choice_checked"></item>
        <item android:state_checked="false" android:color="@color/single_choice_unchecked"></item>
    </selector>
    
    0 讨论(0)
  • 2020-12-10 02:38

    This site is really good for customizing Android components in general: android-holo-colors

    Just choose the radio button, make the color red, download and use it in your project!

    0 讨论(0)
  • 2020-12-10 02:38

    Hope this helps..

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
           radioButton.setButtonTintList(ContextCompat.getColorStateList(mContext, R.color.colorGris));
        }else {//Do something if you have a lower version}
    

    For me its working.

    0 讨论(0)
  • 2020-12-10 02:42

    For kotlin user

    Create an extension

    fun RadioButton.setCircleColor() {
        val colorStateList = ColorStateList(
            arrayOf(
                intArrayOf(-android.R.attr.state_checked), // unchecked
                intArrayOf(android.R.attr.state_checked)  // checked
            ), intArrayOf(
               Color.RED, // unchecked color
               Color.GREEN  // checked color
            )
        )
    
        // finally set button tint list
        buttonTintList = colorStateList
    
        // optionally tint mode or tint blend
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
            buttonTintBlendMode = BlendMode.SRC_IN
        }else{
            buttonTintMode = PorterDuff.Mode.SRC_IN
        }
    
        invalidate() // could not be necessary
    }
    

    Now call it

       radioButton.setCircleColor()
    

    done

    0 讨论(0)
  • 2020-12-10 02:43

    The fastest thing to do is to set the buttonTint to your desired color:

     <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radio"
            android:checked="true"
            android:buttonTint="@color/your_color"/>
    

    In your values/colors.xml put your color in this case a reddish one:

    <color name="your_color">#e75748</color>
    

    Result:

    Colored Android Radio Button

    As @smashing pointed, this only will work on API level >= 21

    0 讨论(0)
提交回复
热议问题