1

I want to change the color of radio button on being tapped. I am using custom layout with on custom edit text and radio button in it.This is my xml for layout item.

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true">

    <com.ekcoffee.ekcoffeeapp.widgets.AppTextView
        android:id="@+id/textViewCountryCode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:paddingRight="@dimen/dimen_10dp"
        android:text="+91 (India)"
        android:textColor="@color/color_black_text"
        android:textSize="@dimen/dimen_16sp"
        app:textStyle="@integer/OPEN_SANS_REGULAR" />


</RelativeLayout>

<RadioButton
    android:id="@+id/selector"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:backgroundTint="@color/color_accent"
    android:checked="false"
    android:clickable="false"
    android:focusable="false"

    />

This is my xml for spinner

<android.support.v7.widget.AppCompatSpinner
    android:id="@+id/spinnerCountryCode"
    style="@style/spinner_style"
    android:layout_width="@dimen/dimen_1dp"
    android:layout_height="@dimen/dimen_1dp"
    android:layout_alignParentBottom="true"
    android:hint="+91"
    android:spinnerMode="dialog"
    android:textColor="@color/color_splash_screen_text"
    android:textColorHint="@color/color_white"
    android:textSize="@dimen/dimen_25dp" />

i have used style also for spinner

<style name="spinner_style" parent="Widget.AppCompat.Spinner.Underlined">
    <item name="android:background">@color/color_primary</item>
    <item name="android:layout_marginLeft">@dimen/dimen_48dp</item>
    <item name="android:layout_marginRight">@dimen/dimen_24dp</item>
    <item name="android:layout_marginBottom">10dp</item>
    <item name="android:checkMark">@drawable/ic_radio_checked</item>
    <item name="android:paddingLeft">8dp</item>
    <item name="android:layout_marginTop">100dp</item>
    <item name="android:paddingBottom">5dp</item>
</style>

3 Answers3

0

More simple, just set the buttonTint color: (only works on api level 21 or above)

<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:

enter image description here

Harshad
  • 1,344
  • 1
  • 10
  • 25
0
  int textColor = Color.parseColor(#000000);
    yourradiobutton.setButtonTintList(ColorStateList.valueOf(textColor));

put above code inside your button's onClick method

Parsania Hardik
  • 4,593
  • 1
  • 33
  • 33
0

In your styles.xml put -

  <!-- custom style -->
  <style name="radionbutton"
         parent="Base.Widget.AppCompat.CompoundButton.RadioButton">
    <item name="android:button">@drawable/radiobutton_drawable</item>
  </style>

Your radio button in xml should look like-

<RadioButton
    android:layout_width="wrap_content"
    style="@style/radionbutton"
    android:checked="false"
    android:layout_height="wrap_content"
    />

Now all you need to do is make a radiobutton_drawable.xml in your drawable folder. Here is what you need to put in it-

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/radio_unchecked" android:state_checked="false" android:state_focused="true"/>
  <item android:drawable="@drawable/radio_unchecked" android:state_checked="false" android:state_focused="false"/>
  <item android:drawable="@drawable/radio_checked" android:state_checked="true" android:state_focused="true"/>
  <item android:drawable="@drawable/radio_checked" android:state_checked="true" android:state_focused="false"/>
</selector>

Your radio_unchecked.xml-

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="oval">
  <stroke android:width="1dp" android:color="@color/colorAccent"/>
  <size android:width="30dp" android:height="30dp"/>
</shape>

Your radio_checked.xml-

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="oval">
      <stroke android:width="1dp" android:color="@color/colorAccent"/>
      <size android:width="30dp" android:height="30dp"/>
    </shape>
  </item>
  <item android:top="5dp" android:bottom="5dp" android:left="5dp" android:right="5dp">
    <shape android:shape="oval">
      <solid android:width="1dp" android:color="@color/colorAccent"/>
      <size android:width="10dp" android:height="10dp"/>
    </shape>
  </item>
</layer-list>

Just replace @color/colorAccent with the color of your choice.

Vaibhav Sharma
  • 2,293
  • 1
  • 17
  • 24
  • I just tried your code. Since spinner should close once you select any item from the list, so for that i have to set radio button with the following attributes android:clickable="false" ,android:focusable="false" .And if i set any of this true my spinner property for single choice is lost(allowing me to select multiple options) – Divya Jaisawal Feb 23 '16 at 13:25
  • You want only on selection from multiple radio buttons? Then you should put the radio buttons in a radio group. This allows only one selection. – Vaibhav Sharma Feb 23 '16 at 13:32
  • Can't put radio button in a radio group because i ' m using adapter to populate each radio button. – Divya Jaisawal Feb 23 '16 at 14:26
  • Then what you can do is maintain a boolean array of positions and by code cal setchecked of radio buttons for the positions. – Vaibhav Sharma Feb 23 '16 at 15:13