Skip to content Skip to sidebar Skip to footer

Align Center Menu Item Text In Android

Using following code I

Solution 1:

You can center-align the menu items dynamically using SpannableString in your activity:

intpositionOfMenuItem=0; //or any other postionMenuItemitem= menu.getItem(positionOfMenuItem);
SpannableStrings=newSpannableString(settingsItemTitle);

s.setSpan(newAlignmentSpan.Standard(Alignment.ALIGN_CENTER), 0, s.length(), 0);

item.setTitle(s);

Solution 2:

I used @Mahm00d's way, that worked perfectly, but I found another way to solve the problem. It is to add \t before the String. If the space isn't enough, I can add more.

Because Chinese words are one by one, the length of the item is same.

Solution 3:

Another way to achieve this is via setting theme

 <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeToolbar.NavigationViewPadding"
    app:itemTextColor="@color/inverse_color"
    app:itemIconTint="@color/inverse_color"
    app:itemBackground="@drawable/selector_navigation"
    app:headerLayout="@layout/activity_home_nav_header"
    app:menu="@menu/activity_home_drawer" />

In your styles

<stylename="ThemeToolbar.NavigationViewPadding"><itemname="listPreferredItemPaddingLeft">50dp</item></style>

How it looks like

Post a Comment for "Align Center Menu Item Text In Android"