How could i change the state of a radiobutton in a radiogroup? - android

How could I change the state of a radio button in a radio group?
I get the value Male or Female from Firebase(stored before out of the radio group) and fill again the Layout. Now I have to convert the Male information to the radio button of the radio group
var rg = radioGruppe
rg!!.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener
{
group, checkedId ->
Log.i("test", "The ID is: ${group.checkedRadioButtonId}")
})
The ID is: 2131230874
But what have I to do now?

found it by myself:
rbMale = mView?.findViewById((R.id.rb_male))
rbFemale = mView?.findViewById((R.id.rb_female))
if(Gender == "Woman") {
radioGruppe!!.check(rbFemale!!.id)
} else {
radioGruppe!!.check(rbMale!!.id)
}

Related

Android Anko Radio Group with default value?

I'm writing an android app with Kotlin and Anko, and I'm trying to give the radio buttons a default value, but when I do the value never gets unselected. How do you set a default value that is changed when you click elsewhere?
radioGroup {
orientation = LinearLayout.HORIZONTAL
bottomPadding = dip(8)
val radBut = { value: Int ->
radioButton {
text = "$value"
onClick { Store.playerCount = value }
if (Store.playerCount == value) {
isChecked = true
}
rightPadding = dip(8)
}
}
(2..5).map { radBut(it) }
}
Don't change isChecked property of radio button directly instead let radio group handle it by giving radio button id to radio group check(int id) method, please check this link for more details https://developer.android.com/reference/android/widget/RadioGroup#check(int)
and here's the code for you
verticalLayout {
orientation = LinearLayout.VERTICAL
radioGroup {
orientation = LinearLayout.HORIZONTAL
padding = dip(8)
val button1 = radioButton {
text = "Button 1"
}
radioButton {
text = "Button 2"
}
radioButton {
text = "Button 3"
}
// Checking button 1 as default
check(button1.id)
}
}

Unselect radiobutton not working when adding buttons programmatically

I add radiobuttons in code:
options.map {
val radioOption = RadioButton(infoLabel.context)
radioOption.text = it
radioOption.isClickable = isReadOnly
if (value.contains(it))
radioOption.isChecked = true
infoRadioGroup.addView(radioOption)
}
In options I have two elements. On start on of them is selected.When I start the app and try to select the second one I get two selected radiobuttons. The first one is still checked. Any ideas?
This can only happen when the Id of both RadioButton are same. Make sure the ID of every RadioButton is unique.
I resolve my problem:
radioOption.setOnCheckedChangeListener { buttonView, isChecked ->
val count = infoRadioGroup.childCount
if (isChecked)
for (i in 0 until count) {
val radio = infoRadioGroup.getChildAt(i)
if (radio is RadioButton) {
radio.isChecked = radio.text == buttonView.text
}
}
}

save edit text value on changing radio button

I have two radio buttons in a radio group and three text edit fields in one layout.
Now when I switch radio button I would able to save data of edit text for pervious selected radio button and vice versa
Please help me.
You can do using radio group.
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
if(checkedId == R.id.radiobutton1)
{
//get data from edittext and save then clear edit text
}else
{
//get data from edittext and save then clear edit text
}
}
});

How to get radio Button values number

I am making an application that has list and Radio Buttons. My Layout contains of the following : a list , A button .
The List contains of Radio buttons. Two radio Buttons. After checking the buttons. I want to make a loop that reads the values of how many are in Button one and how many in Button Two.
This is how I have done so far.
ListViewStudentEditAbsence country ;
for(int i=0;i<listViewStudentNames.size();i++) {
country = listViewStudentNames.get(i);
if (country.getAbsence()==1) {
Absence[i] = "1";
} else {
Absence[i] ="0";
}
}
But It doesn't seem to work.
In your CustomAdapter getView() method
int value=0; //globally
RadioGroup radioGroup = (RadioGroup) convertView.findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
if (checkedId == R.id.rbutton1) {
value=10; //a's value
} else if (checkedId == R.id.rbutton2) {
value=20; //b's value
}
}
});
https://stackoverflow.com/questions/11194515/android-get-value-of-the-selected-radio-button
Android getting value from selected radiobutton
Hope it works. This two links give you proper knowledge about fetching value.

storing radio button value in sqlite database

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

Categories

Resources