J.Ponce
J.Ponce

Reputation: 11

How to align items in an android-toolbar?

How can I align items of a toolbar to the left, middle and right? Everytime I inflate the menu from my toolbar_menu.xml it loads the icons to the far right. How can I put them in the order I want? I am using an standalone toolbar at the bottom of the screen.

I Have an AXML which holds the toolbar.

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbarMenuMain"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="#CC2827"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:layout_alignParentBottom="true"/>

My Main activity:

    mToolbarMenu = FindViewById<SupportToolbar> (Resource.Id.toolbarMenuMain);        
    mToolbarMenu.InflateMenu(Resource.Menu.toolbarMenu);
    mToolbarMenu.MenuItemClick += mToolbarMenu_MenuItemClick;

And the toolbar_menu.xml.

<?xml version="1.0" encoding="utf-8" ?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:myapp="http://schemas.android.com/apk/res-auto">
       <item android:id="@+id/action_home"
             android:icon="@drawable/ic_directions_car_white_24dp"
             android:title="Home"
             android:layout_gravity="left"
             myapp:showAsAction="always"/>
       <item android:id="@+id/action_Shop"
             android:icon="@drawable/ic_store_mall_directory_white_24dp"
             android:title="Shop"
             android:layout_gravity="center"
             myapp:showAsAction="always"/>
       <item android:id="@+id/action_Map"
             android:icon="@drawable/ic_map_white_24dp"
             android:title="Maps"
             android:layout_gravity="right"
             myapp:showAsAction="always"/>
    </menu>

Upvotes: 1

Views: 1907

Answers (2)

Archana vishwakarma
Archana vishwakarma

Reputation: 61

If you want to align items of a toolbar to the left, middle and right, you need to create seperate layout for it. because in menus you don't have any option to change the position, they will always come on right of the screen. You can use custom layout and inflate it in the tool bar. please refer - Add custom layout to toolbar

Upvotes: 0

M Karimi
M Karimi

Reputation: 3513

The layoutDirection attribute should be added to the toolbar tag. for examle:

android:layoutDirection="rtl"

Upvotes: 2

Related Questions