i have a radio group with two radio buttons in it. I want to get the value of the radio button and then store it in the database..how do i do that?? Pls help! I searched for it but all in vain!
I tried this code but my activity stops working after using it
rg=(RadioGroup)findViewById(R.id.radioGroup2);
if(rg.getCheckedRadioButtonId()!=-1)
{
int id=rg.getCheckedRadioButtonId();
View radioButton=rg.findViewById(id);
int radioid=rg.indexOfChild(radioButton);
RadioButton btn = (RadioButton) rg.getChildAt(radioid);
Father_spouse=(String)btn.getText();
}
if you want to store the text label of your RadioButton then use this :
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
if(selectedId != -1) {
// find the radiobutton by returned id
selectedRadioButton = (RadioButton) findViewById(selectedId);
// do what you want with radioButtonText (save it to database in your case)
String radioButtonText = selectedRadioButton.getText();
}
if you want to save a boolean value so test on the selectedId of your RadioButtons and save a 0 or 1 to your database column (Example of two radio buttons to enable/disable updates) :
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
boolean isAllowUpdate = false;
switch(selectedId) {
case R.id.radioAllowUpdate : isAllowUpdate = true; break;
case R.id.radioDisableUpdate : isAllowUpdate = false; break;
}
//save it to database
if(isAllowUpdate)
// true ==> save 1 value
else
// false ==> save 0 value
EDIT :
if you should control the selected value and when send it to database, see this tutorial
Related
So I'm trying to learn Kotlin and have been using Android Studios to practice and learn. Currently I'm trying to make a simple activity with RadioGroup (with Radio Buttons), save the selected value, and then display how much of each value (radiobutton) was selected.
My question is, how do I print which button was selected, and how many of this type of button was selected?
I tried the following:
//in MainActivity.kt in my MainActivity class
s1RadioGroup.setOnCheckedChangeListener { _, checkedId ->
//if catButton was selected add 1 to variable cat
if(checkedId == R.id.catRadio) {
catSum += 1
print(catSum)
}
//if dogButton was selected add 1 to variable dog
if(checkedID == R.id.dogRadio) {
dogSum += 1
print(dogSum)
}
Not sure if I'm going about it the right way, but the desired output is:
I have layout, ID's, clear button, and everything else working. But I'm not sure how to use onClickListener event on 'SaveButton' to save selected radio button and then displaying results (Ex: Cat = 1, Dog =2). I would appreciate any suggestions, or if you can point me in the right direction.
You can maybe try something like this:
RadioButton rb = (RadioButton) findViewById(R.id.radio_button);
// restore previous state
rb.setChecked(lastButtonState);
// set a listener
rb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// call this to enable editing of the shared preferences file
// in the event of a change
SharedPreferences.Editor editor = sharedpreferences.edit();
Boolean isChecked = rb.isChecked();
// use this to add the new state
editor.putBoolean(BUTTON_STATE, isChecked);
// save
editor.apply();
}
});
I realize that this is in Java, and you're asking for kotlin, but a SharedPreference, is what you would need to save the radio button's state.
if you want to save all datam you can use database or sharedprefrence.
and if you only want just display value is clicked, you can make like this in button save.
String result1 = ""
String result2 = ""
String result3 = ""
RadioGroup radioGroup = findViewById('yourRGidFromXml')
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
int selectedId = radioGroup.getCheckedRadioButtonId();
RadioButton rb = findViewById(selecetedId)
result1= rb.getText.toString()
Log.i("ID", String.valueOf(selectedId));
}
});
//this just for see result
btnSave.OnclikListener(view -> {
Log.i("Result1",result1)
})
you can copy code and android will convert that code to kotlin.
I have a radiogroup which loads questions depending on the answer from the previous set of questions. It is the same radiogroup, only the questions change.
Here is code part where I load the questions;
if (key.substring(0,2).equalsIgnoreCase("CS")){
actionText.setVisibility(View.GONE);
radioGroup.setVisibility(View.VISIBLE);
radioGroup.removeAllViews();
btnForward.setEnabled(true);
Set optTextset=thisScreenOpts.lhmOptionText.entrySet();
Set optAnswerSet=thisScreenOpts.lhmNextStep.entrySet();
Iterator i=optTextset.iterator();
Iterator i2=optAnswerSet.iterator();
if (thisScreenOpts.title!=null){
myText.setText(thisScreenOpts.title);
}else{
myText.setText("");
}
while (i.hasNext()){
Map.Entry optTextMap=(Map.Entry)i.next();
Map.Entry optAnswerMap=(Map.Entry)i2.next();
RadioButton radioButton=new RadioButton(this);
radioButton.setTag(optAnswerMap.getValue().toString());
radioButton.setText(optTextMap.getValue().toString());
radioGroup.addView(radioButton);
}
}
and I have a forward button that checks which Radio Button is clicked;
if (masterkey.substring(0,2).equalsIgnoreCase("CS")) {
if (radioGroup.getCheckedRadioButtonId() == -1) {
Toast.makeText(getBaseContext(), "Please select an option", Toast.LENGTH_SHORT).show();
break;
}
int selectedID = radioGroup.getCheckedRadioButtonId();
RadioButton selectedRadio = (RadioButton) findViewById(selectedID);
keyTag = selectedRadio.getTag().toString();
backList.add(masterkey);
masterkey = keyTag;
createScreen(masterkey);
break;
}
My problems is the selected index, or the results of;
radioGroup.getCheckedRadioButtonId()
does not get cleared even though I remove the views. So the keyTag = selectedRadio.getTag().toString(); gives a nullPointerException.
Please let me know how to reset the selected index of a radiogroup.
Thanks in advance
Got it.
raduiGroup.clearCheck();
will clear the selection.
I want to highlight right and wrong option when user clicks a button after checking an radio button. If that option is right, highlight. Else highlight right option.
Is there any way to get radio button id in a group based on its value? Or Do I need to use switch case?
I searched enough but not able to find out what I need.
Edit
I have simple layout which contains one question, 4 choices, one button. User check a radio button and click the check button. If user selects wrong option, highlight the correct option. I know what is correct option by value.
choice1, choice2, choice3, choice4 are four radio buttons. User checks choice3. But choice2 is correct. How do I select choice2 by value in this radio group.
group.getRadioButtonId("choice2")
Anything similar to this?
You could iterate through the children of your RadioGroup to get the required one:
int count = radioGroup.getChildCount();
for (int i = 0 ; i < count; i++) {
RadioButton button = (RadioButton) radioGroup.getChildAt(i);
if (button.getText().equals("choice2")) {
int id = button.getId(); // the ID you're looking for
}
}
You Can check like this
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
int rbId = group.getCheckedRadioButtonId();
RadioButton rb = (RadioButton) findViewById(rbId);
switch (group.getId()) {
case R.id.radiogroupID:
if (rb.getText().toString().equalsIgnoreCase("right")) {
// your logic
} else {
//your logic
}
}
}
you can use these line. its working for me
int checkedId = radioGroup.getCheckedRadioButtonId();
View radioButton = radioGroup.findViewById(checkedId);
int radioId = radioGroup.indexOfChild(radioButton);
RadioButton btn = (RadioButton) radioGroup.getChildAt(radioId);
First check which option is true. hear choice 2 OK. set by default is checked true and than check user checked value . if user select other option than display message and pass true option we already checked true
Create an int array with all the ids of the radiobutton
int[] rb_IDs=new int[]{R.id.rb1,R.id.rb2,R.id.rb3};
Then inside your button click :
RadioGroup rg=(RadioGroup) findViewById(R.id.rg);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for (int i=0;i<rb_IDs.length;i++)
{
RadioButton rb=(RadioButton)findViewById(rb_IDs[i]);
if (rb.getText().toString().equals("YOUR CORRECT ANSWER")&&!rb.isChecked())
{
//
//Do your thing
//
//ID of the correct answer Radiobutton is
int id=rb_IDs[i];
//HighLighting the answer
rb.setBackgroundColor(Color.parseColor("#00ff00"));
break;
}
}
}
});
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();
I have an application form with one radio group that consists of two radio buttons (self delivery and company delivery). While inserting the details everything is working fine and I can get the text of radio button selected by the code below:
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
if (radioButton.isChecked()) {
OPTIONS=radioButton.getText().toString();
if (OPTIONS.equals("SelfDelivery")) {
deliveryAddress.setVisibility(View.GONE);
deliveryEdit.setVisibility(View.GONE);
} else {
deliveryAddress.setVisibility(View.VISIBLE);
deliveryEdit.setVisibility(View.VISIBLE);
}
}
}
});
When I press the edit button and go to edit page, I want the radio button to also be selected according to the user.
For example: If the user is selecting the company delivery (in the edit page the company delivery should be selected in the radio group).
I do not know how to get the selected id and make the radio group to be selected in the edit page.
// try this
// on edit try this way
RadioButton radioButtonSelf;
RadioButton radioButtonCompany;
radioButtonSelf = (RadioButton) findViewById(R.id.selfradiobuttonid);
radioButtonCompany = (RadioButton) findViewById(R.id.radioButtonCompany);
if(OPTIONS.equals("SelfDelivery")){
radioButtonSelf.setChecked(true);
radioButtonCompany.setChecked(false);
}else{
radioButtonCompany.setChecked(true);
radioButtonSelf.setChecked(false);
}