Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MaterialButtonToggleGroup child's MaterialButton layout_marginStart, layout_marginEnd not working

If i am trying to add margin with layout_marginStart or layout_marginEnd but there is no effect on UI. I am not sure why layout_marginStart, layout_marginEnd not working with MaterialButton when i add them as the child of MaterialButtonToggleGroup

enter image description here

 <com.google.android.material.button.MaterialButtonToggleGroup
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="@dimen/ten_dp"
                            app:singleSelection="true">

                            <com.google.android.material.button.MaterialButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginStart="@dimen/twentY"
                                app:icon="@drawable/ic_directions_walk_black_24dp" />

                            <com.google.android.material.button.MaterialButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginStart="@dimen/ten_dp"

                                app:icon="@drawable/ic_directions_car_black_24dp" />

                            <com.google.android.material.button.MaterialButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"

                                android:layout_marginStart="@dimen/ten_dp"
                                app:icon="@drawable/ic_directions_bus_black_24dp" />
                        </com.google.android.material.button.MaterialButtonToggleGroup>
like image 546
Black4Guy Avatar asked Dec 22 '22 18:12

Black4Guy


2 Answers

After hours straggling, I finally found clean solution for this. we can easily use android:insetRight or android:insetLeft to add spacing to the Material buttons.

Here is a code sample of 10dp (5dp + 5dp) space between each button:

<com.google.android.material.button.MaterialButtonToggleGroup
  android:id="@+id/toggleGroup"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <com.google.android.material.button.MaterialButton
    android:id="@+id/button1"
    android:insetRight="5dp"
    ... />

  <com.google.android.material.button.MaterialButton
    android:id="@+id/button2"
    android:insetRight="5dp"
    android:insetLeft="5dp"
    ... />

  <com.google.android.material.button.MaterialButton
    android:id="@+id/button3"
    android:insetLeft="5dp"
    ... />

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

Hope this helps you too :)

like image 60
Behzad Bahmanyar Avatar answered Jan 13 '23 21:01

Behzad Bahmanyar


In order to cohesively group multiple buttons together, MaterialButtonToggleGroup overrides the start and end margins of any children added to this layout such that child buttons are placed directly adjacent to one another.

Sorry, layout_marginStart and layout_marginEnd will not work here. Read official guideline about MaterialButtonToggleGroup.

You can try with ToggleButton.

                  <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="horizontal">

                        <ToggleButton
                            android:id="@+id/tJava"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textOff="JAVA"
                            android:textOn="JAVA" />

                       <ToggleButton
                            android:id="@+id/tKotlin"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textOff="KOTLIN"
                            android:textOn="KOTLIN" />

                </LinearLayout>

Then Java class onCreate() section

    tJava=findViewById(R.id.tJava);
    tKotlin=findViewById(R.id.tKotlin);


    tJava.setOnCheckedChangeListener(changeChecker);
    tKotlin.setOnCheckedChangeListener(changeChecker);

Then changeChecker function outside of onCreate()

CompoundButton.OnCheckedChangeListener changeChecker = new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                if (buttonView == tJava) {

                    tKotlin.setChecked(false);

                }
                if (buttonView == tKotlin) {

                    tJava.setChecked(false);

                }

        }
    };
like image 21
IntelliJ Amiya Avatar answered Jan 13 '23 20:01

IntelliJ Amiya