Hello I want to create a radiogroup 2x2 so I achieved to do this using two radiogroup. But the problem is right now I can select two radiobutton instead of only one.. How can I try to do this ?
I precise I use kotlin to develop.
Thank you !
Hello I want to create a radiogroup 2x2 so I achieved to do this using two radiogroup. But the problem is right now I can select two radiobutton instead of only one.. How can I try to do this ?
I precise I use kotlin to develop.
Thank you !
here you have a very similar question gridlayout 3x3 Because you can't do want you want trivialy, you should to acept 1 of the answers of the question above. What i did:
public class GRadioGroup {
List<RadioButton> radios = new ArrayList<RadioButton>();
/**
* Constructor, which allows you to pass number of RadioButton instances,
* making a group.
*
* @param radios
* One RadioButton or more.
*/
public GRadioGroup(RadioButton... radios) {
super();
for (RadioButton rb : radios) {
this.radios.add(rb);
rb.setOnClickListener(onClick);
}
}
/**
* Constructor, which allows you to pass number of RadioButtons
* represented by resource IDs, making a group.
*
* @param activity
* Current View (or Activity) to which those RadioButtons
* belong.
* @param radiosIDs
* One RadioButton or more.
*/
public GRadioGroup(View activity, int... radiosIDs) {
super();
for (int radioButtonID : radiosIDs) {
RadioButton rb = (RadioButton)activity.findViewById(radioButtonID);
if (rb != null) {
this.radios.add(rb);
rb.setOnClickListener(onClick);
}
}
}
/**
* This occurs everytime when one of RadioButtons is clicked,
* and deselects all others in the group.
*/
public void addRadioButtonsToGroup(RadioButton rb){
radios.add(rb);
rb.setOnClickListener(onClick);
}
OnClickListener onClick = new OnClickListener() {
@Override
public void onClick(View v) {
// let's deselect all radios in group
for (RadioButton rb : radios) {
ViewParent p = rb.getParent();
if (p.getClass().equals(RadioGroup.class)) {
// if RadioButton belongs to RadioGroup,
// then deselect all radios in it
RadioGroup rg = (RadioGroup) p;
rg.clearCheck();
} else {
// if RadioButton DOES NOT belong to RadioGroup,
// just deselect it
rb.setChecked(false);
}
}
// now let's select currently clicked RadioButton
if (!v.getClass().equals(RadioButton.class)) {
RadioButton rb = (RadioButton) v;
rb.setChecked(true);
}
}
};
/**
*
** Returns the Id of the radio button that is checked or -1 if none are checked
*
* @return
*/
public int getCheckedRadioButtonId() {
int checkedId = -1;
// Loop each radio button
for (RadioButton rb : radios) {
if (rb.isChecked())
return rb.getId();
}
return checkedId;
}
public void setCheckedRadioButton(int pos) {
// let's deselect all radios in group
for (RadioButton rb : radios) {
ViewParent p = rb.getParent();
if (p.getClass().equals(RadioGroup.class)) {
// if RadioButton belongs to RadioGroup,
// then deselect all radios in it
RadioGroup rg = (RadioGroup) p;
rg.clearCheck();
} else {
// if RadioButton DOES NOT belong to RadioGroup,
// just deselect it
rb.setChecked(false);
}
}
radios.get(pos).setChecked(true);
}
public void setCheckedRadioButtonDefault() {
radios.get(0).setChecked(true);
}
@Override
public void finalize() {
radios.clear();
}
}
i need add radiobuttons programtically so make my radiobutton.xml:
<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layoutDirection="rtl"
android:paddingStart="@dimen/padding_right_rests_columns"
android:paddingEnd="@dimen/padding_left_rest_column"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:gravity="center"
android:layout_gravity="center"
android:layout_margin="5dp">
</RadioButton>
And for adding (this is in a fragment):
GRadioGroup gr = new GRadioGroup();
RadioButton radioButton = (RadioButton) getActivity().getLayoutInflater().inflate(R.layout.radiobutton, null);//initialize and set content
radioButton.setText("HEY")
// And all the settings you want like position ...
//finally
gr.addRadioButtonsToGroup(radioButton);
Disadvantages? you have to handle the selected radiogroup with bundle or intents if the activity/fragment/whatever was destroyed.
Hope help you!