How to center align the ActionBar title in Android? How to center align the ActionBar title in Android? android android

How to center align the ActionBar title in Android?


To have a centered title in ABS (if you want to have this in the default ActionBar, just remove the "support" in the method names), you could just do this:

In your Activity, in your onCreate() method:

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

abs_layout:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"        android:layout_gravity="center"    android:orientation="vertical">    <android.support.v7.widget.AppCompatTextView        android:id="@+id/tvTitle"        style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:gravity="center"        android:textColor="#FFFFFF" /></LinearLayout>

Now you should have an Actionbar with just a title. If you want to set a custom background, set it in the Layout above (but then don't forget to set android:layout_height="match_parent").

or with:

getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.yourimage));


I haven't had much success with the other answers... below is exactly what worked for me on Android 4.4.3 using the ActionBar in the support library v7. I have it set up to show the navigation drawer icon ("burger menu button")

XML

    <?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="horizontal" >    <TextView        android:id="@+id/actionbar_textview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:maxLines="1"        android:clickable="false"        android:focusable="false"        android:longClickable="false"        android:textStyle="bold"        android:textSize="18sp"        android:textColor="#FFFFFF" /></LinearLayout>

Java

//Customize the ActionBarfinal ActionBar abar = getSupportActionBar();abar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_background));//line under the action barView viewActionBar = getLayoutInflater().inflate(R.layout.actionbar_titletext_layout, null);ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar !        ActionBar.LayoutParams.WRAP_CONTENT,         ActionBar.LayoutParams.MATCH_PARENT,         Gravity.CENTER);TextView textviewTitle = (TextView) viewActionBar.findViewById(R.id.actionbar_textview);textviewTitle.setText("Test");abar.setCustomView(viewActionBar, params);abar.setDisplayShowCustomEnabled(true);abar.setDisplayShowTitleEnabled(false);abar.setDisplayHomeAsUpEnabled(true);abar.setIcon(R.color.transparent);abar.setHomeButtonEnabled(true);


Define your own custom view with title text, then pass LayoutParams to setCustomView(), as Sergii says.

ActionBar actionBar = getSupportActionBar()actionBar.setDisplayShowCustomEnabled(true);actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); actionBar.setCustomView(getLayoutInflater().inflate(R.layout.action_bar_home, null),        new ActionBar.LayoutParams(                ActionBar.LayoutParams.WRAP_CONTENT,                ActionBar.LayoutParams.MATCH_PARENT,                Gravity.CENTER        ));

EDITED: At least for width, you should use WRAP_CONTENT or your navigation drawer, app icon, etc. WON'T BE SHOWN (custom view shown on top of other views on action bar). This will occur especially when no action button is shown.

EDITED: Equivalent in xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="match_parent"    android:layout_gravity="center_horizontal"    android:orientation="vertical">

This doesn't require LayoutParams to be specified.

actionBar.setCustomView(getLayoutInflater().inflate(R.layout.action_bar_home, null);