Android RadioGroup Get Checked RadioButton Index (Position) in Two-way DataBinding - android

Following is my current solution:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onCheckedChanged="#{(group, buttonId) -> student.setGenderIndex(group.indexOfChild(group.findViewById(buttonId)))}"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:checked="#{student.genderIndex == 0}"
android:text="Male" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="#{student.genderIndex == 1}"
android:text="Female" />
</RadioGroup>
This works fine, but I'm wondering if there is a better way to do this. And then I find out that there is a built-in two-way attribute for RadioGroup: android:checkedButton (from Official Documentation), however it is the id not the index I want:
public static final int checkedButton
The id of the child radio button that should be checked by default within this radio group.
May be an integer value, such as "100".
Constant Value: 16843080 (0x01010148)
I still try (to make my binding expression shorter) but doesn't work:
android:onCheckedChanged="#{(group, buttonId) -> student.setGenderIndex(group.indexOfChild(group.checkedButton))}"
Says "Could not find accessor android.widget.RadioGroup.checkedButton"

Add onCheckedChanged like this
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onCheckedChanged="#{vm.onSplitTypeChanged}">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/email" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/sms" />
</RadioGroup>
Then in your view model implement onSplitTypeChanged like this
fun onSplitTypeChanged(radioGroup: RadioGroup?, id: Int) {
val checked = radioGroup?.findViewById<RadioButton>(id)
val index = radioGroup?.indexOfChild(checked)
}

Related

How to check radiobuttons using kotlin code while using MVVM architecture?

I am using MVVM architecture in my app and there are 4 radiobuttons in a 2*2 grid inside a radiogroup but the the problem is oncheckedChanged is not getting called in viewmodel class, here's the xml code:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_10sdp"
android:checkedButton="#id/singleRadioBtn"
android:onCheckedChanged="#{viweModel::selectOption}"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="0dp"
android:layout_height="#dimen/_30sdp"
android:layout_weight="1"
></RadioButton>
<Space
android:layout_width="#dimen/_8sdp"
android:layout_height="match_parent" />
<RadioButton
android:layout_width="0dp"
android:layout_height="#dimen/_30sdp"
android:layout_weight="1"
></RadioButton>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_8sdp"
android:orientation="horizontal">
<RadioButton
android:layout_width="0dp"
android:layout_height="#dimen/_30sdp"
android:layout_weight="1"
></RadioButton>
<Space
android:layout_width="#dimen/_8sdp"
android:layout_height="match_parent" />
<RadioButton
android:layout_width="0dp"
android:layout_height="#dimen/_30sdp"
android:layout_weight="1"
></RadioButton>
</LinearLayout>
</RadioGroup>
and in viewmodel:
fun selectOption(radioGroup: RadioGroup, id: Int)
{
radioGroup.check(id)
}
But the above function is not getting called. So what I am doing wrong here? Please help!
I think your problem is not with DataBinding - it's rather because you put LinearLayouts inside RadioGroup. Since Radiogroup is a subclass of LinearLayout itself it makes a mess, so in general you'll have to do something additional in code to make it work (but then DataBinding in the case is not so useful), you can look through discussion here.
If you try to place RadioButtons just inside RadioGroup (without nested LinearLayouts) I guess your method should be called.
As a workaround you could bind another callback - each RadioButton's onClick (setting index of RadioGroup there explicitly):
//...........
<RadioButton
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:onClick="#{(v) -> viewModel.selectOption(1)}"
></RadioButton>
<Space
android:layout_width="8dp"
android:layout_height="match_parent" />
<RadioButton
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:onClick="#{(v) -> viewModel.selectOption(2)}"
></RadioButton>
//...............
UPDATE
One of the choices - to make it work according to your current model:
To give id to Radiogroup (so it can be used in binding expression)
To give ids to RadioButtons
To change a little bit method in ViewModel
XML
<RadioGroup
android:layout_width="match_parent"
android:id="#+id/radioGroup"
.......>
<RadioButton
android:layout_width="0dp"
android:id="#+id/button1"
android:onClick="#{(v) -> viewModel.selectOption(radioGroup,v)}" // <- v - is your button
ViewModel
fun selectOption(radioGroup: RadioGroup, radioButton: View)
{
radioGroup.check(radioButton.id)
}

