How to center align the ActionBar title in Android?
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);
}
Related videos on Youtube
Sourabh Saldi
Updated on November 19, 2021Comments
-
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 yearsThis 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 almost 4 yearsAlmost 8 years after i asked this question , using the answer again :D
-
-
Sourabh Saldi over 11 yearshow to add custom back button image to this?
-
alfongj over 11 yearsFor 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 over 11 yearsPepillo'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 over 11 yearsI was needed to add
ActionBar.LayoutParams
withmatch_parent
parameter to get customView layout actually centered. Check out code snippet from this answer. -
Ahmad over 11 years@plastiv hmm not a bad idea either.
-
Mitesh Shah over 10 years@Ahmad how to tackle back navigation??
-
Someone Somewhere about 10 yearsNote: Android support library v7 does contain a function
getSupportActionBar()
, so it's not just limited to Action Bar Sherlock -
Nezam about 10 yearsthe title doesnt change now with
setTitle
-
Sufian about 10 years@Nezam it's an expected behaviour. Try
((TextView)actionBar.getCustomView().findViewById(R.id.textView1)).setText("new title");
, where textView1 is yourTextView
's ID in your CustomView. -
Admin about 10 yearshow to add back button in this code?
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
not working -
Sufian almost 10 years@Jamm is it not showing or it clicks but doesn't work?
-
Someone Somewhere almost 10 yearsthis doesn't work - maybe because I have the navigation menu button showing ?
-
Someone Somewhere almost 10 yearsis vertical orientation in this solution even correct?? The results don't look like it ...
-
sashoalm over 9 years"I'm set ActionBar.LayoutParams..." - that doesn't make sense. Also, please format your code.
-
djdance over 9 yearsHah, plz do not beat 曲连强, his setDisplayOptions example helps me to add centered title AND show home button
-
Mohammad Imran over 9 yearsFYI , If you will add menu items, title will not be exactly centered.
-
DominicM about 9 yearsIf 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 about 9 yearsWhat is "R.layout.action_bar_title"? I can't find it.
-
Aditya almost 9 yearssame issue stackoverflow.com/questions/31803463/…
-
AndrewBramwell over 8 yearsSimple, and it works! quotehd.com/imagequotes/authors39/tmb/…
-
Aditya Vyas-Lakhan over 8 yearscan any one tell me that after using this answer my drawer icon is not visible in my app...what is the issue?
-
Ruchir Baronia over 8 years@AndrewBramwell getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); is throwing a null pointer exception
-
Ruchir Baronia over 8 yearsDo you guys know how I can fix this?
-
Ajay Kulkarni over 8 yearsWhat is "R.layout.action_bar_title"? Explain it. Do I need to register that custom xml in manifests.xml
-
Khan about 7 yearsjava.lang.NullPointerException ;)
-
Karl Jamoralin over 6 yearsIf you want to add the back button, you need to place
getsupportActionBar().setDisplayHomeAsUpEnabled(true)
after setting the custom view -
Kostadin over 5 yearsOther problem with centering horizontal can provoked by "match_parent". After replacing it with "wrap_content"
-
Deep Lathia over 4 yearsWhy 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 about 4 yearscheck you app theme!!
-
Sunil Chaudhary almost 4 yearswhat if i am extending my class with FragmentActivity??
-
Anderson Spencer over 3 yearswas looking for something like this, BINGO!