1

Why are menu items aligned so strangely on the toolbar?

enter image description here

It behaves even more strangely with smaller toolbar:

enter image description here

I use the following code:

MainActivity.java:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //getMenuInflater().inflate(R.menu.menu_main_right, menu);

    Toolbar toolbar = findViewById(R.id.toolbar);
    ActionMenuView toolbarLeft = toolbar.findViewById(R.id.toolbarLeft);
    getMenuInflater().inflate(R.menu.menu_main_left, toolbarLeft.getMenu());

    return true;
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ActionBar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.v7.widget.ActionMenuView
            android:id="@+id/toolbarLeft"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:gravity="center_vertical|start"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.v7.widget.Toolbar>

</android.support.constraint.ConstraintLayout>

styles.xml:

<resources>

    <style name="ActionBar">
        <item name="android:background">@drawable/toolbar</item>
        <item name="actionMenuTextColor">@color/colorToolbarText</item>
    </style>

</resources>

menu_main_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">

    <item
        android:id="@+id/actionAdd"
        android:title="Add"
        app:showAsAction="always"/>

    <item
        android:id="@+id/actionDetails"
        android:title="Details"
        app:showAsAction="always"/>

    <item
        android:id="@+id/actionLegend"
        android:title="Legend"
        app:showAsAction="always"/>

    <item
        android:id="@+id/actionSettings"
        android:title="Settings"
        app:showAsAction="always"/>

</menu>
Dmitry
  • 14,306
  • 23
  • 105
  • 189
  • Why don't you use OnCreateOptionsMenu? – dejavu89 May 12 '18 at 20:56
  • I use it - see the code. My goal is to align some toolbar menu items to the left (in the posted code I'm trying to align all menu items to the left). – Dmitry May 12 '18 at 21:02
  • Actually, my code is from here: https://stackoverflow.com/questions/29807744/how-can-i-align-android-toolbar-menu-icons-to-the-left-like-in-google-maps-app And it works strangely for me. Left alignment doesn't work. – Dmitry May 12 '18 at 21:03
  • @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.settingsmenu, menu); return true; } – dejavu89 May 12 '18 at 21:11
  • Why do you want to do left alignment. It's anti pattern anti design – dejavu89 May 12 '18 at 21:12
  • I'm implementing custom design that has nothing to do with Android right-aligned menus. – Dmitry May 12 '18 at 21:13
  • From the link you have posted, the answer has slight differences with how you have implemented. For example the oncreatedoptionsmenu method. Try matching it and see if it works? – dejavu89 May 12 '18 at 21:15
  • My `onCreateOptionsMenu` is the same. There is no difference. – Dmitry May 12 '18 at 21:19
  • So, why does it give to the 2 buttons too much space and no space for other buttons? I uploaded even more strange screenshot of the smaller toolbar. – Dmitry May 12 '18 at 22:31

0 Answers0