Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center align the ActionBar title in Android?

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); 
like image 528
Sourabh Saldi Avatar asked Sep 12 '12 11:09

Sourabh Saldi


People also ask

Where is ActionBar in Android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.


2 Answers

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)); 
like image 141
Ahmad Avatar answered Sep 23 '22 18:09

Ahmad


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); 
like image 23
Someone Somewhere Avatar answered Sep 22 '22 18:09

Someone Somewhere