Developer
Developer

Reputation: 133

How to right align text of MenuItem in Toolbar

My MenuItem:

menu_setting.xml

How to right align text of MenuItem in Toolbar, Icon in first next text, like this:

enter image description here

code of menu_setting.xml for MenuItem:

    <?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:title="Main Menu"
        app:showAsAction="always"
        android:icon="@drawable/ic_more_vert">

        <menu>

            <item
                android:id="@+id/about_app"
                android:orderInCategory="100"
                android:title="About app"
                android:icon="@drawable/ic_info"
                app:showAsAction="never" />

            <item
                android:id="@+id/contact_us"
                android:orderInCategory="100"
                android:title="Contact us"
                android:icon="@drawable/ic_email"
                app:showAsAction="never" />

        </menu>

    </item>

</menu>

Thanks in advance :)

Upvotes: 1

Views: 3858

Answers (3)

Android Geek
Android Geek

Reputation: 9225

You can use girdle for align text of MenuItem in Toolbar:

dependencies {
compile 'com.lucasurbas:guidelinescompattoolbar:1.0.0'
}

This is how it should look like:

enter image description here

Change your style.xml code like this:

<style name=”Theme.GuidelinesCompat.Light.DarkToolbar” 
parent=”Theme.AppCompat.Light.DarkActionBar”>
 ...
 <!— Toolbar styles -->

 <item name=”toolbarStyle”>@style/Custom.Widget.Toolbar</item>
 <item name=”toolbarNavigationButtonStyle”>
 @style/Custom.Widget.Toolbar.Button.Navigation</item>
 </style>
 <style name=”Custom.Widget.Toolbar” parent=”Widget.AppCompat.Toolbar”>
 <item name=”maxButtonHeight”>48dp</item>
 <item name=”android:paddingLeft”>
 @dimen/toolbar_horizontal_padding</item>
 <item name=”android:paddingRight”>
 @dimen/toolbar_horizontal_padding</item>
 <item name=”contentInsetStart”>
 @dimen/first_keyline</item>
 </style>
<style name=”Custom.Widget.Toolbar.Button.Navigation” 
parent=”Widget.AppCompat.Toolbar.Button.Navigation”>
 <item name=”android:minWidth”>48dp</item>
</style>

Here are necessary resources:

 <resources>
 <dimen name=”first_keyline”>24dp</dimen>
 <dimen name=”second_keyline”>80dp</dimen>
 <dimen name=”toolbar_horizontal_padding”>12dp</dimen>
 </resources>

layout/layout.xml

 <android.support.v7.widget.Toolbar
 android:id=”@+id/toolbar”
 android:layout_width=”match_parent”
 android:layout_height=”wrap_content”
 android:minHeight=”?attr/actionBarSize”
 android:elevation=”6dp”
 android:background=”@color/colorPrimary”
 app:navigationIcon=”@drawable/ic_menu_white_24dp”
 app:contentInsetStart=”@dimen/second_keyline”
 app:theme=”@style/Theme.GuidelinesCompat.DarkToolbar”
 app:popupTheme=”@style/Toolbar.Popup.AppCompat” />

Try this it helps you.

Upvotes: 1

Ankita
Ankita

Reputation: 1149

<?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=".activities.MainActivity">
    <item
        android:id="@+id/action_notification1"
        android:icon="@android:drawable/btn_dropdown"
        android:title="action_notification"
        app:showAsAction="always">
        <menu>
            <item
                android:id="@+id/profile"
                android:icon="@android:drawable/ic_menu_edit"
                android:orderInCategory="100"
                android:title="PROFILE" />

            <item
                android:id="@+id/c"
                android:icon="@android:drawable/ic_menu_edit"
                android:orderInCategory="100"
                android:title="COMPLETED TRIPS" />
        </menu>
    </item>
</menu>

Upvotes: 1

Raja
Raja

Reputation: 2815

You have to try like this

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/about"
          android:title="About"
          android:gravity="right" />
        <item android:id="@+id/help"
          android:title="Help"
          android:gravity="right" />
   </menu>

Upvotes: 0

Related Questions