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)
}
}
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 want to toggle textView on click.
val targetView = findViewById<TextView>(R.id.targetText)
targetView.text = 'example'
targetView.setOnClickListener {
if (it.visibility == View.VISIBLE) {
it.visibility = View.INVISIBLE
} else {
it.visibility = View.VISIBLE
}
}
But once I clicked and visibility of text was changed to invisible, then I can not click this again.
Suggestion: How about creating a view behind the text and make use of the view for toggle triggering instead of text.
i.e. onViewClicked { toggleTextVisibility() }
Store the text of the textview in a temporary string. Instead of toggling the visibility, set the text to null and then back to what it was using the temp variable.
String final tempText = "your text"
then
targetView.setOnClickListener(new View.OnClickListener) {
#Override
public void onClick(View view){
if (view.getText() == null) {
view.setText(tempText);
} else {
view.setText(" ");
}
});
}
just make sure your textview on the layout properties has a set minWidth and not wrap_content as it will disappear when you set the text to null
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
}
}
}
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