Hi i created dynamic radiogroup with radiobutton.Now i want to get values from dynamic radiogroup. Below is my code
final RadioGroup rg = new RadioGroup(this);
rg.setId(questionNo);
RadioButton[] rb = new RadioButton[answers.length()];
for (int i = 0; i < answers.length(); i++) {
JSONObject answerObject = answers.getJSONObject(i);
rb[i] = new RadioButton(this);
rb[i].setText(answerObject.getString("AnswerValue"));
rb[i].setId(answerObject.getInt("AnswerId"));
rg.addView(rb[i]);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
int selectedId = rg.getCheckedRadioButtonId();
Log.i("ID", String.valueOf(selectedId));
}
});
Button click event
submit_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(rg.getCheckedRadioButtonId()!=-1){
int id= rg.getCheckedRadioButtonId();
View radioButton = rg.findViewById(id);
int radioId = rg.indexOfChild(radioButton);
RadioButton btn = (RadioButton) rg.getChildAt(radioId);
String selection = (String) btn.getText();
Log.i("selection", selection);
}
}
});
I am getting only last index of the radio group.
I know it's too late, But this may help someone else
While creating your Dynamic Radiobutton Group, Create a parent layout(Linear layout or Relative Layout)
All radio groups will be the child of that Layout and all radio button will be a child of the specific radio group.
Creating No of Dynamic Radio Group
Create all Dynamic radio group in a parent layout
Add all radio button to a group
Add all radio group to the parent layout
public RadioButton[] rb;
public RadioGroup grp;
public LinearLayout layoutmain; //Parent layout
for(int i=0;i<qus.size();i++) //No of Questions
{
rb = new RadioButton[size];
grp = new RadioGroup(this);
grp.setId(a+qus.get(i).qusetionId);
for(int j=0;j<qus.get(i).choices.size();j++) //No of Radio button in radio group
{
rb[j] = new RadioButton(this);
rb[j].setText(qus.get(i).choices.get(j).getChoiceText());
grp.addView(rb[j]); // adding button to group
}
layoutmain.addView(grp); // adding group to layout
}
Retrieve All selected button task
From parent, layout get all child
Find all radio group using the child Instance
Create an object for that radio group
using that object get the selected radio button
Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for(int i=0;i<layoutmain.getChildCount();i++) //From the parent layout (Linear Layout) get the child
{
View child = layoutmain.getChildAt(i);
if(child instanceof RadioGroup) //Check weather its RadioGroup using its INSTANCE
{
RadioGroup rg = (RadioGroup) child; //create a RadioButton for each group
int selectedId = rg.getCheckedRadioButtonId(); // get the selected button
RadioButton radiochecked = (RadioButton) findViewById(selectedId);
Log.e("Radio",radiochecked.getText().toString()); //from the button get the selected text
}
}
} });
Related
We are working on a project where there is a questionnaire with dynamically created radio groups with radio buttons.At the bottom of the listview we have placed a button and we need the selected values from the radio buttons on clicking the button.The issue is how to get the selected Radio button values.Also i wanted to know in which class i can get the values in the main activity or in the Base Adapter class?
We have done the following:
The main activity:
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(m=0;m<question_list_array.size();m++)
{
View view= question_list.getChildAt(m);
RadioGroup radioGroup= (RadioGroup) view.findViewById(R.id.single_radiogroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for(int n=0;n<group.getChildCount();n++)
{
RadioButton rb= (RadioButton) group.getChildAt(n);
if (rb.getId() == checkedId) {
String result= rb.getText()+"";
Toast.makeText(MainActivity.this,result, Toast.LENGTH_LONG).show();
// do something with text
return;
}
}
}
});
}
}
});
But i am getting NPE at the following line:
RadioGroup radioGroup= (RadioGroup) view.findViewById(R.id.single_radiogroup);
The adapter class:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String question_type = question_list_array.get(position).getQuestion_type();
option_list = question_list_array.get(position).option_list;
number_of_options = option_list.size();
v_single=convertView;
if (v_single == null) {
v_single = inflater.inflate(R.layout.single_choice_layout, null);
sh = new SingleHolder();
sh.single_question_name = (TextView) v_single.findViewById(R.id.single_question_name);
sh.single_radiogroup = (RadioGroup) v_single.findViewById(R.id.single_radiogroup);
v_single.setTag(sh);
}
else
{
sh = (SingleHolder) v_single.getTag();
}
sh.single_radiogroup.clearCheck();
sh.single_radiogroup.removeAllViews();
sh.single_question_name.setText(question_list_array.get(position).getQuestion_name());
for (int j = 0; j < number_of_options; j++)
{
RadioButton rb = new RadioButton(context);
rb.setText(option_list.get(j).getOption_name());
sh.single_radiogroup.addView(rb);
}
v_final=v_single;
return v_final;
}
We dont have any issues with the Adapter as such.Please reply at the earliest.
I have a custom radio group dialog and i am inflating it dynamically.
In my onCreate() I am doing this
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
alarmType1 = (TextView) findViewById(R.id.tvAlarmType);
View view = LayoutInflater.from(this).inflate(R.layout.custom_layout, null, false);
radioAlarmtype = (RadioGroup) view.findViewById(R.id.radiogroup);
initRadioDialog();
then initializing the dialog with the following code
String string_Alarmtype[]={"Melody","Vibration","Melody and Vibration"};
public void initRadioDialog()
{
Context context=AlarmActivity.this;
dialog_radio=new Dialog(context);
dialog_radio.requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = LayoutInflater.from(context).inflate(R.layout.custom_layout, null, false);
radioAlarmtype = (RadioGroup) view.findViewById(R.id.radiogroup);
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
// add 3 radio buttons to the group
RadioButton rb;
for (int i = 0; i < 3; i++){
rb = new RadioButton(context);
rb.setText(string_Alarmtype[i]);
rb.setId(i);
radioAlarmtype.addView(rb, layoutParams);
}
radioAlarmtype.check(2);
radioAlarmtype.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
RadioButton rb = (RadioButton) findViewById(checkedId);
//alarmType1.setText(rb.getText());
Toast.makeText(AlarmActivity.this, checkedId + "", Toast.LENGTH_SHORT).show();
}
});
dialog_radio.setContentView(view);
}
but above commented line //alarmType1.setText(rb.getText());is causing a nullpointerException
so i tried it on my textview onClickListener
like this
View.OnClickListener alarmTypeListener = new View.OnClickListener() {
public void onClick(View v) {
dialog_radio.show();
RadioButton rb = (RadioButton) findViewById(radioAlarmtype.getCheckedRadioButtonId());
alarmType1.setText(rb.getText());//NUllpointerException here
Toast.makeText(AlarmActivity.this,"HEllo"+rb.getText(),Toast.LENGTH_SHORT).show();
//here also nullpointerException if I comment above line
}
};
But the problem still persist so how can i change the textview as change in the selection of radio button in my dialog.
I am generating the radio buttons dynamically from server returned json data in android.
I can display the radio buttons in radio group. Also can get id of each radio button.
But when I click on the next radio buttons all are stayed as selected. None of them are unchecked.
As the radio numbers are generated dynamically so it sizes will vary.
My code snippets :
if(type.equals("radio_buttons")){
String optionName = regData.getOption();
optionName = Character.toString(optionName.charAt(0)).toUpperCase()+optionName.substring(1);
List listItem = new ArrayList();
listItem.add(optionName);
final RadioGroup rg = new RadioGroup(getActivity()); //create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);
final int radioSize = listItem.size();
final RadioButton[] rb = new RadioButton[radioSize];
for(int i=0; i<radioSize; i++){
rb[i] = new RadioButton(getActivity());
rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
rb[i].setId(i);
rb[i].setText(optionName);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for(int j = 0; j<radioSize; j++){
rg.removeViewAt(checkedId);
}
/*
switch (checkedId)
{
case 1:
Toast.makeText(OnsiteRegistrationFragment.this.getActivity(), "VIEW ID " + checkedId, Toast.LENGTH_LONG).show();
case 2:
Toast.makeText(OnsiteRegistrationFragment.this.getActivity(), "VIEW ID " + checkedId, Toast.LENGTH_LONG).show();
}*/
}
});
this.linearLayout.addView(rg);
}
How can i clear/uncheck the other radio buttons when one radio button is selected in android?
LinkedList<RadioButton> radiobuttons
At the time of creating button add radiobuttons in radiobuttons in your for loop.
for (int i = 0; i < radioSize.size(); i++) {
RadioButton rb;
rb = radiobuttons.get(i);
rg.removeView(rb);
}
In my app i have created list view of checkbox in linear layout and i have created dynamic radio group of three radio button.when user click on any checkbox than radio group will be created at the end. but i want to create this radio group just below the clicked checked box. i am trying to set cursor position but not able to do.i don't have any idea what to do for this.
this is part of main.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewGroup checkboxContainer = (ViewGroup) findViewById(R.id.checkbox_container);
for (int i = 0; i < 25; i++) {
final CheckBox checkBox = new CheckBox(this);
checkBox.setText(Oils_and_Condiments[i]);
checkboxContainer.addView(checkBox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
for (int i = 0; i < Oils_and_Condiments.length; i++) {
String a= (String) checkBox.getText();
if(a==Oils_and_Condiments[i])
{
RadioGroup rg = create_rb(i);
checkboxContainer.get
checkboxContainer.addView(rg);
rg.setVisibility(View.VISIBLE);
//rg.getChildAt(i).setVisibility(View.VISIBLE);
}
RadioGroup create_rb(int a ){
final RadioGroup radiogp = new RadioGroup(this);
radiogp.setOrientation(RadioGroup.HORIZONTAL);
RadioButton rb0 = new RadioButton(this);
RadioButton rb1 = new RadioButton(this);
RadioButton rb2 = new RadioButton(this);
rb0.setText("1KG");
rb0.setTextSize(10);
rb1.setText("2KG");
rb1.setTextSize(10);
rb2.setText("3KG");
rb2.setTextSize(10);
radiogp.addView(rb0);
radiogp.addView(rb1);
radiogp.addView(rb2);
//checkboxContainer.addView(radiogp);
return radiogp;
}
My layout xml looks like
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollview1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearlayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
I am creating radiogroup and adding radiobuttons through code
LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout1);
RadioGroup rg = new RadioGroup(this);
rg.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
rg.setOrientation(0);
while(itr.hasNext()){
final String drinkname = itr.next();
System.out.println("drinkname: "+drinkname);
RadioButton rb = new RadioButton(this);
rb.setText(drinkname);
rg.addView(rb);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//group.clearCheck();
int id = group.getCheckedRadioButtonId();
RadioButton selectrb = (RadioButton) findViewById(id);
System.out.println(selectrb.getText().toString());
WebMenu.drinkname = selectrb.getText().toString();
}
});
ll.addView(rg);
It is supposed to show 6 values; But it is showing only 3 properly and remaining looks like hidden. Can someone help me out?
Thanks in advance!!
With the current RadioGroup class implementation, you cannot have two rows of RadioButtons.
So, either you set an HorizontalScrollView to your layout and allow the user to scroll to see all the RadioButtons or you use two RadioGroups, each of them containing 3 RadioButtons.
So, using your example (with little variations), this last solution can be implemented like this:
LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout1);
RadioGroup rg = new RadioGroup(this);
RadioGroup rg2 = new RadioGroup(this);
rg.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
rg.setOrientation(LinearLayout.HORIZONTAL);
rg2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
rg2.setOrientation(LinearLayout.HORIZONTAL);
String [] drinks = new String [] {"Mountain Dew", "7Up", "Root Beer", "Pepsi", "Cola", "Ice Tea"};
int i = 0;
while(i<drinks.length){
final String drinkname = drinks[i];
Log.i(TAG, "drinkname: "+drinkname);
RadioButton rb = new RadioButton(this);
rb.setText(drinkname);
if(i %2==0)
rg.addView(rb);
else
rg2.addView(rb);
i++;
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//unselect all radios in the other RadioGroup
if(rg2.getCheckedRadioButtonId()!=-1){
rg2.setOnCheckedChangeListener(null);
rg2.clearCheck();
rg2.setOnCheckedChangeListener(this);
}
int id = group.getCheckedRadioButtonId();
RadioButton selectrb = (RadioButton) findViewById(id);
if(selectrb != null){
Log.i(TAG,selectrb.getText().toString());
WebMenu.drinkname = selectrb.getText().toString();
}
}
});
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//unselect all radios in the other RadioGroup
if(rg.getCheckedRadioButtonId()!=-1){
rg.setOnCheckedChangeListener(null);
rg.clearCheck();
rg.setOnCheckedChangeListener(this);
}
int id = group.getCheckedRadioButtonId();
RadioButton selectrb = (RadioButton) findViewById(id);
if(selectrb != null){
Log.i(TAG,selectrb.getText().toString());
WebMenu.drinkname = selectrb.getText().toString();
}
}
});
ll.addView(rg);
ll.addView(rg2);
You just have to be careful with the fact that your two RadioGroups keep track of only 3 RadioButtons each. They do not communicate with each other and do not affect each other. So if you select one RadioButton from the first row (first RadioGroup), you still have your selection on the second RadioGroup and vice-versa. You have to manage this situation yourself.
Edit: One way to manage this is unselecting all the radioButtons in RadioGroup1, when we select one RadioButton in RadioGroup2 and vice-versa. I have added code to do that.