18

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. How to do this?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
shiv
  • 235
  • 1
  • 3
  • 9

11 Answers11

23

Just came to show something that really help me with this:

Everyone talks about how to use the tint and how to use the colorAccent, but, this wont work on phones with API less than 21.

So, the real fix on this or at least what helped me was to use android.support.v7.widget.AppCompatRadioButton instead of RadioButton

With this on your layout, you can use: app:buttonTint="@color/yourColor"

without getting warnings or problems about the compat of the view.

And, don't you forget about adding:

xmlns:app="http://schemas.android.com/apk/res-auto"

to your layout parent or to your widget.

Edit:

@aselims mention on a comment that there's not buttonTintin the app namespace.

So... here's my current style for this solution:

<style name="MRadioButton.Purple" parent="Widget.AppCompat.CompoundButton.RadioButton">
    <item name="colorAccent">@color/youColor</item>
    <item name="colorControlHighlight">@color/yourColor</item>
    <item name="android:colorPressedHighlight">@color/yourColor</item>
    <item name="colorPrimaryDark">@color/yourColor</item>
    <item name="colorPrimary">@color/yourColor</item>
    <item name="colorControlActivated">@color/yourColor</item>
</style>
Henry
  • 17,490
  • 7
  • 63
  • 98
Sierisimo
  • 639
  • 8
  • 21
  • 1
    There is no buttonTint at the app namespace. – aselims Mar 04 '16 at 15:30
  • Maybe you're right, but that helped me. Also, I'm gonna edit my answer to add the current style for the RadioButton. But other people think like me: http://stackoverflow.com/a/33834545/1663453 – Sierisimo Mar 04 '16 at 17:37
  • 1
    As of today : app:buttonTint is now available ! Enjoy :) – Tobliug Dec 26 '16 at 16:00
17

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

mtb
  • 1,350
  • 16
  • 32
Jorge Arimany
  • 5,814
  • 2
  • 28
  • 23
  • Thanks smashing, forgot to test on lower API levels. – Jorge Arimany Apr 26 '15 at 10:04
  • 2
    use `@color/accentColor` in style – sujith s Sep 22 '15 at 04:43
  • @sujiths the colorAccent is part of the [Material design](http://www.google.es/design/spec/style/color.html#color-ui-color-application) that again requires api level >= 21, you can get backward compatibility using [V7 Support Libraries](https://developer.android.com/intl/zh-cn/training/material/compatibility.html) – Jorge Arimany Sep 22 '15 at 11:29
  • 1
    @JorgeArimany with support library colorAccent will suitable with api level <21 , and use the following for to manage normal color state `//black is the color of the normal state @android:color/white` – sujith s Sep 23 '15 at 05:54
  • @sujiths that's exactly what I've said: "you can get backward compatibility using V7 Support Libraries", just make sure you use V7 or above, the provided link is about mantaining compatibility while using Material design. thanks – Jorge Arimany Sep 23 '15 at 06:59
  • Does it work belof api 21 with AppCompatRadioButton? – Roel Apr 20 '16 at 11:47
8

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>
Weiyi
  • 1,843
  • 2
  • 22
  • 34
  • hello where do I put single_choice_state_list? – Woppi Feb 21 '17 at 11:17
  • @Woppi you can see in the code: `R.color.single_choice_state_list`, that means android will go to `res/color/` to look for resource file, refer to https://developer.android.com/guide/topics/resources/color-list-resource.html – Weiyi Feb 22 '17 at 05:12
  • 2
    If I use this, it warns me: `AppCompatRadioButton.setSupportButtonTintList can only be called from within the same library group (groupId=com.android.support)`, do you know why this is? – Damnum Apr 18 '17 at 13:28
  • 1
    @Damnum I didn't met this issue, hope this answer could help you: http://stackoverflow.com/questions/41150995/appcompatactivity-oncreate-can-only-be-called-from-within-the-same-library-group – Weiyi Apr 19 '17 at 01:39
  • 1
    @Damnum see my edit. AppCompatRadioButton#setSupportButtonTintList should be accessed from CompoundButtonCompat like so: `CompoundButtonCompat.setButtonTintList(radioButton, stateList)` – Jeremy Jun 23 '17 at 09:16
4

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!

fecub
  • 945
  • 10
  • 27
4

Create a selector drawable for you radio button under drawable/radio_button.xml folder and mention all the required states for your radio button.

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
 android:state_checked="true"
 android:state_window_focused="false"
 android:drawable="@drawable/radio_button_on" />
<item
 android:state_checked="false"
 android:state_window_focused="false"
 android:drawable="@drawable/radio_button_off" />
<item 
 android:state_checked="true" 
 android:state_pressed="true"
 android:drawable="@drawable/radio_button_on_pressed" />
<item
 android:state_checked="false" 
 android:state_pressed="true"
  android:drawable="@drawable/radio_button_off_pressed" />
<item
 android:state_checked="true"
  android:state_focused="true"
  android:drawable="@drawable/radio_button_on_selected" />
<item
  android:state_checked="false"
  android:state_focused="true"
  android:drawable="@drawable/radio_button_off_selected" />
<item
 android:state_checked="true"
 android:drawable="@drawable/radio_button_on" />
<item
 android:state_checked="false"
 android:drawable="@drawable/radio_button_off" />
</selector>

And specify android:button="@drawable/radio_button" for your radio button

Dont forget to add the corresponding images for different states of radio button.

user1764879
  • 112
  • 1
  • 9
4
//get radio button reference from layout
RadioButton raPrivate = (RadioButton) layout.findViewById(R.id.radioPrivate);
//parse textColor from string hex code
int textColor = Color.parseColor("#000000");
//set textcolor to radioButton
raPrivate.setButtonTintList(ColorStateList.valueOf(textColor));

u can only assing ColorStateList objets as color for the radioButton, if u use valueOf it will only use one color.

Hope this helps :>

BooNonMooN
  • 250
  • 3
  • 13
4

You can perform a backwards compatible tint on the radio button

XML:

<android.support.v7.widget.AppCompatRadioButton
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/radioButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  app:buttonTint="@color/red"/>

Or java:

CompoundButton button = (CompoundButton) findViewById(R.id.radioButton);
CompoundButtonCompat.setButtonTintList(button, ContextCompat.getColorStateList(this, R.color.red));
Simas
  • 43,548
  • 10
  • 88
  • 116
1

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.

Pablo
  • 261
  • 2
  • 4
1

add these two properties in your style this you are using in the manifest with the activity

<item name="colorControlNormal">@color/grey</item> // for state released color
<item name="colorAccent">@color/blueLogo</item>  //for state pressed color 
asadullah
  • 121
  • 1
  • 8
1

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

Tarif Chakder
  • 1,708
  • 1
  • 11
  • 10
0

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');
            }
        });
    }
Ansar
  • 364
  • 3
  • 16