How to change only checked RadioButton text color in android

How to set radio button color after selection of radio button selected item.

In this tutorial we are creating three radio buttons and changing radio button text color after user selects it. This method are called as dynamically modifying color of radio button at run time. So here is the complete step by step tutorial for How to change only checked RadioButton text color in android.

android-project-download-code-button

How to change only checked RadioButton text color in android.

Code for MainActivity.java file.

 package com.changeonlycheckedradiobuttoncolor_android_examples.com;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends Activity {

 RadioGroup radiogroup;
 RadioButton radiobutton1,radiobutton2,radiobutton3,radiobutton4;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 radiogroup = (RadioGroup)findViewById(R.id.radioGroup);
 radiobutton1 = (RadioButton)findViewById(R.id.radioButton1);
 radiobutton2 = (RadioButton)findViewById(R.id.radioButton2);
 radiobutton3 = (RadioButton)findViewById(R.id.radioButton3);
 radiobutton4 = (RadioButton)findViewById(R.id.radioButton4);
 
 
 radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
 
 @Override
 public void onCheckedChanged(RadioGroup group, int checkedId) {
 // TODO Auto-generated method stub
 
 if(radiobutton1.isChecked())
 { 
 // Changing radio button 1 color on checked.
 radiobutton1.setTextColor(Color.parseColor("#fe9c02"));
 
 //Changing other remaining radio button color to black.
 radiobutton2.setTextColor(Color.BLACK);
 radiobutton3.setTextColor(Color.BLACK);
 radiobutton4.setTextColor(Color.BLACK);
 }
 
 if(radiobutton2.isChecked())
 { 
 // Changing radio button 2 color on checked.
 radiobutton2.setTextColor(Color.parseColor("#fe9c02"));
 
 //Changing other remaining radio button color to black.
 radiobutton3.setTextColor(Color.BLACK);
 radiobutton4.setTextColor(Color.BLACK);
 radiobutton1.setTextColor(Color.BLACK);
 }
 
 if(radiobutton3.isChecked())
 { 
 // Changing radio button 3 color on checked.
 radiobutton3.setTextColor(Color.parseColor("#fe9c02"));
 
 //Changing other remaining radio button color to black.
 radiobutton1.setTextColor(Color.BLACK);
 radiobutton2.setTextColor(Color.BLACK);
 radiobutton4.setTextColor(Color.BLACK);
 }
 
 if(radiobutton4.isChecked())
 { 
 // Changing radio button 4 color on checked.
 radiobutton4.setTextColor(Color.parseColor("#fe9c02"));
 
 //Changing other remaining radio button color to black.
 radiobutton1.setTextColor(Color.BLACK);
 radiobutton2.setTextColor(Color.BLACK);
 radiobutton3.setTextColor(Color.BLACK);
 }
 
 }
 });
 
 }

}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.changeonlycheckedradiobuttoncolor_android_examples.com.MainActivity" >

 <RadioGroup
 android:id="@+id/radioGroup"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 >
 
 

 <RadioButton
 android:id="@+id/radioButton1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Android" />

 <RadioButton
 android:id="@+id/radioButton2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/radioButton1"
 android:layout_below="@+id/radioButton1"
 android:text="PHP" />

 <RadioButton
 android:id="@+id/radioButton3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/radioButton2"
 android:layout_below="@+id/radioButton2"
 android:text="Blogger" />
 
 <RadioButton
 android:id="@+id/radioButton4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignLeft="@+id/radioButton3"
 android:layout_below="@+id/radioButton3"
 android:text="HTML" />
 
 </RadioGroup>

</RelativeLayout>

Screenshots:

change only checked RadioButton text color in android

selected-radio-button

Click here to download How to change only checked RadioButton text color in android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *