Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Material Button Toggle Group

I'm creating a form in android which asks for gender. To get this input I use Material Button Toggle Group which contains two buttons. I don't know how to know which button is clicked in my activity.java. How to get to know about the selected button in my activity so that i can save the details in different database.

myxml.xml

    <com.google.android.material.button.MaterialButtonToggleGroup
                android:id="@+id/toggleButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:singleSelection="true">
                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Male"
                    android:layout_marginStart="5dp"
                    style="?attr/materialButtonOutlinedStyle"/>
                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Female"
                    android:layout_marginStart="10dp"
                    style="?attr/materialButtonOutlinedStyle"/>

            </com.google.android.material.button.MaterialButtonToggleGroup>

Myactivity.java

    MaterialButtonToggleGroup toggleButton = findViewById(R.id.toggleButton);
    toggleButton.addOnButtonCheckedListener(); 
    // I CAN'T FIND ANY PROPER SOLUTION
like image 714
Asuwathaman R C Avatar asked Jul 12 '26 11:07

Asuwathaman R C


2 Answers

You can use the getCheckedButtonId() method.
Something like:

MaterialButtonToggleGroup materialButtonToggleGroup = 
         findViewById(R.id.toggleButton);
int buttonId = materialButtonToggleGroup.getCheckedButtonId();
MaterialButton button = materialButtonToggleGroup.findViewById(buttonId);

Only if you need a listener you can use the addOnButtonCheckedListener:

materialButtonToggleGroup.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
      @Override
      public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
        if (isChecked) {
          if (checkedId == R.id.button1) {
            //..
          }
        }
  }
});

You have to check the checkedId value but also the isChecked value. The same listener is called when you check a button but also when you unckeck a button.

It means that if you click the button1 the listener is called with isChecked=true and checkedId=1. Then if you click the button2 the listener is called twice. Once with isChecked=false and checkedId=1, once with isChecked=true and checkedId=2.

like image 179
Gabriele Mariotti Avatar answered Jul 14 '26 01:07

Gabriele Mariotti


You can do it like this :

toggleButton.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
            @Override
            public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
                if(group.getCheckedButtonId()==R.id.button1)
                {
                    //Place code related to button1 here
                    Toast.makeText(MainActivity2.this, "Button1 Clicked", Toast.LENGTH_SHORT).show();

                }else if(group.getCheckedButtonId()==R.id.button2) {
                    //Place code related to button 2 here
                    Toast.makeText(MainActivity2.this, "Button2 Clicked", Toast.LENGTH_SHORT).show();
                }

            }
        });
like image 27
AgentP Avatar answered Jul 14 '26 02:07

AgentP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!