RadioGroup add RadioButton duplicate when standby phone android - android

I have a problem with RadioGroup and add dynamically RadioButton
I have this function:
private void createRadio() {
RadioGroup rgp= (RadioGroup) findViewById(R.id.radioGroup);
RadioGroup.LayoutParams rprms;
for(int i=0;i<radios.length();i++){
RadioButton radioButton = new RadioButton(this);
try {
JSONObject object = radios.getJSONObject(i);
radioButton.setText(object.getString("name"));
radioButton.setId(object.getInt("id"));
rprms= new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
assert rgp != null;
rgp.addView(radioButton, rprms);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Where radios is a JSONArray that I retrieve from my db.
And my problem is that when I put in "stand by" my app, and so the activity is stopped when i reopen my activity I found the value of radiogroup duplicate.
Example:
if in my App have 3 radio button like:
1
2
3
When I stanby phone without close app, and reopen app after unlocked phone, i find this:
1
2
3
1
2
3
I Believe that the problem is the rgp.addView that add radioButton without check if radiobutton already exists.
There is a method for check if radioGroup is already populated?

You can do any of following:-
1) Remove all RadioButtons from RadioGroup before adding new ones as
radioGroup.removeAllViews();
2) Check if RadioGroup is already populated
boolean alreadyPopulated = radioGroup.getChildCount() > 0;

Related

How to get all of unchecked radio buttons in android

I have 16 Radio Group in my layout and i have 40 Radio Button . I want to get which Radio Button is unchecked in Radio Groups. I want to know how can i know is there any unchecked Radio Button in my layout thanks
You should probably group all of your buttons like so:
RadioGroup rg = (RadioGroup) findViewById(R.id.my_radio_group);
List<RadioButton> radioButtonsList = new ArrayList<>();
for(int i = 0; i < rg.getChildCount(); ++i) {
RadioButton b = rg.getChildAt(i);
if(b.isChecked()) radioButtonsList.add(b);
}
Do it for all of your groups and you'll have all your unchecked buttons in a list.
Also you can use:
int checkedRadioButtonId = rg.getCheckedRadioButtonId()
to get only checked button's id.
ArrayList<RadioGroup> radioGroupList = new ArrayList<RadioGroup>();
RadioGroup group1 = (RadioGroup)findViewById(...);
RadioGroup group2 = (RadioGroup)findViewById(...);
.
.
RadioGroup group16 = (RadioGroup)findViewById(...);
radioGroupList.add(group1);
radioGroupList.add(group2);
.
.
radioGroupList.add(group16);
and later you can check which is checked or not with this
for(RadioGroup radioButtonGroup:RadioGroupList){
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);
}
or if it's the RadioButtons you are interested in then add them in an ArrayList the same way and loop in that list like this
for(RadioButton radioButton:radioButtonList){
boolean isChecked = radioButton.isChecked();
}
You can check the state of radio buttons by using the isChecked() method.
This question has already been answered here:
How to check if "Radiobutton" is checked?

how can i get values from dynamically created radion groups in my Android activity

I am creating an attendance list of student at run time from database and wants to create dynamically radio buttons in groups against each student and then take their attendance values on a submit button: my code looks like
Ll = new LinearLayout(this);
rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
RadioButton rb1 = new RadioButton(this);
rb1.setText("P");
rb1.setId(id);
rg.addView(rb1); //the RadioButtons are added to the radioGroup instead of the layout
///////////////////////////////////////////////
RadioButton rb2 = new RadioButton(this);
rb2.setText("A");
rb2.setId(id+1);
rg.addView(rb2); //the RadioButtons are added to the radioGroup instead of the layout
/////////////////////////////////////
RadioButton rb3 = new RadioButton(this);
rb3.setText("L");
rb3.setId(id+2);
rg.addView(rb3); //the RadioButtons are added to the radioGroup instead of the layout
Ll.addView(rg);//you add the whole RadioGroup to the layout
tr.addView((View)Ll); // A
how can i get values from dynamically created radion groups in my Android activity
Hope this code will help you to get selected radio button value
int selectedId = rg.getCheckedRadioButtonId();
RadioButton radiochecked = (RadioButton) findViewById(selectedId);
Toast.makeText(context,
radiochecked.getText(), Toast.LENGTH_SHORT).show();

