Home » Android » Centered title in action bar is moved when up indicator appears

Centered title in action bar is moved when up indicator appears

Posted by: admin November 1, 2017 Leave a comment

Questions:

I am developing simple android app. What I want to achieve is have centered label in the action bar. Following popular advice I used custom layout for action bar.

action_bar_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/action_bar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/mainExhibitionTitle"
        style="@style/EEActionBarTextTheme" />

</RelativeLayout>

styles.xml:

<resources>    
    <style name="EETheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="actionBarStyle">@style/EEActionBarTheme</item>
        <item name="android:homeAsUpIndicator">@drawable/button_up_indicator</item>
        <item name="android:windowBackground">@color/appBackground</item>
    </style>    
    <style name="EESplashScreenTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:windowBackground">@drawable/splash</item>
    </style>    
    <style name="EEActionBarTheme" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/menuBackground</item>
        <item name="background">@color/menuBackground</item>
        <item name="titleTextStyle">@style/EEActionBarTextTheme</item>
    </style>    
    <style name="EEActionBarTextTheme" parent="TextAppearance.AppCompat.Title">
        <item name="android:textSize">18dp</item>
        <item name="android:textColor">#000000</item>
    </style>
</resources>

The problem is: whenever user changes activity, which indicates appear of up indicator in action bar, my title get moved slightly to right:

MainActivity:

enter image description here

Child activity:

enter image description here

How can I keep title in the same spot, regardless of other action bar elements?

Answers: