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
Related
I had added radio buttons dynamically like below.
for (int i = 0; i< typeArrayList.size(); i++) {
radioButtons[i] = new RadioButton(MainActivity.this);
radioButtons[i].setId(i);
radioButtons[i].setText(typeArrayList.get(i).toString());
if(i==0) {
radioButtons[i].setChecked(true);
}
typeLayout.addView( radioButtons[i]);
}
I have a button when clicked calls a method to which the selected item (text) of the dynamically added radio buttons should be passed . How can I get the selected radio button text for the dynamically added radio buttons?
Its very important to set your radio button id when adding buttons to a radio group.
RadioButton rb = new RadioButton(context);
rb.setText(option);
rb.setId(rb.hashCode());
radioGroup.addView(rb);
radioGroup.setOnCheckedChangeListener(mCheckedListner);
Now in your click listener check for the unique id.
private RadioGroup.OnCheckedChangeListener mCheckedListner = new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
JSONArray actionArray = new JSONArray();
if(group.findViewById(checkedId)!=null) {
RadioButton rb = ((RadioButton) group.findViewById(checkedId)).getText();
//Your Code here
}
}
}
};
You have stored all RadioButtons that you create dynamically in a radioButtons array.
So if you want to know which radiobutton is selected you can loop all this array and check each RadioButton
for (int i = 0; i< radioButtons.size(); i++) {
if(radioButtons[i].isChecked()){
// selected radio button is here
String text = radioButtons[i].getText();
}
}
First you have to get all childs of your view. Then check if this view is RadioButton. And at last check which button is checked.
int childcount = typeLayout.getChildCount();
for (int i=0; i < childcount; i++){
View view = typeLayout.getChildAt(i);
if (view instanceof RadioButton) {
if(((RadioButton)view).isChecked()) {
RadioButton yourCheckedRadioButton = (RadioButton) typeLayout.getChildAt(i); // this is your checked RadioButton
}
}
}
#Komali Shirumavilla
Add those dynamic radio buttons to radio group
so add following lines in your code
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
for (int i = 0; i< typeArrayList.size(); i++) {
radioButtons[i] = new RadioButton(MainActivity.this);
radioButtons[i].setId(i);
radioButtons[i].setText(typeArrayList.get(i).toString());
if(i==0) {
radioButtons[i].setChecked(true);
}
rg.addView(radioButtons[i]); // add dynamic radio buttons to radio group
}
typeLayout.addView(rg); // add radio group to view
Now set setOnCheckedChangeListener on the RadioGroup
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
RadioButton btn = (RadioButton)findViewById(checkedId);
Log.d("Your selected radio button id",btn.getText());
}
}
});
This can be simply achieved using tags.
While adding the RadioButton set a tag that particular object. In your case, set the tag to be the text of the radio button,
radioButtons[i].setTag(typeArrayList.get(i).toString());
This way all your radio buttons will have the text associated to them as a tag.
Now whenever a particular radio button is selected, just get the tag which will give you the text associated with it.
String text = (String) selectedRadioButton.getTag();
Hope it helps.
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?
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();
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
....
}
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.