I have a radio group used to set data class properties. Here's how I've gone so far: I manually set the "bankDestination" view to invisible if proxy_bi_fast is checked. However the "bankDestination" view is still visible.
fun onRadioButtonClicked(view: View) {
if (view is RadioButton) {
val checked = view.isChecked
when (view.getId()){
R.id.proxy_BI_Fast ->
if (checked) {
binding.bankDestination.visibility = View.GONE
}
}
}
}
the related xml code:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="#+id/account_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="#string/account_number"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="#+id/proxy_BI_Fast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="90dp"
android:layout_marginTop="20dp"
android:text="#string/proxy_bi_fast" />
</RadioGroup>
You haven't added onClick method in your proxy_BI_Fast radio button. Try adding that and then check.
It wont' work android:onClick="onRadioButtonClicked" in the RadioButton instead of this you have to use setOnCheckedChangeListener on the RadioGroup as describe below.
binding.rbGroup.setOnCheckedChangeListener { group, checkedId ->
when (checkedId) {
R.id.proxy_BI_Fast -> {
binding.bankDestination.visibility = View.GONE
}
}
}
Related
When I add an on click event in .xml I don't have and error like this
Here is my code
<RadioButton
android:id="#+id/dientich"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickHandler"
android:text="dien tich" />
<RadioButton
android:id="#+id/thetich"
android:onClick=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="the tich" />
</RadioGroup>
Thank you!
I think that the better way is to set a listener in your code
radioGroup.setOnCheckedChangeListener { radioGroup, optionId ->
when (optionId) {
R.id.dientich -> {
// do something when radio button dientich is selected
}
R.id.thetich -> {
// do something when radio button thetich is selected
}
}
}
Can you please refer to this link
<RadioButton android:id="#+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User 1"
android:onClick="onRadioButtonClicked"/>
public void onRadioButtonClicked(View view) {
//your code
}
https://developer.android.com/guide/topics/ui/controls/radiobutton
I have a layout which contains an edittext and two button for increment and decrement of the value in editext. Now I am using include to use this button in my activity as I am having multiple use of that.
eg :-
<include
android:id="#+id/editTextOne"
layout="#layout/include_edittext_with_buttons"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<include
android:id="#+id/editTextTwo"
layout="#layout/include_edittext_with_buttons"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
I can get the onClick events of my increment and decrement buttons but I don't understand how will i catch that the onClick of the particular include has been clicked. ?
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/editText"
android:layout_width="#dimen/_120sdp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="#+id/btnPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/_10sdp"
android:onClick="onClick"
app:layout_constraintBottom_toBottomOf="#id/editText"
app:layout_constraintEnd_toEndOf="#id/editText"
app:layout_constraintTop_toTopOf="#id/editText" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="#+id/btnMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_10sdp"
android:onClick="onClick"
app:layout_constraintBottom_toBottomOf="#id/editText"
app:layout_constraintStart_toStartOf="#id/editText"
app:layout_constraintTop_toTopOf="#id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>
NOTE :- I get that working using binding. editTextOne.btnPlus.setOnClickListener { Toast.makeText(this, "Plus", Toast.LENGTH_SHORT).show() } and similar for the other's but what i want is I can write the plus and minus click listener once and then they update the values of the edittext in which they are included only.
Not sure if that would be possible any better suggestions ?
there is a solution for solving this problem.
first, assign an id for each of the includes.
<include
android:id="#+id/inc1"
layout="#layout/include_layout"
...
/>
<include
android:id="#+id/inc2"
layout="#layout/include_layout"
...
/>
then add oclick in view's xml code.
//include_layout.xml"
<androidx.appcompat.widget.AppCompatImageButton
android:id="#+id/ib"
android:onClick="onClick"
...
/>
after implementing onClick function in your code you can specify which view exactly clicked by checking its parent id.
public void onClick(View view) {
if (view.getId() == R.id.ib) {
int parent_id = ((View) view.getParent()).getId();
if (parent_id == R.id.inc1) {
//clicked view in first include
}
else if (parent_id == R.id.inc2) {
//clicked view in second include
}
}
}
You can get views like that.
// First View
val includedView1 = findViewById<ConstraintLayout>(R.id.editTextOne)
val btnPlus1 = includedView1.findViewById<Button>(R.id.btnPlus)
val btnMinus1 = includedView1.findViewById<Button>(R.id.btnMinus)
// Second View
val includedView2 = findViewById<ConstraintLayout>(R.id.editTextTwo)
val btnPlus2 = includedView2.findViewById<Button>(R.id.btnPlus)
val btnMinus2 = includedView2.findViewById<Button>(R.id.btnMinus)
Please do not say that this is duplicate question, because i search on google and try every answer then also not getting result. In my Listview row contain one TextView and three Radio button.
Problem is
When i click on other side in particular row then Listview setOnItemClickListener work fine but problem is when i click on radio button then setOnItemClickListener not called i don't know why ?
I want that when i click or select any redioButton then call one method, I know that this is possible by adapter but i want radio button click event in listview onItemClick() method.
Sorry for bad english, Thanks in advance and help would be appreciate
xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:padding="5dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:background="#drawable/student_row_background">
<TextView
android:id="#+id/tv_attendance_studentName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_weight=".5"
android:gravity="center_vertical"
android:text="Name"
android:textColor="#color/headerActionbar"
android:textSize="15dp"
android:focusable="false"
android:focusableInTouchMode="false"/>
<RadioGroup
android:id="#+id/rg_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="horizontal"
android:gravity="center|right"
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="21dp"
android:text="P"
android:id="#+id/radioButton_present"
android:textColor="#color/headerActionbar"
android:textStyle="bold"
android:gravity="center"
android:paddingRight="10dp"
android:textSize="12dp"
android:buttonTint="#color/headerActionbar"
android:focusable="false"
android:focusableInTouchMode="false" />
<View
android:layout_width="1dp"
android:layout_height="21dp"
android:background="#color/dialogTextColor"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="21dp"
android:text="A"
android:textColor="#color/headerActionbar"
android:textStyle="bold"
android:paddingRight="10dp"
android:gravity="center"
android:textSize="12dp"
android:id="#+id/radioButton_absent"
android:buttonTint="#color/headerActionbar"
android:focusable="false"
android:focusableInTouchMode="false"/>
<View
android:layout_width="1dp"
android:layout_height="21dp"
android:background="#color/dialogTextColor"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="21dp"
android:text="L"
android:textColor="#color/headerActionbar"
android:textStyle="bold"
android:paddingRight="10dp"
android:gravity="center"
android:textSize="12dp"
android:id="#+id/radioButton_leave"
android:buttonTint="#color/headerActionbar"
android:focusable="false"
android:focusableInTouchMode="false"/>
</RadioGroup>
</LinearLayout>
</RelativeLayout>
This is java file
lv_studentList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
RadioButton radioButton_present = (RadioButton)view.findViewById(R.id.radioButton_present);
RadioButton radioButton_absent = (RadioButton)view.findViewById(R.id.radioButton_absent);
RadioButton radioButton_leave = (RadioButton)view.findViewById(R.id.radioButton_leave);
if (!radioButton_present.isChecked() && !radioButton_absent.isChecked()
&& !radioButton_leave.isChecked())
{
constant.showSnackBar("Please select at least one attendance option");
}
else if(radioButton_present.isChecked())
{
constant.showSnackBar("Student is present");
}
else if (radioButton_absent.isChecked())
{
constant.showSnackBar("Student is absent");
}
else if (radioButton_leave.isChecked())
{
constant.showSnackBar("Student is leave");
}
}
});
Use View parameter of onItemClick() method to find the RadioGroup or RadioButton view. Then use that view as per requirement.
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
RadioGroup group = group = (RadioGroup) view.findViewById(R.id.rg_1);
final RadioButton radioButton_present = (RadioButton)view.findViewById(R.id.radioButton_present);
final RadioButton radioButton_absent = (RadioButton)view.findViewById(R.id.radioButton_absent);
final RadioButton radioButton_leave = (RadioButton)view.findViewById(R.id.radioButton_leave);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (!radioButton_present.isChecked() && !radioButton_absent.isChecked()
&& !radioButton_leave.isChecked())
{
Toast.makeText(MainActivity.this,"Please select at least one attendance option",Toast.LENGTH_LONG).show();
}
else if(radioButton_present.isChecked())
{
Toast.makeText(MainActivity.this,"Student is present",Toast.LENGTH_LONG).show();
}
else if (radioButton_absent.isChecked())
{
Toast.makeText(MainActivity.this,"Student is absent",Toast.LENGTH_LONG).show();
}
else if (radioButton_leave.isChecked())
{
Toast.makeText(MainActivity.this,"Student is leave",Toast.LENGTH_LONG).show();
}
}
});
}
I have a list view with a text and 3 radio button. If i select any radio button, and scroll the list the radio button gets un-selected. I am maintaining the ID of items for which radio button is selected and in adapter Getview setting the required radio button to be selected. On debugging, it's executing statement setChecked(true) but the radio button is still unchecked. I tried setSelected(true) and setting through radio group ((RadioButton)holder.rg.getChildAt(2)).setChecked(true); but nothing seem to be working.
XML:
<TextView
android:id="#+id/refill_product_name"
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="#string/hello_world"
android:textColor="#android:color/black"
android:textStyle="normal"
android:textSize="#dimen/item_sub_heading"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"/>
<RadioGroup
android:id="#+id/refill_product_rg"
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<RadioButton
android:id="#+id/refill_product_regular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/regular"
android:textColor="#android:color/black"
android:textStyle="bold"
android:buttonTint="#color/primary"/>
<RadioButton
android:id="#+id/refill_product_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/small"
android:textColor="#android:color/black"
android:textStyle="bold"
android:layout_marginLeft="#dimen/radio_btn_margin"
android:buttonTint="#color/primary"/>
<RadioButton
android:id="#+id/refill_product_extra_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/extra_small"
android:textColor="#android:color/black"
android:textStyle="bold"
android:layout_marginLeft="#dimen/radio_btn_margin"
android:buttonTint="#color/primary"/>
</RadioGroup>
Adapter Code:
private class ViewHolder {
TextView productName;
RadioButton regularBtn, smallBtn, extraSmallBtn;
RadioGroup rg;
}
GetView Method in Adapter
holder.rg.clearCheck();
if (mAppSession.getSelRefillProducts() != null) {
for (RefillProduct pr : mAppSession.getSelRefillProducts()) {
if (pr.getProductId() == rf.getProductId()) {
if (pr.getSize().equals("R")) {
((RadioButton)holder.rg.getChildAt(0)).setChecked(true);
} else if (pr.getSize().equals("S")) {
holder.smallBtn.setSelected(true);
} else if (pr.getSize().equals("XS")) {
((RadioButton)holder.rg.getChildAt(2)).setChecked(true);
}
}
}
}
I am not sure if I am missing something, but setChecked or setSelected is not working. Please advise.
Try this, before setting radio button to checked, clear all the checks in the radio group:
yourRadioGroup.clearCheck();
yourRadioButton.setChecked(true);
I solved this by removing if (convertView == null) and the else part from getView method in adapter.
else {
holder = (ViewHolder) convertView.getTag();
It is an expected behavior of ListView since it reuses the View for each item. Consider tracking the checkbox checked state using a SparseBoolArray. Check out this link: Getting the state of a checkbox inside of a listview
I've created a radio button in radiogroup, but when I try running the apps all radio button can be selected all the time, and how to set only one radiobutton can be selected at one time?
I'm using Fragment
RadioGroup radioGroup = (RadioGroup) rootView.findViewById(R.id.RGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.Abdominal) {
Toast.makeText(getActivity().getApplicationContext(), "choice: A",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Arm) {
Toast.makeText(getActivity().getApplicationContext(), "choice: B",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Back){
Toast.makeText(getActivity().getApplicationContext(), "choice: C",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Chest){
Toast.makeText(getActivity().getApplicationContext(), "choice: D",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Leg){
Toast.makeText(getActivity().getApplicationContext(), "choice: E",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Shoulder){
Toast.makeText(getActivity().getApplicationContext(), "choice: F",
Toast.LENGTH_SHORT).show();
}
}
});
here my xml code for RG and RB
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/RGroup">
<TableRow android:weightSum="1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abdominal"
android:id="#+id/Abdominal"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arm"
android:id="#+id/Arm"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="#+id/Back" />
</TableRow>
<TableRow>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chest"
android:id="#+id/Chest"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leg"
android:id="#+id/Leg"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shoulder"
android:id="#+id/Shoulder"/>
</TableRow>
</RadioGroup>
EDITED 1 : Answer :
If you dont want radio button can be selected in one time, so dont use Tablerow
It's not working because of TableRow inside RadioGroup. All RadioButtons are not grouped together because of TableRow between them.
RadioButton should be the direct child of RadioGroup, Otherwise grouping does not work.
Just change your code like this it will work :
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/RGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abdominal"
android:id="#+id/Abdominal"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arm"
android:id="#+id/Arm"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="#+id/Back" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chest"
android:id="#+id/Chest"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leg"
android:id="#+id/Leg"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shoulder"
android:id="#+id/Shoulder"/>
</RadioGroup>
Hope this helps. :)
I have noticed that single selection does not work without setting id to radio buttons.
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/expenseRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/expense" />
<RadioButton
android:id="#+id/incomeRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/income" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:id="#+id/radioGroup">
<RadioButton
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:text="Debit"
android:id="#+id/rDebit"
android:checked="false"
/>
<RadioButton
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:text="Credit"
android:id="#+id/rCredit"
android:checked="false" />
</RadioGroup>
And in java file
RadioGroup radioGroup;
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
And when to do something
if (radioGroup.getCheckedRadioButtonId() == R.id.rCredit)
{
// do something
}
I encountered the same issue and found where I was making mistakes, i'm describing it below:
If you are using RadioButtons in RadioGroup, then RadioButtons should be Direct Child of RadioGroup.
Check first condition, if it is correct then give id to RatioButton and run project, It will solve the problem automatically.
Simple way. onclick of radio button. do code as per below.
public void clearRadioChecked() {
rdopa.setChecked(false);
rdopb.setChecked(false);
rdopc.setChecked(false);
rdopd.setChecked(false);
}
if you wann select rdopa then on click of rdopa do as below.
clearRadioChecked()
rdopa.setChecked(true);
You can use android:checkedButton attribute on RadioGroup, providing the id of the RadioButton you want to be checked initially and selecting another RadioButton will clear the previous selection.
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="#+id/rbNo"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:text="Yes" />
<RadioButton
android:id="#+id/rbNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</RadioGroup>
You can create your own RadioGroup, that is able to find all RadioButtons that are nested in your group regardless of direct child or not. Below the code of my NestedRadioGroup. Using this instead of your RadioGroup in your xml file should do the trick.
public class NestedRadioGroup extends LinearLayout {
private SparseArray<RadioButton> radioButtons;
private int checkedId;
public NestedRadioGroup(Context context) {
this(context, null);
}
public NestedRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
radioButtons = new SparseArray<>();
checkedId = -1;
}
#Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
findRadioButtons(child);
}
private void findRadioButtons(View child) {
if (child instanceof RadioButton) {
RadioButton newButton = (RadioButton) child;
newButton.setId(radioButtons.size());
newButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
if (checkedId != -1) {
radioButtons.get(checkedId).setChecked(false);
}
radioButtons.get(buttonView.getId()).setChecked(true);
checkedId = buttonView.getId();
}
});
radioButtons.put(newButton.getId(), newButton);
} else if (child instanceof ViewGroup) {
ViewGroup group = (ViewGroup) child;
for (int i = 0; i < group.getChildCount(); i++) {
this.findRadioButtons(group.getChildAt(i));
}
}
}
}
Any ideas to make the code cleaner?
I hope it works out for you :)
We can use any layout like LinearLayout, RelativeLayout, ConstraintLayout or GridLayout as RadioGroup using the following method in Kotlin.
For the following example I used ConstraintLayout to get fixed width and height for RadioButton.
layout.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/rbYellow"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space_2x"
android:layout_marginEnd="#dimen/space"
android:gravity="center"
android:text="Yellow"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#+id/rbOrange"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="#+id/rbOrange"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space"
android:layout_marginEnd="#dimen/space"
android:gravity="center"
android:text="Orange"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#id/rbYellow"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#+id/rbRed"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/rbYellow"
app:layout_constraintTop_toTopOf="#id/rbYellow" />
<RadioButton
android:id="#+id/rbRed"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space"
android:layout_marginEnd="#dimen/space"
android:gravity="center"
android:text="Red"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#id/rbYellow"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#+id/rbGreen"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/rbOrange"
app:layout_constraintTop_toTopOf="#id/rbYellow" />
<RadioButton
android:id="#+id/rbGreen"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space"
android:layout_marginEnd="#dimen/space_2x"
android:gravity="center"
android:text="Green"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#id/rbYellow"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/rbRed"
app:layout_constraintTop_toTopOf="#id/rbYellow" />
</androidx.constraintlayout.widget.ConstraintLayout>
Kotlin code
radioGroup.children.forEach { aBtn ->
(aBtn as RadioButton).setOnCheckedChangeListener { button, isChecked ->
if(isChecked) {
radioGroup.children.forEach { bBtn ->
if(bBtn.id != button.id) {
(bBtn as RadioButton).isChecked = false
}
}
}
}
}