11

I tried my own Custom Action bar for getting action bar title centered on the middle. It worked. However, after I added the options menu in Action bar, the title again shifted to the left. What should I do in order to get the title centered on the middle of the action bar? my code is:

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);    
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    View cView = getLayoutInflater().inflate(R.layout.custom_actionbar, null);
    actionBar.setCustomView(cView);

and my layout is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/holo_blue_light"
android:orientation="vertical" >

<TextView
    android:id="@+id/apptitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_gravity="center_horizontal|center_vertical"
    android:gravity="center_horizontal|center_vertical"
    android:text="@string/app_name" />

Anu
  • 1,303
  • 3
  • 23
  • 38

2 Answers2

17

I am sure, you are using the below to set your custom theme.

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

If not, then please add the first line. And the text in your custom layout is centered. (Gravity = Center)

Also I found a similar link here and here. Hope this helps.

Try this out as well.

I was able to accomplish what I wanted by not showing the title of the activity, and instead using a customView as you suggested. The key that I missed for a while was you must use the setCustomView(View, ActionVar.LayoutParams) method in order to get the custom view centered, simply setting a layout_gravity="center" in the custom view layout file does not work. Here's a short code snippet showing how I got it to work:

            TextView customView = (TextView)
LayoutInflater.from(this).inflate(R.layout.actionbar_custom_title_view_centered,
null);
            ActionBar.LayoutParams params = new
ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);

            customView.setText("Some centered text");
            getSupportActionBar().setCustomView(customView, params);

This method is working for me on at least 3.2, 2.3, and 2.2 devices.

Source https://groups.google.com/forum/#!msg/actionbarsherlock/A9Ponma6KKI/Llsp41jWLEgJ

Community
  • 1
  • 1
Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
  • Did you check solutions in the links as well. And , try setting android:layout_width="match_parent" in your RelativeLayout. – Atul O Holic Feb 14 '14 at 04:08
  • This works, but removes the button for the navigation menu. The ActionBar in Android bloody stinks, so I've always suspected that I'd end up having to roll my own... – Someone Somewhere May 02 '14 at 22:14
  • It actually does and is necessary to maintain Androids standard user experience. However once we get familiar to its properties we can use It to suit our needs. Just check if you have set the parent activity to your drawer cause I have used it this way and the drawer icon shows – Atul O Holic May 03 '14 at 05:28
  • 1
    this solution does not work if you have overflow icons. overflow icon width does not count for the custom action bar view width, so your centered logo will always be displayed more to the left (as wide as the overflow icons are wide). – dy_ Oct 01 '14 at 17:08
4

First, create a .xml file like this: (action_bar.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
    android:id="@+id/actionbar_title"
    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="#000000" />

</LinearLayout>

And then, in your activity:

View view = getLayoutInflater().inflate(R.layout.action_bar, null);
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.MATCH_PARENT,
            Gravity.CENTER);

    TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
    Title.setText("Your Title");

getSupportActionBar().setCustomView(view,params);       
getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title
getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title

I've tried a lot of solutions and only this one works. Hope it helps you.

Willy Chen
  • 241
  • 2
  • 3