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
....
}
Related
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;
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();
This is the layout I want to create but dynamically through java code.
Here parent layout is LinearLayout.
The tableLayout created through java code consists of 5 radio buttons which are created dynamically.
I need to place these radio button in radio group.
How to do that.
So far i tried this which is working fine. But am not able to add all the 5 radio buttons in 1 radio group.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (LinearLayout) findViewById(R.id.mainl); //parent layout
t1 = new TableLayout(this);
rb1 = new RadioButton(this);
rb2 = new RadioButton(this);
rb3 = new RadioButton(this);
t1.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TableRow row = new TableRow(this);
TableRow row1 = new TableRow(this);
row.addView(rb1);
row.addView(rb2);
row1.addView(rb3);
t1.addView(row);
t1.addView(row1);
l.addView(t1);
}
I can't find you'r declaration of a RadioGroup.
You need something like this:
...
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);
rg.addView(rb1);
rg.addView(rb2);
rg.addView(rb3);
row.addView(rg);
...
RadioGroup inherits from LinearLayout. You'll have to create your own implementation of RadioGroup that inherits from TableLayout.
If you examine the source code of RadioGroup, you'll see it just keep track of its child views which are RadioButton's and makes sure only one of them is checked. Apart from that it provides a listener if checked RadioButton changes.
You can try modifying the source to make it extend from TableLayout etc.
I have created RadioGroup and RadioButton dynamically as following:
RadioGroup radioGroup = new RadioGroup(context);
RadioButton radioBtn1 = new RadioButton(context);
RadioButton radioBtn2 = new RadioButton(context);
RadioButton radioBtn3 = new RadioButton(context);
radioBtn1.setText("Less");
radioBtn2.setText("Normal");
radioBtn3.setText("More");
radioBtn2.setChecked(true);
radioGroup.addView(radioBtn1);
radioGroup.addView(radioBtn2);
radioGroup.addView(radioBtn3);
Here step radioBtn2.setChecked(true); causes radioBtn2 always checked. That means I cannot uncheck radioBtn2 by checking other two radio buttons (radioBtn1,radioBtn3). I want to make that RadioGroup can check only one radio button at a time (Now it can check two radiobutton at a time).
How can I solve this problem?
you should check the radiobutton in the radiogroup like this:
radiogroup.check(IdOfYourButton)
Of course you first have to set an Id to your radiobuttons
EDIT: i forgot, radioButton.getId() works as well, thx Ramesh
EDIT2:
android:checkedButton="#+id/my_radiobtn"
works in radiogroup xml
In case for xml attribute its android:checkedButton which takes the id of the RadioButton to be checked.
<RadioGroup
...
...
android:checkedButton="#+id/IdOfTheRadioButtonInsideThatTobeChecked"
... >....</RadioGroup>
In the XML file set the android:checkedButton field in your RadioGroup, with the id of your default RadioButton:
<RadioGroup
....
android:checkedButton="#+id/button_1">
<RadioButton
android:id="#+id/button_1"
...../>
<RadioButton
android:id="#+id/button_2"
...../>
<RadioButton
android:id="#+id/button_3"
...../>
</RadioGroup>
RadioGroup radioGroup = new RadioGroup(WvActivity.this);
RadioButton radioBtn1 = new RadioButton(this);
RadioButton radioBtn2 = new RadioButton(this);
RadioButton radioBtn3 = new RadioButton(this);
radioBtn1.setText("Less");
radioBtn2.setText("Normal");
radioBtn3.setText("More");
radioGroup.addView(radioBtn1);
radioGroup.addView(radioBtn2);
radioGroup.addView(radioBtn3);
radioGroup.check(radioBtn2.getId());
RadioGroup radioGroup = new RadioGroup(context);
RadioButton radioBtn1 = new RadioButton(context);
RadioButton radioBtn2 = new RadioButton(context);
RadioButton radioBtn3 = new RadioButton(context);
radioBtn1.setText("Less");
radioBtn2.setText("Normal");
radioBtn3.setText("More");
radioGroup.addView(radioBtn1);
radioGroup.addView(radioBtn2);
radioGroup.addView(radioBtn3);
radioBtn2.setChecked(true);
There was same problem in my Colleague's code. This sounds as your Radio Group is not properly set with your Radio Buttons. This is the reason you can multi-select the radio buttons. I tried many things, finally i did a trick which is wrong actually, but works fine.
for ( int i = 0 ; i < myCount ; i++ )
{
if ( i != k )
{
System.out.println ( "i = " + i );
radio1[i].setChecked(false);
}
}
Here I set one for loop, which checks for the available radio buttons and de-selects every one except the new clicked one. try it.
It's a bug of RadioGroup
RadioButton radioBtn2 = new RadioButton(context);
radioBtn2 without viewId, and generateViewId is in onChildViewAdded()
public void onChildViewAdded(View parent, View child) {
if (parent == RadioGroup.this && child instanceof RadioButton) {
int id = child.getId();
// generates an id if it's missing
if (id == View.NO_ID) {
id = View.generateViewId();
child.setId(id);
}
((RadioButton) child).setOnCheckedChangeWidgetListener(
mChildOnCheckedChangeListener);
}
if (mOnHierarchyChangeListener != null) {
mOnHierarchyChangeListener.onChildViewAdded(parent, child);
}
}
so, first radioGroup.addView(radioBtn2), then radioBtn2.setChecked(true);
Like this:
RadioGroup radioGroup = new RadioGroup(context);
RadioButton radioBtn1 = new RadioButton(context);
RadioButton radioBtn2 = new RadioButton(context);
RadioButton radioBtn3 = new RadioButton(context);
radioBtn1.setText("Less");
radioBtn2.setText("Normal");
radioBtn3.setText("More");
radioGroup.addView(radioBtn1);
radioGroup.addView(radioBtn2);
radioGroup.addView(radioBtn3);
radioBtn2.setChecked(true);
Add android:checked = "true" in your activity.xml
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.