Android: How to center activity label

10,629

If you are using Toolbar you might try this out :

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize">
 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My App"
    android:layout_gravity="center"
    android:id="@+id/toolbar_title" />
</android.support.v7.widget.Toolbar>

If you are using ActionBar try this :

In your onCreate() add this :

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.your_LAYOUT);

And your_LAYOUT should be like :

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="myApp"
    android:id="@+id/tvtitle" />

Share:
10,629
Eric
Author by

Eric

Updated on June 05, 2022

Comments

  • Eric
    Eric almost 2 years

    I want to make the label(My App) to be aligned centrally in action bar. How should I do?

    Here is the current screen:

    Home screen