0

I have a Toolbar widget on my App with 4 icons and I want to change the space size between these icons to be the same.

Form this: current design

To be like this: wanted design

menu.xml

<?xml version="1.0" encoding="utf-8"?>

<item
    android:id="@+id/action_home"
    android:orderInCategory="100"
    android:icon="@drawable/white_home"
    android:title="@string/action_settings"
    app:showAsAction="always" />

<item
    android:id="@+id/action_profile"
    android:orderInCategory="101"
    android:icon="@drawable/white_profile"
    android:title="@string/action_settings"
    app:showAsAction="always" />

<item
    android:id="@+id/action_inbox"
    android:orderInCategory="102"
    android:icon="@drawable/white_inbox"
    android:title="@string/action_settings"
    app:showAsAction="always" />

<item
    android:id="@+id/action_contacts"
    android:orderInCategory="103"
    android:icon="@drawable/white_contacts"
    android:title="@string/action_settings"
    app:showAsAction="always" />

app_bar_main.xml

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@drawable/ic_fab_plus"
    app:backgroundTint="@color/colorPrimary" />

Thanks in advance.

Khalil Jomaa
  • 135
  • 1
  • 2
  • 14

1 Answers1

1

Here is an example of how you can hack your Toolbar to place items in it filling all its width.

Main point is that you need to declare your own Toolbar's inheritor in which you override addView method:

@Override
public void addView(View child, ViewGroup.LayoutParams params) {
    if (child instanceof ActionMenuView) {
        params.width = LayoutParams.MATCH_PARENT;
    }
    super.addView(child, params);
}

UPDATED

If you want to be able to modify space between action items through xml and don't want to extend Toolbar.class and also you use your toolbar as action bar (I meed you calling setSupportActionBar()) then you can change your application theme in styles.xml resource file:

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionButtonStyle">@style/MyActionButtonStyle</item>
</style>

<style name="MyActionButtonStyle" parent="AppBaseTheme">
    <item name="android:minWidth">20dp</item>
    <item name="android:padding">0dp</item>
</style>
Community
  • 1
  • 1
Michael Spitsin
  • 2,539
  • 2
  • 19
  • 29