How to center align the ActionBar title in Android?

232,950

Solution 1

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));

Solution 2

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 ActionBar
final ActionBar abar = getSupportActionBar();
abar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_background));//line under the action bar
View 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);

Solution 3

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);

Solution 4

Just a quick addition to Ahmad's answer. You can't use getSupportActionBar().setTitle anymore when using a custom view with a TextView. So to set the title when you have multiple Activities with this custom ActionBar (using this one xml), in your onCreate() method after you assign a custom view:

TextView textViewTitle = (TextView) findViewById(R.id.mytext);
textViewTitle.setText(R.string.title_for_this_activity);

Solution 5

Code here working for me.

    // Activity 
 public void setTitle(String title){
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    TextView textView = new TextView(this);
    textView.setText(title);
    textView.setTextSize(20);
    textView.setTypeface(null, Typeface.BOLD);
    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    textView.setGravity(Gravity.CENTER);
    textView.setTextColor(getResources().getColor(R.color.white));
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(textView);
} 

// Fragment
public void setTitle(String title){
    ((AppCompatActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
    ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    TextView textView = new TextView(getActivity());
    textView.setText(title);
    textView.setTextSize(20);
    textView.setTypeface(null, Typeface.BOLD);
    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    textView.setGravity(Gravity.CENTER);
    textView.setTextColor(getResources().getColor(R.color.white));
    ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    ((AppCompatActivity)getActivity()).getSupportActionBar().setCustomView(textView);
}
Share:
232,950

Related videos on Youtube

Sourabh Saldi
Author by

Sourabh Saldi

Updated on November 19, 2021

Comments

  • Sourabh Saldi
    Sourabh Saldi over 2 years

    I am trying to use the following code to center the text in the ActionBar, but it aligns itself to the left.

    How do you make it appear in the center?

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle("Canteen Home");
    actionBar.setHomeButtonEnabled(true);
    actionBar.setIcon(R.drawable.back);
    
    • ᴛʜᴇᴘᴀᴛᴇʟ
      ᴛʜᴇᴘᴀᴛᴇʟ about 7 years
      This is a VERY old question. However, the only solution I see below is to create a custom action bar. So here is the solution without creating a custom action bar. stackoverflow.com/a/42465266/3866010 hope it helps someone
    • Sourabh Saldi
      Sourabh Saldi almost 4 years
      Almost 8 years after i asked this question , using the answer again :D
  • Sourabh Saldi
    Sourabh Saldi over 11 years
    how to add custom back button image to this?
  • alfongj
    alfongj over 11 years
    For me the result was the actionbar was a bit displaced to right or left depending on the action buttons I was showing. To fix it, I just set the layout_width to "WRAP_CONTENT"
  • Tom Naessens
    Tom Naessens over 11 years
    Pepillo's solution did not work for me, as the content wasn't centered anymore at all. I could solve this by adding android:layout_gravity="center" to the LinearLayout.
  • Sergii Pechenizkyi
    Sergii Pechenizkyi over 11 years
    I was needed to add ActionBar.LayoutParams with match_parent parameter to get customView layout actually centered. Check out code snippet from this answer.
  • Ahmad
    Ahmad over 11 years
    @plastiv hmm not a bad idea either.
  • Mitesh Shah
    Mitesh Shah over 10 years
    @Ahmad how to tackle back navigation??
  • Someone Somewhere
    Someone Somewhere about 10 years
    Note: Android support library v7 does contain a function getSupportActionBar(), so it's not just limited to Action Bar Sherlock
  • Nezam
    Nezam about 10 years
    the title doesnt change now with setTitle
  • Sufian
    Sufian about 10 years
    @Nezam it's an expected behaviour. Try ((TextView)actionBar.getCustomView().findViewById(R.id.textV‌​iew1)).setText("new title");, where textView1 is your TextView's ID in your CustomView.
  • Admin
    Admin about 10 years
    how to add back button in this code? getSupportActionBar().setDisplayHomeAsUpEnabled(true); not working
  • Sufian
    Sufian almost 10 years
    @Jamm is it not showing or it clicks but doesn't work?
  • Someone Somewhere
    Someone Somewhere almost 10 years
    this doesn't work - maybe because I have the navigation menu button showing ?
  • Someone Somewhere
    Someone Somewhere almost 10 years
    is vertical orientation in this solution even correct?? The results don't look like it ...
  • sashoalm
    sashoalm over 9 years
    "I'm set ActionBar.LayoutParams..." - that doesn't make sense. Also, please format your code.
  • djdance
    djdance over 9 years
    Hah, plz do not beat 曲连强, his setDisplayOptions example helps me to add centered title AND show home button
  • Mohammad Imran
    Mohammad Imran over 9 years
    FYI , If you will add menu items, title will not be exactly centered.
  • DominicM
    DominicM about 9 years
    If there are menu items it won't be centered correctly. To really always have the title centered add "WRAP_CONTENT" to the LinearLayout and move the android:layout_gravity="center" from the TextView to the LinearLayout.
  • Jose Manuel Abarca Rodríguez
    Jose Manuel Abarca Rodríguez about 9 years
    What is "R.layout.action_bar_title"? I can't find it.
  • Aditya
    Aditya almost 9 years
  • AndrewBramwell
    AndrewBramwell over 8 years
  • Aditya Vyas-Lakhan
    Aditya Vyas-Lakhan over 8 years
    can any one tell me that after using this answer my drawer icon is not visible in my app...what is the issue?
  • Ruchir Baronia
    Ruchir Baronia over 8 years
    @AndrewBramwell getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUST‌​OM); is throwing a null pointer exception
  • Ruchir Baronia
    Ruchir Baronia over 8 years
    Do you guys know how I can fix this?
  • Ajay Kulkarni
    Ajay Kulkarni over 8 years
    What is "R.layout.action_bar_title"? Explain it. Do I need to register that custom xml in manifests.xml
  • Khan
    Khan about 7 years
    java.lang.NullPointerException ;)
  • Karl Jamoralin
    Karl Jamoralin over 6 years
    If you want to add the back button, you need to place getsupportActionBar().setDisplayHomeAsUpEnabled(true) after setting the custom view
  • Kostadin
    Kostadin over 5 years
    Other problem with centering horizontal can provoked by "match_parent". After replacing it with "wrap_content"
  • Deep Lathia
    Deep Lathia over 4 years
    Why do we need to create a params object?? Can we not specify the gravity in the external layout file that we're assigning to the ActionBar.
  • turbandroid
    turbandroid about 4 years
    check you app theme!!
  • Sunil Chaudhary
    Sunil Chaudhary almost 4 years
    what if i am extending my class with FragmentActivity??
  • Anderson Spencer
    Anderson Spencer over 3 years
    was looking for something like this, BINGO!