RadioButton text color change when clicked on android

I want to change the text color of the Radio button if the user clicked. However, I can change the background color, but I don’t want it ... and can someone even tell me how to put a cross or a right sign according to the right or wrong choice ...? Here is my code for the main class ..

final RadioButton Red = (RadioButton)findViewById(R.id.radioButton1); //RadioButton Blue = (RadioButton)findViewById(R.id.radioButton2); RadioButton Green = (RadioButton)findViewById(R.id.radioButton3); Red.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { Red.setBackgroundColor(Color.RED); } } }); Green.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub } }); 

here is my layout file:

 <RadioGroup android:id="@+id/radiogroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton" /> </RadioGroup> 
+6
source share
2 answers

To change the text color of a radio object:

 Red.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { Red.setTextColor(Color.RED); } } }); 

For custom icons; create radiobutton.xml in your folder with this:

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

And put these lines in your styles.xml (in the values folder):

 <?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomTheme" parent="android:Theme"> <item name="android:radioButtonStyle">@style/RadioButton</item> </style> <style name="RadioButton" parent="@android:style/Widget.CompoundButton.RadioButton"> <item name="android:button">@drawable/radiobutton</item> </style> </resources> 
+6
source
  RadioGroup question1RadioGroup = (RadioGroup) findViewById(R.id.radiogroup1); question1RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup view, @IdRes int checkedId) { RadioButton Red = (RadioButton) findViewById(R.id.radioButton1); RadioButton Green = (RadioButton) findViewById(R.id.radioButton3); if (checkedId == R.id.radioButton1) { Red.setTextColor(getResources().getColor(R.color.colorRed)); Red.setBackgroundResource(R.drawable.wrong_option); } else if (checkedId == R.id.radioButton3) { Green.setTextColor(getResources().getColor(R.color.colorGreen)); Green.setBackgroundResource(R.drawable.correct_option); } } }); 
0
source

All Articles