Skip to content Skip to sidebar Skip to footer

Action Buttons Do Not Cover The Entire Toolbar

I have used a Toolbar to display four action buttons. When I inflate the toolbar with menu, the action buttons are displayed towards one side as shown in the picture : How can I

Solution 1:

You can add LinearLayout to toolbar with parameters

 <LinearLayout android:layout_height="?attr/actionBarSize"
 android:layout_width="match_parent"
 android:orientation="horizontaly"
 android:weightSum="4">  </LinearLayout>

then for buttons

<Button android:layout_height="match_parent" 
        android:layout_width="0dp"
        android:layout_weight="1"></Button>

and every button will take 1/4 of toolbar space.

so to sum up, inside toolbar you will have:

<LinearLayout android:layout_height="?attr/actionBarSize"
     android:layout_width="match_parent"
     android:orientation="horizontaly"
     android:weightSum="4"> 
<Button android:layout_height="match_parent" 
            android:layout_width="0dp"
            android:layout_weight="1"></Button>
<Button android:layout_height="match_parent" 
            android:layout_width="0dp"
            android:layout_weight="1"></Button>
<Button android:layout_height="match_parent" 
            android:layout_width="0dp"
            android:layout_weight="1"></Button>
<Button android:layout_height="match_parent" 
            android:layout_width="0dp"
            android:layout_weight="1"></Button>
 </LinearLayout>

I wrote it from head, so I not tested this code, but I hope it will be helpful for you.


Solution 2:

If you have only four options for menu then you can think of not using Toolbar . Instead you can use a LinearLayout directly in AppBarlayout as it can hold other views then toolbar too. Like

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/app_background">

    <LinearLayout
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:orientation="horizontaly"
        android:weightSum="4">

        <Button
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"></Button>

        <Button
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"></Button>

        <Button
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"></Button>

        <Button
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"></Button>
    </LinearLayout>
    </android.support.design.widget.AppBarLayout>

I used the same layout of MyWay. Thanks MyWay.


Post a Comment for "Action Buttons Do Not Cover The Entire Toolbar"