How to center toolbar back button?

mewa picture mewa · Apr 27, 2015 · Viewed 13.8k times · Source

I'm having a problem trying to center the back button on the support toolbar. I'm using it inside an ActionBarActivity.

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

And set the up navigation inside the Activity's onCreate() like this:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

getSupportActionBar().setTitle(R.string.title_activity_scanner);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

However, what I'm getting is this:

As you can see the back button is misplaced

Edit: The problem seems to lie in a custom value for ?attr/actionBarSize set to 40dp, however, it turns out now, that it's the title that is misplaced instead.

Answer

Rick Sanchez picture Rick Sanchez · Jun 21, 2015

From the developer.android.com :

Toolbar supports a more focused feature set than ActionBar. From start to end, a toolbar may contain a combination of the following optional elements:

A navigation button. This may be an Up arrow, navigation menu toggle, close, collapse, done or another glyph of the app's choosing. This button should always be used to access other navigational destinations within the container of the Toolbar and its signified content or otherwise leave the current context signified by the Toolbar. The navigation button is vertically aligned within the Toolbar's minimum height, if set.

So if you set minHeight attribute the same as your toolbar height (layout_height ), the back arrow will be centered vertically.