Two radio buttons getting selected in Radio Group

Two buttons getting selected in the radio group.
I do not know where I am getting wrong. Please help me out.
final RadioGroup rg=new RadioGroup(Survay_MainActivity.this);
rg.clearCheck();
rg.setId(Integer.valueOf(entry1.getKey()));
Log.v("rg getid", "rg"+rg.getId());
for(int i =0;i<values.size();i++){
// Create Button
final RadioButton btn = new RadioButton(Survay_MainActivity.this);
btn.setId(i);
btn.setTextColor(Color.parseColor("#000000"));
btn.setBackgroundColor(Color.TRANSPARENT);
btn.setGravity(Gravity.LEFT);
btn.setText(values.get(i));
rg.addView(btn);
btn.setLayoutParams(params);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
JSONObject quesAns = new JSONObject();
String ans=btn.getText().toString().trim();
try {
quesAns.put(String.valueOf(rg.getId()), ans);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jsonarray.put(quesAns);
Log.v("jsonarray", "jsonarray"+jsonarray);
}
});
}
views.addView(rg);
1) I am creating the RadioGroup out of the loop.
2) Adding radio button to the RadioGroup in the for loop
3) When the loop finishes the RadioGroup is added to the linerlayout.
You just need to change different ids for different radio button.
There may be some id clash in gen file.
Radio button1 : android:id="#+id/one
Radio button2 : android:id="#+id/two"
Radio button3 : android:id="#+id/three"
Hope this would help.
There are some things misplaced these changes you have to make out.
1.Change your OnClickListener by OnCheckChangeListener
2.Clear check your radioGroup after adding all radioButtons and before adding it in LinearLayout.
Your problem comes from the fact that you are setting an id for your items manually
rg.setId(Integer.valueOf(entry1.getKey()));
btn.setId(i);
Android works with id's generated automatically that are different. Setting the id's manually it might happen that you give the same ID to the radio group and the radio button.
I used your code and set the group to ID 3 and radios from 0 to 4.
Needless to say that after I click the button with id 3 it always stays on because the group has the same id.
So, try to remove the id setting part, or, if you insist make sure you always have distinct id's.
I faced the same problem. The cause is the RadioGroup's id is same as one of RadioButtons
the problem is this line. you didn't send where is this code belonged to. when you get out of this scope and come back you will create new RadioGroup. You should have one and only one RadioGroup so the RadioButtons have same RadioGroup and by default one of them will be selected. To this, you can define RadioGroup in an XML and use this to define variable for it RadioGroup rg = (RadioGroup) findViewById(R.id.YOURNAME); or you can define your RadioGroup as Instance variable(Global).
final RadioGroup rg=new RadioGroup(Survay_MainActivity.this);

Methods for UI elements created dynamically on Android

