How to Group Radiobutton from Different Linearlayouts

Take Radio buttons in two linear layout in radio group not work

Try this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical" >

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:orientation="horizontal">

<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />

<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />

</RadioGroup>

<RadioGroup
android:id="@+id/radioGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />

<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />

</RadioGroup>

</LinearLayout>

Java code :

private OnCheckedChangeListener listener1 = new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId != -1) {
m_group2.setOnCheckedChangeListener(null); // remove the listener before clearing so we don't throw that stackoverflow exception(like Vladimir Volodin pointed out)
m_group2.clearCheck(); // clear the second RadioGroup!
m_group2.setOnCheckedChangeListener(listener2); //reset the listener
// Log.e("XXX2", "do the work");

int chkId1 = m_group1.getCheckedRadioButtonId();
int chkId2 = m_group2.getCheckedRadioButtonId();
int realCheck = chkId1 == -1 ? chkId2 : chkId1;

RadioButton rb = (RadioButton)findViewById(realCheck);
selected_mode = rb.getText().toString();
Log.i("Selected_Mode", "" + selected_mode);
}
}
};

private OnCheckedChangeListener listener2 = new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId != -1) {
m_group1.setOnCheckedChangeListener(null);
m_group1.clearCheck();
m_group1.setOnCheckedChangeListener(listener1);
//Log.e("XXX2", "do the work");

int chkId1 = m_group1.getCheckedRadioButtonId();
int chkId2 = m_group2.getCheckedRadioButtonId();
int realCheck = chkId1 == -1 ? chkId2 : chkId1;

RadioButton rb = (RadioButton)findViewById(realCheck);
selected_mode = rb.getText().toString();
Log.i("Selected_Mode", "" + selected_mode);
}
}
};

call the listener to OnCreate()

m_group1.setOnCheckedChangeListener(listener1);
m_group2.setOnCheckedChangeListener(listener2);

add radio buttons inside LinearLayout to a RadioGroup

You are not able to have multyply selection in RadioGroup , for that you can use checkboxes or you should not be using RadioGroup

Add RadioGroup in dynamic LinearLayout

  1. You are creating a new layout during every iteration in loop.
  2. By inflating a new view, you are losing the old views
  3. Every time you are using the same id R.id.radiobtn for to find a view and add the same view again in the layout which is not possible hence the issue.

for (int i=0;i<drawables;i++){
// 1,2
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//
final View rowView = inflater.inflate(R.layout.radiobutton, null);

parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount());
rowView.setId(i);
RadioButton rd = (RadioButton) rowView.findViewById(R.id.radiobtn);
l1.addView(rd); // same view with same id R.id.radiobtn
rd.setText(current.getAnswers().get(i).getAns());
}

Solution :

public void drawRAnswers(int pst){
int drawables = qmclist.get(pst).getAnswers().size();
// create a radio group as container with direction
RadioGroup l1 = new RadioGroup(this);
l1.setOrientation(LinearLayout.VERTICAL);
for (int i=0;i<drawables;i++){
// create radio button, with id and text
RadioButton rd = new RadioButton(this);
rd.setId(i);
rd.setText(current.getAnswers().get(i).getAns());
// add radio button into group
l1.addView(rd);
}
// finally, add radio group with buttons into parent layout
parentLinearLayout.addView(l1, parentLinearLayout.getChildCount());
}

android- radio button in different linear layout

You could set up a RadioGroup programmatically. Check out this question: setting up a RadioGroup programmatically

Inflating Nested Layout with RadioButton to a Container

The problem here is the RadioGroup it looks for a RadioButton child only, and because Im using a nested layout that contain the RadioButton the RadioGroup cannot find the RadioButton that is inflated programmatically.

How I solve this problem is with the
https://github.com/worker8/RadioGroupPlus this is a tweak RadioGroup that dig deep on nested layout and find the RadioButton inside.



Related Topics



Leave a reply



Submit