0
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?

Senith Umesha
  • 154
  • 4
  • 13

1 Answers1

0

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);
Aman
  • 458
  • 4
  • 16
  • Actually I was kinda looking for a way to solve this within these 2 lines without going for a custom one, anyway thanks though – Senith Umesha Nov 02 '21 at 08:39