How to disable radiobutton's text from clickable feature?

I need to select radio button while touching only the icon of a radio button. By default when i touch on the radio button's text, the radio button also get checked. How can i disable the feature?
<RadioGroup
android:id="#+id/optionGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton"
android:textColor="#color/optionText"
android:textSize="#dimen/text_exam"
android:gravity="top" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
android:textColor="#color/optionText"
android:textSize="#dimen/text_exam"
android:gravity="top" />
<RadioButton
android:id="#+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
android:textColor="#color/optionText"
android:textSize="#dimen/text_exam"
android:gravity="top" />
<RadioButton
android:id="#+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
android:textColor="#color/optionText"
android:textSize="#dimen/text_exam"
android:gravity="top" />
</RadioGroup>
To answer your question, you can't do it using the default radiobutton class.
The android-radiobutton is an object of the class which actually makes the radiobutton itself. This class sets the properties of the RadioButton, and, to a certain extent, you cannot change these properties. You can, however, make it unclickable, change the color, set the text, however, you cannot change what portion of the button is clickable. To do that, you must make your own class of a RadioButton, and extend it to the class that you are using.
Only way is by making your own class. Let me know if this answered your question.
:)
Try this simple hack
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"/>
</LinearLayout>

Change layout_bellow attribute field to another field programmatically

Given the following code:
<RelativeLayout
android:id="#+id/rel"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textPersonalInfoEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:hint="Edit to set your personal info"
android:textSize="16sp"
android:visibility="gone" />
<EditText
android:id="#+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="15dp"
android:hint="Name"
android:visibility="gone"
android:textSize="16sp" />
<Button
android:id="#+id/insertGameId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editName"
android:layout_marginTop="20dp"
android:text="Insert" />
</RelativeLayout>
The TextView and the EditText are visible depending the user interaction and both will never be visible at the same time. One per time only!
The problem is the third component(Button) need to change the attribute android:layout_below depending on which component is visible.
I need to this programmatically.
Any help?
Use Relativelayout for your textview and edittext. And relativelayout's height:wrap-content
Then give an id to your relativelayout
and then add this line your xml file for your button
<Button
android:layout_below="#id/RealtiveLayoutid" />
Thats all
First, use a ToggleButton instead of Button. It can have all the attributes that you assigned to it.
Next add this to the ToggleButton:
<ToggleButton
....
android:textOn="#string/ifTextView"
android:textOff="#string/ifTextBox"
android:onClick="onToggleClicked"
/>
Then this for the activity
public void onToggleClicked(View v) {
boolean on = ((ToggleButton) v).isChecked();
if(on) {
theTextView.setVisibility(View.VISIBLE);
theTextBox.setVisibility(View.INVISIBLE);
} else {
theTextBox.setVisibility(View.VISIBLE);
theTextView.setVisibility(View.INVISIBLE);
}
}
Remember to import the required classes.

Only one radio button selected at a time

