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?
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.
In xml layout I have RadioGroup, Button1, Button2. When user clicks on button1, several radio buttons are programmatically created in RadioGroup (total amount of radio buttons may differ (pocet = count of radio buttons to be created).
final RadioButton[] rb = new RadioButton[pocet];
RadioGroup rg = (RadioGroup) findViewById(R.id.MyRadioGroup);
radiobuttonCount++;
for(int i=0; i<pocet; i++){
rb[i] = new RadioButton(this);
rb[i].setText("Radio Button " + radiobuttonCount);
rb[i].setId(radiobuttonCount+i);
rb[i].setBackgroundResource(R.drawable.button_green);
rg.addView(rb[i]);
}
What I try to do is this: When user selects xy item from RadioGroup, I'll pass selected value to textview and remove all radioButtons.
For deleting purpose I use:
public void onCheckedChanged(RadioGroup rGroup, int checkedId)
{
RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(checkedId);
boolean isChecked = checkedRadioButton.isChecked();
if (isChecked)
{
RadioGroup rg = (RadioGroup) findViewById(R.id.MyRadioGroup);
for (int i=0; i< rg.getChildCount(); i++){
rg.removeViewAt(i);
}
}
Problem is that this sometimes works well, but sometimes first radio button remains undeleted.
P.S.
Later I want to add button2 that will feed radiogroup with different items and different radio buttons amount. That's why I need to remove all radio buttons after user does selection.
Its really easy, you just do this:
rg.removeAllViews();
because i worked with the for loop but it didn't remove all the RadioButtons.
have fun :)
My first guess is that this portion of code is bad:
for (int i=0; i< rg.getChildCount(); i++){
rg.removeViewAt(i);
}
you can't run over one view's children while removing child at the same time
(rg.getChildCount() will change during the run)
I am creating a quiz application in android.For each question I have set up 4 radio buttons for the user to click. How do i change the text of the radiobutton dynamically for each question? The text for these radio button are stored in a raw text file.
Here you go.
I have set up 4 radio buttons for the user to click.
You must have defined them in radioGroup,right?
Then you can iterate in RadioGroup to set names to RadioButton
or you can get RadioButton by index and set name to it.
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.group);
for (int i = 0; i < radioGroup .getChildCount(); i++) {
((RadioButton) radioGroup.getChildAt(i)).setText(String.valueOf(i));
}
You can get an object of the button by using
RadioButton button = (RadioButton)findViewById(R.id.yourButtonId);
Then write:
button.setText("This is another text...");
Didn't try to run it, but it should work...
I have a radio group which I do not want to user to be able to select any of the buttons until a particular checkbox is selected within my app. If the checkbox is unticked then this disables the radio-group. How do I go about doing this.
The real trick is to loop through all children view (in this case: CheckBox) and call it's setEnabled(boolean)
Something like this should do the trick:
//initialize the controls
final RadioGroup rg1 = (RadioGroup)findViewById(R.id.radioGroup1);
CheckBox ck1 = (CheckBox)findViewById(R.id.checkBox1);
//set setOnCheckedChangeListener()
ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton checkBox, boolean checked) {
//basically, since we will set enabled state to whatever state the checkbox is
//therefore, we will only have to setEnabled(checked)
for(int i = 0; i < rg1.getChildCount(); i++){
((RadioButton)rg1.getChildAt(i)).setEnabled(checked);
}
}
});
//set default to false
for(int i = 0; i < rg1.getChildCount(); i++){
((RadioButton)rg1.getChildAt(i)).setEnabled(false);
}
If you have just a few radio buttons, better way would be to setClickable(false) for all children
radiobutton1.setClickable(false);
radiobutton2.setClickable(false);
radiobutton3.setClickable(false);
RadioGroup cannot be disabled directly, we have to loop through the radio button and setEnabled as false.
// To disable the Radio Buttons of radio group.
for (int i = 0; i < radioGroup.getChildCount(); i++) {
radioUser.getChildAt(i).setEnabled(false);
}
If you want to implement the same for Kotlin, Here's my code->
where rgNotificationType is the radio group name.
for (i in 0 until rgNotificationType.childCount) {
(rgNotificationType.getChildAt(i)).isEnabled = false
}
You can use the onCheckedChangeListener on your CheckBox and use the method setEnabled on your RadioGroup.
Best wishes,
Tim
By using kolin's extensions:
fun RadioGroup.setChildrenEnable(enable: Boolean) {
for (i in 0 until this.childCount) {
this.getChildAt(i).isEnabled = enable
}
}
And in your code you can just call the function like this:
radioGroup.setChildrenEnable(false)
Kotlin Solution
for (index in 0..radio.childCount - 1)
radio.getChildAt(index).isEnabled = false
Take actions according to the state of the checkbox and set the radiogroup accordingly.
Assuming that you have a radio-group named radiogroup you can enable or disable the radiogroup by
radiogroup.setEnabled(true);
Add a OnCheckedChangeListener() to your checkbox.
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.