Skip to content Skip to sidebar Skip to footer

How Do I Make My Toggle Button Act Like A Radio Button?

I have 2 toggle buttons in my code and I want to change the behavior such that when one is selected the other one is not and vice versa. (want it to work like a radio button ).Any

Solution 1:

They both extendCompoundButton so there isn't too much to it. Just put them inside of a RadioGroup and treat them like a RadioButton.

RadioGroup Docs

Not sure what else you need but I do it like this

<RadioGroupandroid:id="@+id/typeGroup"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:weightSum="3"><ToggleButtonandroid:id="@+id/hotBtn"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:layout_marginLeft="40dp"android:layout_marginTop="20dp"android:checked="true"style="@style/HotButton"android:onClick="filterItems"/><ToggleButtonandroid:id="@+id/coldBtn"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"style="@style/ColdButton"android:onClick="filterItems"android:layout_marginLeft="40dp"android:layout_marginRight="40dp"android:layout_marginTop="20dp"/><ToggleButtonandroid:id="@+id/frozenBtn"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:layout_marginRight="40dp"android:layout_marginTop="20dp"style="@style/FrozenButton"android:onClick="filterItems"/></RadioGroup>

Post a Comment for "How Do I Make My Toggle Button Act Like A Radio Button?"