S. O. Chaos S. O. Chaos - 2 months ago 27
Android Question

Center align of text of title bar in Android

I'm new at Android programming so I don't know all the features.
As I understood, all generated activities has a title bar which contains app_name string resource. I managed to change title of sole activity but I don't understand how to change alignment of the text of the title. I mean, the're generated like this:

enter image description here

but I need it to be like this:

enter image description here

Answer

If you are using support toolbar , the approach will be easy. just add a text view inside it and make it centered :

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar_top"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/action_bar_bkgnd"
    app:theme="@style/ToolBarTheme" >


     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Toolbar Title"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title" />


    </android.support.v7.widget.Toolbar>

in your activity you can access the title like so:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_top);
TextView Title = (TextView) toolbar.findViewById(R.id.toolbar_title);