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...
Related
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?
as part of my application I have to create four radioButtons within a radiogroup and get the radio button clicked. I wrote the following code but my selectId attribute gives values 2131034181 when first option is selected and 2131034182 when second option is selected and so on, there's change in unit digit of value when 3 and 4 are clicked. Why is it so?
int selectId=rbg.getCheckedRadioButtonId();
RadioButton selected= (RadioButton) rbg.findViewById(selectId);
String selected_user = (String) selected.getText();
To get radiobutton,use findViewById from context instead of radiogroup,So change
RadioButton selected= (RadioButton) rbg.findViewById(selectId);
String selected_user = (String) selected.getText();
to
RadioButton selected = (RadioButton)findViewById(selectId);
String selected_user = selected.getText().toString();
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);
I am trying to generate radio button in a radio Group dynamically. I have a radioGroup having three radioButtons. On runtime I want to put a validation that if no radio Button is checked, set a background color of radioGroup to red indicating warning.
For the purpose I am using
public String validate() {
if (radioGroup.findViewById(radioGroup.getCheckedRadioButtonId()) != null){
return "SUCCESS";
} else {
radioGroup.setBackgroundResource(R.drawable.red_border);
return "failure";
}
}
but here the background is not changing for radioGroup i have tried similar approach for editText and DatePicker in all that it's working.
We can do this using looping through radioGroup children
for (int i = 0; i < radioGroup.getChildCount(); ++i) {
((RadioButton) radioGroup.getChildAt(i))
.setBackgroundResource(R.drawable.red_border);
}
This will loop through each and every children of RadioGroup and like this we can achieve the desired functionality
Hi I am creating an edit page for a database and i want to
get the string ive passed over via intent
then depending on if the string is equal to "on" or "off"
i want to change the the selection of the radio button
so far i have this
radiogroup = R.id.radioGroup1;
if (switch.equals("on")){
radiogroup.check(R.id.radio1);
R.id.radio1.setChecked(true);
} else {
radiogroup.check(R.id.radio0);
R.id.radio1.setChecked(true);
}
btw this is in my oncreate as i want it to run as soon as the Activity is open
Inflate your radio buttons, per example with
RadioButton radio1 = findViewById(R.id.radio1);
Then you can use
radio1.setChecked(true/false)
like you were trying.
You need to find the RadioButtons like this:
RadioButton radio1 = (RadioButton) findViewById(R.id.radio1);
after that you can call
radio1.setChecked(true); or radio1.setChecked(false);