I have a doubt... In my Android app, I have an Activity that its GUI is created dynamically from certain data inside my SQLite database... I didn't had any kind of trouble with this and is working fine...
In this activity, there are a bunch of TextView's and RadioGroup's and RadioButton's that are created inside a for loop and the amount of RadioButton's varies due to a condition inside this loop... like this:
for(int i=0;i<numFilasFormulario;i++){
TextView pregunta = new TextView(this);
pregunta.setText(c.getString(c.getColumnIndex("texto")));
pregunta.setGravity(Gravity.LEFT);
pregunta.setTextColor(Color.rgb(0, 0, 0));
pregunta.setTextSize(15);
pregunta.setLayoutParams(params);
ll.addView(pregunta);
if(c.getString(c.getColumnIndex("tipopregunta")).equals("Si o No")){
RadioGroup rg = new RadioGroup(this);
ll.addView(rg);
RadioButton b1 = new RadioButton(this);
b1.setText("SI");
rg.addView(b1);
RadioButton b2 = new RadioButton(this);
b2.setText("NO");
rg.addView(b2);
}else{
if(c.getString(c.getColumnIndex("tipopregunta")).equals("Seleccion Simple")){
RadioGroup rg = new RadioGroup(this);
ll.addView(rg);
RadioButton b1 = new RadioButton(this);
b1.setText("SI");
rg.addView(b1);
RadioButton b2 = new RadioButton(this);
b2.setText("NO");
rg.addView(b2);
RadioButton b3 = new RadioButton(this);
b3.setText("N/A");
rg.addView(b3);
}
}
c.moveToNext();
}
So my question is how to obtain the value of the RadioButton selected by the user... I mean, do I have to invoke the method setOnClickListener() for each RadioButton? Or I have to do it for each RadioGroup? And where do I declare this statements: inside the for loop or outside? Or there is another way?
I'm very very lost here! Any kind of help would be apreciated! Thanks!
There are only one way to get it by using OnCheckedChangeListener
You can create each listener and assign it to your RadioGroup in the loop but I am not recomment to do that. You can create multiple ID in your resource and the create just a single listener before loop and in the loop assign it to your RadioGroup ( Your Radiogroup need set the ID that create in the resource ). Here is example:
OnCheckedChangeListener listener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (group.getId()) {
case R.id.myradio_group1:
// DO your work here
break;
default:
break;
}
}
};
// your loop is here
for(int i=0;i<numFilasFormulario;i++){
....
RadioGroup rg = new RadioGroup(this);
rg.setId(R.id.myradio_group1); // or simply just using rg.setId(i+1000);
// make sure 1000, 1001, 1002, ... is already create at Resource
....
}

Android RadioGroup checks more than one RadioButton?

I am using RadioGroup, added RadioButton rdbut to RadioGroup rdgrp like rdgrp.addView(rdbut).
for(int j=0;j<3;j++)
{
RadioGroup rdgrp = new RadioGroup;
for(int i=0;i<=10;i++)
{
RadioButton rdbut = new RadioButton(this);
rdbut.setText("RadioButtion"+i);
rdbut.setId(i);
rdbut.setTag("somename");
rdgrp.addView(rdbut);
}
}
the above code shows how I initialize the radiogroup and radio button. after I run the this code, in emulator/mobile , i am able to check 2 radio buttons at a time.
What could be the problem?
Change your code like this.
RadioGroup rdgrp[] = new RadioGroup[3];
For(int j=0;j<3;j++)
{
RadioButton rdbut[] = new RadioButton[10];
For(int i=0;i<=10;i++)
{
rdbut[i].setText("RadioButtion"+i);
rdbut[i].setId(j*100+i);
rdbut[i].setTag("somename");
rdgrp[j].addView(rdbut[i]);
}
}
You have created three different Radio Group. You can select only one radio button from a single group.So from three groups you can select three buttons But there is no inter-group relationship. You can select radio buttons from different group simultaneously. In your case you can select three buttons at max.
Thanks
Sunil
Use Some thing like this xml design in user layout file.
<TableLayout
android:id="#+id/tbl_layoutchoice"
style="#style/InfoTableView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dip" >
<RadioGroup
android:id="#+id/SelectLayout_Group"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</RadioGroup>
</TableLayout>
and For Use this RadioGroup in Activity 's OnCreate() Method and findView like this
mRadioGroup = (RadioGroup) this.findViewById(R.id.SelectLayout_Group);
and Then Use Below Code With Your Require Change To Add Radio Button in One RadioGroup.Use Also Below Require Declaration for Create Radio Button Dynamically.
ArrayList<String> layoutlist = new ArrayList<String>(3);
int index = -1;
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
for (String layout : layoutlist) {
RadioButton r = new RadioButton(this);
index++;
r.setText(layout);
r.setId(index);
r.setLayoutParams(lp);
r.setTextAppearance(this, R.style.TextBase);
mRadioGroup.addView(r);
}
So don't forget to add your String Value in layoutlist before for loop .and R.style is some Require Style for Text Show in RadioButton.

Categories

Resources