" + "Settings" + "" /> " + "Settings" + "" /> " + "Settings" + ""/>

Center action bar title - Android

105 Views Asked by At
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
setTitle(Html.fromHtml("<font color=\"white\">" + "Settings" + "</font>"));

This is how the action bar looks like now

I'm using these two lines on onCreate method to change action bar title and color but it's aligned to the left, is there a way to center it?

1

There are 1 best solutions below

1
Aman On

Instead of using setTitle use a custom toolbar

<android.support.v7.widget.Toolbar
     android:id="@+id/toolbar_top"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:minHeight="?android: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 file
   Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_top);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);