Search code examples
androidandroid-actionbarlabel

Android: How to center activity label


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


Solution

  • 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" />