0

I have two methods to get toolbar TextView and modify it.

private void setupToolbarTitle() {
    mToolbarTitle = getActionBarTextView();

    mToolbarTitle.setAllCaps(true);
    mToolbarTitle.setGravity(Gravity.CENTER_HORIZONTAL);
}

private TextView getActionBarTextView() {
    TextView titleTextView = null;

    try {
        Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
        f.setAccessible(true);
        titleTextView = (TextView) f.get(toolbar);
    } catch (NoSuchFieldException e) {
    } catch (IllegalAccessException e) {
    }
    return titleTextView;
}

toolbar.xml file:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="@color/appMainBgLighter"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

After setGravity TextView is still on the left side of Toolbar (setAllCaps works fine and capitalize all letters). Why is that?

y07k2
  • 1,898
  • 4
  • 20
  • 36
  • check this:http://stackoverflow.com/questions/29443604/android-how-to-centered-title-int-toolbar-in-android – Ashwini Bhat Jun 02 '16 at 07:35
  • It is a strange situation now. I use `LayoutParams` to center it and it did, but it take width without "hamburger menu". Next when I tried to use menu.xml in `onCreateOptionsMenu` it doesn't be centered and stays in the same position as before I insert `onCreateOptionsMenu`. – y07k2 Jun 02 '16 at 08:09

0 Answers0