I have two radio button in a radio group. I also have 2 androd:button checkbox- for when the radio button is deselected and checkbox_v for when he user selects the checkbox. I also implemnted that a method onRadioButtonClick in order to make sure that only one radio button had drawable:checkbox and the other has checkbox_v . How can I implemnt onRadioClick to do this? any idea?
public void onRadioButtonClick(View v)
{
RadioButton button = (RadioButton) v;
boolean checkBox1Selected;
boolean checkBox2Selected = false;
Toast.makeText(MainActivity.this,
button.getId()+ " was chosen."+R.id.wificheckBox,
Toast.LENGTH_SHORT).show();
if ( button.getId() ==R.id.wificheckBox) {
// Toggle status of checkbox selection
checkBox1Selected = radiowifiButton.isChecked();
// Ensure that other checkboxes are not selected
if (checkBox2Selected) {
radiomobileButton.setChecked(false);
checkBox2Selected = false;
}
else if (button.getId() ==R.id.wifimobilecheckBox) {
// Toggle status of checkbox selection
checkBox2Selected = radiomobileButton.isChecked();
// Ensure that other checkboxes are not selected
if (checkBox1Selected) {
radiowifiButton.setChecked(false);
checkBox1Selected = false;
}
}
}
main xml
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_1"
android:layout_marginLeft="20dp">
<LinearLayout
android:id="#+id/ll_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_1"
android:layout_marginLeft="20dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/wifimobilecheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/button_radio"
android:checked="true"
android:onClick="onRadioButtonClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/wificheckBox"
android:layout_toRightOf="#+id/wificheckBox"
android:paddingLeft="15dp"
android:text="WiFi or mobile network"
android:textColor="#333333"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ll_2"
android:paddingTop="20dp"
android:layout_marginLeft="20dp"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/wificheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/button_radio"
android:onClick="onRadioButtonClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/wificheckBox"
android:layout_toRightOf="#+id/wificheckBox"
android:paddingLeft="15dp"
android:text="WiFi "
android:textColor="#333333"
android:textSize="20dp" />
</LinearLayout>
</RadioGroup>
Drawabe- button_radio
<item android:state_checked="true"android:state_pressed="false" android:drawable="#drawable/checkbox_v"/><item android:state_checked="false"android:state_pressed="false"'android:drawable="#drawable/checkbox"/>
If they are in a radiogroup, only one can be selected at a time. If you want both to be able to be selected, remove them from the radiogroup.
As the top voted answer didn't work for me as for many others. I am sharing the method that i used and its working perfectly.
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="#+id/rb_female"
android:orientation="horizontal">
<RadioButton
android:id="#+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:button="#drawable/selector_radio_button"
android:text="Female" />
<RadioButton
android:id="#+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/selector_radio_button"
android:text="Male" />
</RadioGroup>
Setting the android:checkedButton="#+id/rb_female" worked for me in RadioGroup.
You should remove your onRadioButtonClick method and add OnCheckedChangeListener to your radiogroup. http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html
I guess by implementing your own click handler your are overwriting some functionality of the radiogroup.
Your requirement is not clear. Would you mind to make it simpler. I tried my best to understand your requirements and these are the findings given below :(.
A radio button can have text. So you don't have to add another TextView in your layout. Please remove them and add a simple android:text="blah.. blah.." to your radiobutton.
With my understanding you have two doubts.
How to customize RadioButtons with custom drawables??
Checkout the answer here.
How to deselect the previously selected radiobutton??
The truth is you don't have to do it manually as you are keeping those radiobuttons into a single radiogroup.
Hope this will help you.
To check/uncheck radio buttons not part of a RadioGroup,
add a method in the android GUI designer under the
RadioButton Properties 'onClick' (In this example radio1_onClick).
In your activity.xml under the xml for the RadioButton
RadioButton
[blah blah blah]
android:id="#+id/radio1"
android:onClick="radio1_onClick"
In your activity_main.xml you would write the following method.
private void radio1_onClick(View v)
{
CompoundButton b=(CompoundButton)findViewById(R.id.radio1);
b.setChecked(true); /* light the button */
//b.setChecked(false); /* unlight the button */
}

How to change the position of RadioGroup

I am making some exercise to get used to Android dev.
So I'm trying to change the position of a Radiogroup in the layout by having the user press buttons
here is the code I'm using, it actually changes the position, but not by much, it barely moves form the original position when I hit center or right.
else if(group==gravedad)
{
if(checkedId==R.id.izq)
{
gravedad.setGravity(Gravity.LEFT);
}else if(checkedId==R.id.ctr)
{
gravedad.setGravity(Gravity.CENTER);
}else if(checkedId==R.id.der)
{
gravedad.setGravity(Gravity.RIGHT);
}
}
Maybe i messed up the xml...here is how I have set it up
<RadioGroup
android:id="#+id/gravedad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip" >
<RadioButton
android:id="#+id/izq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Izquierda" />
<RadioButton
android:id="#+id/ctr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centro" />
<RadioButton
android:id="#+id/der"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Derecha" />
</RadioGroup>
if your api is >11 then you can do like this to change the position of radio group..
RadioGroup r=(RadioGroup)findViewById(R.id.radioGroup1);
r.setX(30);
r.setY(30);

Categories

Resources