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
}
}
}
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.
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)
}
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)
}
}
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.
I have a radio group which I do not want to user to be able to select any of the buttons until a particular checkbox is selected within my app. If the checkbox is unticked then this disables the radio-group. How do I go about doing this.
The real trick is to loop through all children view (in this case: CheckBox) and call it's setEnabled(boolean)
Something like this should do the trick:
//initialize the controls
final RadioGroup rg1 = (RadioGroup)findViewById(R.id.radioGroup1);
CheckBox ck1 = (CheckBox)findViewById(R.id.checkBox1);
//set setOnCheckedChangeListener()
ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton checkBox, boolean checked) {
//basically, since we will set enabled state to whatever state the checkbox is
//therefore, we will only have to setEnabled(checked)
for(int i = 0; i < rg1.getChildCount(); i++){
((RadioButton)rg1.getChildAt(i)).setEnabled(checked);
}
}
});
//set default to false
for(int i = 0; i < rg1.getChildCount(); i++){
((RadioButton)rg1.getChildAt(i)).setEnabled(false);
}
If you have just a few radio buttons, better way would be to setClickable(false) for all children
radiobutton1.setClickable(false);
radiobutton2.setClickable(false);
radiobutton3.setClickable(false);
RadioGroup cannot be disabled directly, we have to loop through the radio button and setEnabled as false.
// To disable the Radio Buttons of radio group.
for (int i = 0; i < radioGroup.getChildCount(); i++) {
radioUser.getChildAt(i).setEnabled(false);
}
If you want to implement the same for Kotlin, Here's my code->
where rgNotificationType is the radio group name.
for (i in 0 until rgNotificationType.childCount) {
(rgNotificationType.getChildAt(i)).isEnabled = false
}
You can use the onCheckedChangeListener on your CheckBox and use the method setEnabled on your RadioGroup.
Best wishes,
Tim
By using kolin's extensions:
fun RadioGroup.setChildrenEnable(enable: Boolean) {
for (i in 0 until this.childCount) {
this.getChildAt(i).isEnabled = enable
}
}
And in your code you can just call the function like this:
radioGroup.setChildrenEnable(false)
Kotlin Solution
for (index in 0..radio.childCount - 1)
radio.getChildAt(index).isEnabled = false
Take actions according to the state of the checkbox and set the radiogroup accordingly.
Assuming that you have a radio-group named radiogroup you can enable or disable the radiogroup by
radiogroup.setEnabled(true);
Add a OnCheckedChangeListener() to your checkbox.