Android RadioGroup Radiobutton Java - java

I have two radiogroups - RadioGroupSelect, questionRG.
In RadioGroupSelect is radioButton1, radioButton2.
In questionRG is radiobutton questionRB1.
How to set any text in questionRB1 if i will select radioButton1 and set other text, if i will select radioButton2?

Use the following code
if(radioButton1.isChecked()){
questionRB1.setText("your text");
}
Do similarly for the other radio button.

Related

how to setChecked radio button in programmatically (radio button created in dynamically ) android

I try that
radioButton.setChecked(true);
but its only work 4th radio button. I try to create radio button dynamically. I create radio buttion within for loop, then store the radio button value. Then restore the radio button value (that means, I have 4 options that time I choose 2nd option and saved it then restore it(setChecked 2nd option) ) but its only setChecked 4th option.
Create radioButton.
for (int k = 0; k < choiceElementList.size(); k++) {
if (choiceElementList.get(k).dataFormatId == 1) {
radioButton = new RadioButton(getContext());
radioButton.setText(choiceElementList.get(k).getDataFormatValue());
radioButton.setLayoutParams(params1);
radioButton.setPadding(0, 5, 0, 5);
Log.e("setid", String.valueOf(choiceElementList.get(k).getId())) ;
radioGroup.addView(radioButton);
}
}
Try to restore that
if(choiceElementList.get(k).getId() == Cons.Id){
radioButton.setChecked(true);
}
First set Ids to your RadioButtons
for (int k = 0; k < choiceElementList.size(); k++) {
if (choiceElementList.get(k).dataFormatId == 1) {
RadioButton radioButton = new RadioButton(getContext());
// Set ID to Radio Button
radioButton.setId(k);
radioButton.setText(choiceElementList.get(k).getDataFormatValue());
radioButton.setLayoutParams(params1);
radioButton.setPadding(0, 5, 0, 5);
Log.e("setid", String.valueOf(choiceElementList.get(k).getId())) ;
radioGroup.addView(radioButton);
}
}
now just use your RadioGroup to check desire RadioButton with its ID
if(choiceElementList.get(k).getId() == Cons.Id){
radioGroup.check(k); // K will be your ID Set for your desire RadioButton
}
Happy Coding...
according to this code segments, your radioButton variable only refer last created element (radiobutton). That's why it only marks fourth one. You need to get correct reference for radio button what u want.
This is because you're adding all your RadioButton in a RadioGroup. When a RadioButton is checked in a RadioGroup, the othe RadioButton will be unchecked. You can see the RadioGroup documentation say it clearly:
This class is used to create a multiple-exclusion scope for a set of radio buttons. Checking one radio button that belongs to a radio group unchecks any previously checked radio button within the same group.
Intially, all of the radio buttons are unchecked. While it is not possible to uncheck a particular radio button, the radio group can be cleared to remove the checked state.
The selection is identified by the unique id of the radio button as defined in the XML layout file.

Dynamically adding radio button list according to string values in Android Studio?

I have seen that this can be done for ListView where you can just grab the string-array values from strings.xml and populate the ListView dynamically depending on the number of listed items.
But I was wondering if the same feat can be done using Radio Buttons and Radio Button Group?
I've come across this piece of code
RadioGroup rgp= (RadioGroup) findViewById(R.id.radiogroup);
RadioGroup.LayoutParams rprms;
for(int i=0;i<3;i++){
RadioButton radioButton = new RadioButton(this);
radioButton.setText("new"+i);
radioButton.setId("rbtn"+i);
rprms= new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rgp.addView(radioButton, rprms);
}
And was wondering how I can modify the radioButton.setId("rbtn"+i) to grab string-array values from the strings.xml file instead.
Use String[] foo_array = getResources().getStringArray(R.array.foo_array); to read array from strings.xml. Then loop through the obtained array.

get the name of the selected radio button from button group

I want to return selected radio button name from button group in java :
ButtonGroup bg = new ButtonGroup();
bg.add(radiobutton1);
bg.add(radiobutton2);
bg.add(radiobutton3);
I think you can loop through the radio buttons, for each button if isSelected() is true then call button.getText() to get the name.
for (AbstractButton button : bg.getElements())
if (button.isSelected())
return button.getText();
Why do you want the button name? It isn't of much use. You can, however, get the all of the button references using bg.getElements(), and you can get the text of each of those references with button.getText(), both of which are of much more use than the name you gave the button. If you REALLY want the name, you can create the buttons and then call radiobutton1.setName("radiobutton1") and then use getName() on the reference.

How to uncheck all radio buttons in a radio group, when a radio button is checked in Android?

I have created programmatically 5 radio groups with 4 radio buttons each. The problem is, when i first check a radio button from the first radio group and after that i check the second radio button from the same radio group, the first radio button remains checked. What can i do to have the normal behavior for all the radio buttons in the radio group?
This is my code:
radioGroup = new RadioGroup[5];
answer = new RadioButton[4];
int i = 0;
for (Question qn : questions) {
radioGroup[i] = new RadioGroup(this);
int j = 0;
for (Answer an : answers) {
if (qn.getID() == an.getQuestion_id_answer()) {
answer[j] = new RadioButton(this);
answer[j].setText(an.getAnswer());
radioGroup[i].addView(answer[j]);
j++;
}
}
linearLayout.addView(radioGroup[i]);
i++;
}
Thanks!
"Checking one RadioButton that belongs to a RadioGroup unchecks any previously checked RadioButton within the same group." Source: developer.android.com
If I understand right: You are talking about 4 RadioButtons in the same RadioGroup that refuse to uncheck themselves when another button in the same RadioGroup gets checked?
If that's the case, just give a different Resource ID to every RadioButton and that should fix it.

how to check radio Button on activity load

I have taken a radio group in my layout and I want to check one of the radio button from that radio group by default when activity is initialised. The re is a condition on which any one of the radio button should be checked by default.the code for that is:
if (preferencedPractices == null) {
allRadioBtn.setSelected(true);
System.out.println("preferences are null");
} else {
prefRadioBtn.setSelected(true);
System.out.println("there are some preferences");
}
Here if preferencedPractices is null, I want allRadioBtn checked by default else i want prefRadioBtn checked bydefault. But due to some reason this is not working. None of the button is checked when activity is started. I have to check any of the button..Please help.
In your xml file you can always have a radio button as checked using:
android:checked="true"
In your java code you can do the same using:
rb1= (RadioButton) findViewById(R.id.rb1);
rb2 = (RadioButton) findViewById(R.id.rb2);
rb1.setChecked(true);
Just keep in mind that this will not work alone in RadioGroup you have to individually initialize the radiobutton which you want to set as checked.

Categories