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
which has horizontal orientation and added the imageview
, textview
and radiobutton
programatically.
So if I check on one radio button
, the other radio buttons should automatically unchecked. Before going to that task itself, I got another problem that if my radio buttons are checked once, then the radio button are not uncheckable though clicked on them. Below is my code.
public class MainActivity extends Activity{
RadioButton[] radioBtns = new RadioButton[10];
String texts[] = {"text1", "text2", .... "text10"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
entireLayout = (LinearLayout)findViewById(R.id.main_layout);
for(int i =0; i<10; i++)
{
LinearLayout rowLayout=new LinearLayout(this);
radioBtns[i] = new RadioButton(this);
radioBtns[i].setId(i);
radioBtns[i].setOnCheckedChangeListener(cblistener);
ImageView imageView = new ImageView(this);
TextView tv = new TextView(this);
tv.setText(texts[i]);
rowLayout.addView(imageView);
rowLayout.addView(tv);
rowLayout.addView(radioBtns[i]);
entireLayout.addView(rowLayout);
View line = new View(this);
line.setBackgroundColor(getResources().getColor(R.color.horizontallinecolor));
entireLayout.addView(line, new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 1));
}
}
CompoundButton.OnCheckedChangeListener cblistener = new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton checkedbutton, boolean paramAnonymousBoolean)
{
switch (checkedbutton.getId())
{
case 0:
Log.d("tag", "checked 0th position");
break;
..........................
..........................
}
}
};
}
I have observed through keeping logs. The control enters onCheckedChanged() for the first time when those are getting checked, but not when unchecked. I wonder how those are uncheckable.
OR According to my requirement I have one more idea to prepare this layout like I prepare one more layout for the row in xml. Then inflating the view 10 times. But then also, how can I check only one radio button so that other selected one gets unchecked. Can someone please suggest me how to achieve this kind of radiogroup through the best way?
Note: I have kept my above code because to show what I have tried. If it is completely wrong way, please go easy and please suggest me how to get that done.
It's not difficult to implement your custom RadioGroup class to fit your requirements. Also you can implement a custom RadioButton. Having custom components defined makes it easier to extend and bypass restrictions of default components.
I have described the process of creating a custom RadioGroup and a custom RadioButton in this tutorial. Also I have uploaded my code to Github.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With