Centered ActionBar Title

11,107 views
Skip to first unread message

shrift

unread,
Jan 3, 2012, 1:50:20 PM1/3/12
to ActionBarSherlock
Sorry if I am just being a noob here, but I cannot figure out how to
make the ActionBar Title be centered in the space between the "up"
logo on the left and the actions on the right, *without* modifying the
ActionBarSherlock project. Is there a way to do this? I was able to
get the behavior I wanted on pre-honeycomb devices by modifying the
layout file in ActionBarSherlock, but I am not sure how to do
accomplish this for a native ActionBar... I know that's outside the
realm of ABS, just thought I'd ask and see if you knew a way.

I know having a centered title is not the standard behavior for an
Android ActionBar, nonetheless, it is a request I need to attempt to
comply with. : (

To be clear I am hoping for a way I can make the title be centered for
all action bars (native and ABS) by just defining an attribute
somewhere in my xml style.

Below I added the android:layout_gravity="center" to the parent
LinearLayout of the title text views to get the behavior I want, which
of course only worked for ABS:

<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:paddingRight="10dip">
<com.actionbarsherlock.internal.widget.ScrollingTextView
android:id="@+id/abs__action_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:scrollHorizontally="true"
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
/>
<com.actionbarsherlock.internal.widget.ScrollingTextView
android:id="@+id/abs__action_bar_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:scrollHorizontally="true"
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:visibility="gone"
/>
</LinearLayout>

Jake Wharton

unread,
Jan 3, 2012, 1:59:23 PM1/3/12
to actionba...@googlegroups.com
Only thing I can think of to make it work everywhere would be to use a custom navigation view, specify layout_width="fill_parent" and center within that. It definitely can't be done with the regular title on the native action bar though.

Let me know if that works or not because I'm willing to bet others are interested in this as well.

shrift

unread,
Jan 3, 2012, 2:15:47 PM1/3/12
to ActionBarSherlock
Ok, I'll look into that; I'll post back here if I figure anything out.

On Jan 3, 1:59 pm, Jake Wharton <jakewhar...@gmail.com> wrote:
> Only thing I can think of to make it work everywhere would be to use a
> custom navigation view, specify layout_width="fill_parent" and center
> within that. It definitely can't be done with the regular title on the
> native action bar though.
>
> Let me know if that works or not because I'm willing to bet others are
> interested in this as well.
>
> ---
> Jake Whartonhttp://about.me/jakewharton

shrift

unread,
Jan 3, 2012, 5:01:04 PM1/3/12
to ActionBarSherlock
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.

Jake Wharton

unread,
Jan 3, 2012, 5:21:50 PM1/3/12
to actionba...@googlegroups.com
Excellent. Glad it worked.

---
Jake Wharton
http://about.me/jakewharton

Paresh N. Mayani

unread,
Sep 20, 2012, 1:14:11 AM9/20/12
to actionba...@googlegroups.com
Hi Jack,

Is there any way to change Title text color of the activity? I have tried below code:

com.actionbarsherlock.app.ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setBackgroundDrawable(getResources().getDrawable(
R.drawable.bg_actionbar));
actionBar.setIcon(getResources().getDrawable(icon));
actionBar.setTitle(heading);

Please let me know if there is anyway other than setting custom view?


Thanks,
Paresh 

Jake Wharton

unread,
Sep 20, 2012, 10:09:49 AM9/20/12
to actionba...@googlegroups.com

There is a titleTextColor attribute on the action bar style referenced from the theme.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages