I am trying to prepare custom radiogroup like layout in the below image. I have nearly 8-10 rows to do that. So, I prepared one linear layout
w
I had the same layout as you but the number of rows in my case was limited by four. And I found out that the easiest way to make radioButtons work together is to use the simple method that will loop through array of radioButtons and check ALL of them OFF and then check ON the one which is needed.
And that's all folks (c)
You don't need to use regular RadioGroup neither creating custom radio group class, you don't need to setOnCheckChangedListeners.
final RadioButton radio1 = (RadioButton) findViewById(R.id.radio_1);
final RadioButton radio2 = (RadioButton) findViewById(R.id.radio_2);
final RadioButton radio3 = (RadioButton) findViewById(R.id.radio_3);
final RadioButton radio4 = (RadioButton) findViewById(R.id.radio_4);
final RadioButton[] radios = {radio1, radio2, radio3, radio4};
View.OnClickListener buttonListener = new View.OnClickListener(){
@Override
public void onClick(View v) {
for(RadioButton radio : radios){
radio.setChecked(false);
}
switch (v.getId()){
case R.id.button_1:
radio1.setChecked(true);
break;
case R.id.button_2:
radio2.setChecked(true);
break;
case R.id.button_3:
radio3.setChecked(true);
break;
case R.id.button_4:
radio4.setChecked(true);
break;
}
}
};
RelativeLayout button1 = (RelativeLayout) findViewById(R.id.button_1);
button1.setOnClickListener(buttonListener);
RelativeLayout button2 = (RelativeLayout) findViewById(R.id.button_2);
button2.setOnClickListener(buttonListener);
RelativeLayout button3 = (RelativeLayout) findViewById(R.id.button_3);
button3.setOnClickListener(buttonListener);
RelativeLayout button4 = (RelativeLayout) findViewById(R.id.button_4);
button4.setOnClickListener(buttonListener);
*Hint: Don't forget to set
android:text=""
android:checked="false"
android:clickable="false"
to your layout to each RadioButton. And set initial state programmatically for that radioButton which should be checked.