Birrel
Birrel

Reputation: 4834

Android, Position Menu Items in Toolbar Layout

I have two toolbars (top and bottom), as shown:

Toolbars

The two toolbars are defined in their own xml files, nothing special:

top_toolbar.xlm

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

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

bottom_toolbar.xlm

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorBottomTrans"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

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

And added to the main activity:

activity_main.xlm

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relLayout"
    android:background="@android:color/white">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"></include>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:layout_below="@+id/tool_bar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_margin="20dp" />

    <include
        android:id="@+id/tool_bar_bottom"
        layout="@layout/tool_bar_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"></include>

</RelativeLayout>

Where the items in each toolbar are inflated from menu files.

MainActivity

private void initToolbars() {
    topToolbar = (Toolbar) findViewById(R.id.tool_bar);
    bottomToolbar = (Toolbar) findViewById(R.id.tool_bar_bottom);

    setSupportActionBar(topToolbar);

    bottomToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_item_1:
                    break;
            }
            return true;
        }
    });
    bottomToolbar.inflateMenu(R.menu.menu_main);
    bottomToolbar.getBackground().setAlpha(125);
}

But what I would like to do is move "Item 1" in the bottom toolbar to the left side, as shown:

Split toolbar items

Is there a way to do this?

Thanks in advance.

Upvotes: 2

Views: 9764

Answers (1)

Birrel
Birrel

Reputation: 4834

The easiest solution I came up with was the to change the bottom toolbar layout:

bottom_toolbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorBottomTrans"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lin"
        android:orientation="horizontal"
        android:layout_gravity="left"
        android:gravity="center_vertical">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@mipmap/ic_launcher"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ITEM 1"
            android:textStyle="bold"
            android:textSize="12dp"
            android:id="@+id/toolbar_title"
            android:layout_marginLeft="10dp"
            android:textColor="@android:color/white" />
    </LinearLayout>

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

And then when I initialize the toolbar, to add:

LinearLayout linearLayout = (LinearLayout) bottomToolbar.findViewById(R.id.lin);
linearLayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ... do stuff ...
    }
});

Left-side button

Just need to remember to remove "Item 1" from the bottom_toolbar_menu.xml. And can create a drawable file to visually show click events.

Upvotes: 1

Related Questions