How to programmatically select RadioButton in an activity - android

In my project, I get the value of the gender from the database as 0 and 1.
0 for male and 1 for female.
Based on this values, I need to check the corresponding RadioButton in a RadioGroup.
If 0 is pressed, radio0 will be checked or radio1 will be checked.
I don't know how to check the radio button based on this string values...
This is the code I tried:
String gendd=ViewProfileActivity.participantview.get(4);
System.out.println("Gender:::::"+gendd);
male=(RadioButton)findViewById(R.id.radio0);
female=(RadioButton)findViewById(R.id.radio1);
if(gendd.equals("0")){
male.setSelected(true);
female.setSelected(false);
}
else
{
male.setSelected(false);
female.setSelected(true);
}
But it fails. Can anyone help me?

Try this.
if(gendd.contains("0"))
{
male.setChecked(true);
female.setChecked(false);
}
else
{
male.setChecked(false);
female.setChecked(true);
}

You shoud use check(radioButtonId) method on parent RadioGroup.
E.g.
radioGroup.check(gendd.equals("0") ? R.id.radio0 : R.id.radio1);
(assuming the comparison holds as the types haven't been mentioned)

Related

null object reference in Android

I am trying to work on radio groups. I have an ok button which when clicked selects one item from four inflated radio button groups. My problem is that, when the user does not select an option from one of the groups, I have a null pointer exception.
Here is my selection code
private void getSelectedValuesAdults()
{
rl=(RelativeLayout)findViewById(R.id.activity_selections);
int id1 = ((RadioGroup)findViewById(R.id.KSradioGrp)).getCheckedRadioButtonId();
int id2 = ((RadioGroup)findViewById(R.id.KTradioGrp)).getCheckedRadioButtonId();
int id3 = ((RadioGroup)findViewById(R.id.KDradioGrp)).getCheckedRadioButtonId();
int id4 = ((RadioGroup)findViewById(R.id.KSoradioGrp)).getCheckedRadioButtonId();
stringChoice[0]= ((RadioButton)findViewById(id1)).getText().toString().contains(generalKid[0].getName())==true?generalKid[0].getName():generalKid[1].getName();
stringChoice[1]= ((RadioButton)findViewById(id2)).getText().toString().contains(generalKid[2].getName())==true?generalKid[2].getName():generalKid[3].getName();
stringChoice[2]= ((RadioButton)findViewById(id3)).getText().toString().contains(generalKid[4].getName())==true?generalKid[4].getName():generalKid[5].getName();
stringChoice[3]= ((RadioButton)findViewById(id4)).getText().toString().contains(generalKid[6].getName())==true?generalKid[6].getName():generalKid[7].getName();
genCost[0]= ((RadioButton)findViewById(id1)).getText().toString().contains(generalKid[0].getName())==true?generalKid[0].getCost()+5:generalKid[1].getCost()+5;
genCost[0]= ((RadioButton)findViewById(id2)).getText().toString().contains(generalKid[2].getName())==true?generalKid[2].getCost()+5:generalKid[3].getCost()+5;
genCost[0]= ((RadioButton)findViewById(id3)).getText().toString().contains(generalKid[4].getName())==true?generalKid[4].getCost()+5:generalKid[5].getCost()+5;
genCost[0]= ((RadioButton)findViewById(id4)).getText().toString().contains(generalKid[6].getName())==true?generalKid[6].getCost()+5:generalKid[7].getCost()+5;
}
The Ok button jumps to the method above. I have used if(id1==-1||id2==-1||id3==-1||id4==-1) callDialogMessage() just before the assignment to stringChoice[0] where callDialogMessage() is a message that lets you know you have not selected from a radio group but my app still crashes. What can I do? Any help will be highly appreciated
On click of OK button get selected radio button Id using radio group .You can refer the below code.
int selectedId =radioButtonGroup.getCheckedRadioButtonId();;
// find the radiobutton by returned id
RadioButton radioButton = FindViewById<RadioButton>(selectedId);
if(radioButton !=null)
{
//do things here
}

Two radio buttons getting selected in Radio Group

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);

Want to Pre-set Radio buttons based on string

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);

How to clear checkboxes when reset button is clicked

In my project I have a number of checkboxes in a linear layout. When running the project by default all checkboxes are unchecked and I need to check some checkboxes. Below the checkboxes there is a reset button. When the reset button is clicked all the checkboxes are unchecked. How do I do this? Please help.
Call this in onClick()of Reset button.
if (checkBox1.isChecked()) {
checkBox1.setChecked(false);
}
if (checkBox2.isChecked()) {
checkBox2.setChecked(false);
}
.
.
.
and so on
To Uncheck all checked CheackBoxes keep references of the checked Checkboxes in ListView/Array and when reset button is clicked mark them as unchecked,
ListView <CheckBox> selectedcheckBox = new ListView<CheckBox> ();
when Checkbox is Checked---
selectedcheckBox.add(referanceofckeckbox).
now when reset button is clicked
public void onclick(View v){
for(CheckBox cb : selectedcheckBox){
cb.setChecked(false);
}
}
Hope it help.
if (checkBox.isChecked()) {
checkBox.toggle();
}
use this for all the checkboxes used.
For me it worked like this:
CheckBox checkBox = (CheckBox) findViewById(R.id.check_box);
checkBox.setChecked(false);
I see this is old stuff, but has no valid answer so maybe this one helps anyone.
jQuery + CSS selectors work very well for this. If you just want to uncheck all those checkboxes that are checked you just need the following code:
$('[type=checkbox]:checked').prop('checked', false);
While if you want to toggle all you can add variable to hold the checked status like this:
var isChecked = $('[type=checkbox]').is(':checked');
$('[type=checkbox]').prop('checked', !isChecked);

How to get the value inside the if condition?

I am comparing strings in if condition and printing the value of selectedradbtn inside the if condition. Here the selectedradbtn is printing inside the if condition...but when I am printing before if condition the value is printing...
My code:
ArrayList<String> array2;
static int selectedradbtn;
selectedradbtn = Previouspage.selectedradiobtn;
System.out.println("selected radio button outside if"+selectedradbtn); //here value is printing
if(Integer.toString(selectedradbtn).equals(array2.get(0)) && array2.get(3).equals("1") )
{
System.out.println("selected radio button in if"+selectedradbtn); //value not printing
correct = correct+ 1;
System.out.println("correct if"+correct);
}
Where I am going wrong...please help me regarding this...
Thanks in advance...
not sure, but does this help
selectedradbtn = Previouspage.selectedradiobtn;
System.out.println("selected radio button outside if"+selectedradbtn); //here value is printing
if((Integer.toString(selectedradbtn).equals(array2.get(0)) && (array2.get(3).equals("1")))
{
System.out.println("selected radio button in if"+selectedradbtn); //value not printing
correct = correct+ 1;
System.out.println("correct if"+correct);
}
Add an else {} and check if your condition is satisfied or not.

Categories

Resources