Search code examples
androidradio-buttonbackground-color

How to reset Backgroud color of a radio button?


I am just trying to reset background color of radio button. Already i have set color for radio button and its text along with background perfectly.Later to reset text color of radio button i have used ColorStateList , which worked perfectly for text-color only but did not work for background resetting. Can anyone please suggest me how to reset background color of radio button? is there any other method like **COlorStateList ** to reset background ?

Thanks in advance.

also i have attached my code below:

package com.hfad.rdiobuttontest;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
RadioButton radioButton1,radioButton2,radioButton3;
RadioGroup radioGroup;
TextView question;
Button button, button2;

private ColorStateList defaulttextcolor;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    radioButton1=findViewById(R.id.radio_button1);
    radioButton2=findViewById(R.id.radio_button2);
    radioButton3=findViewById(R.id.radio_button3);
    radioGroup=findViewById(R.id.radio_group);
    question=findViewById(R.id.question);
    button=findViewById(R.id.test);
    button2=findViewById(R.id.reset);

    defaulttextcolor=radioButton1.getTextColors();

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            radiobuttoncolor ();

        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            radioButton1.setTextColor(defaulttextcolor);
            radioButton2.setTextColor(defaulttextcolor);
            radioButton3.setTextColor(defaulttextcolor);


            radioGroup.clearCheck();

            question.setText("result- background still same");
            radioButton1.setText("my backgroud color did not           changed,/n can you please help me to cahnge it?");
            radioButton2.setText("textcolr has been reset");
            radioButton3.setText("opps, 1 - ur backgroud is still blue");

        }
      });

   }

   public void radiobuttoncolor (){
   radioButton1.setTextColor(defaulttextcolor);
   radioButton2.setTextColor(defaulttextcolor);
   radioButton3.setTextColor(defaulttextcolor);


   radioGroup.clearCheck();

   question.setText("please click on color reset button");
   radioButton1.setText("My backgroud color is blue");
   radioButton1.setTextColor(Color.GREEN);
   radioButton1.setBackgroundColor(Color.BLUE);
   radioButton2.setText("I am green");
   radioButton2.setTextColor(Color.GREEN);
   radioButton3.setText("1) backgroud blue, \n2) text green \n3)i am default ");

   }

   }

and not the layout

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:padding="16dp"
tools:context="com.hfad.rdiobuttontest.MainActivity">


<TextView
    android:id="@+id/question"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/radio_group"
    android:layout_marginBottom="16dp"
    android:freezesText="true"
    android:text="please click on color test button"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="20sp" />

<RadioGroup
    android:id="@+id/radio_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    >

    <RadioButton
        android:id="@+id/radio_button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:freezesText="true"
        android:text="Option 1"
        android:padding="6dp"
        android:layout_marginTop="10dp"
        />

    <RadioButton
        android:id="@+id/radio_button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:checked="false"
        android:freezesText="true"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layoutDirection="ltr"
        android:padding="6dp"
        android:text="Option 2"
        android:textColor="@color/colorAccent"

        />

    <RadioButton
        android:id="@+id/radio_button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:freezesText="true"
        android:text="Option 3"
        android:padding="6dp"/>


</RadioGroup>

<Button
    android:id="@+id/test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/radio_group"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="91dp"
    android:freezesText="true"
    android:text="colortest" />

<Button
    android:id="@+id/reset"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/radio_group"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="140dp"
    android:freezesText="true"
    android:text="Color reset" />


Solution

  • There are two ways in which you can achieve this,

    To reset the color you can either use the transparent color or use the default color of the Radio Button. So inside your click listener try this -

    radioButton1.setBackgroundColor(android.R.drawable.btn_radio); 
    

    or alternatively,

    radioButton1.setBackgroundColor(0x00000000);