By urSus


2012-10-05 16:05:15 8 Comments

How could I make an actionbar to have logo in the center and two action items on sides like this?

enter image description here

I will be using ActionBar Sherlock.

1 comments

@dennisdrew 2012-10-05 17:10:20

As I noted in a comment, I am not exactly sure if any of this would change with ABS (I'm sure not much would), but with the standard Action Bar you can load a custom layout .xml for your action bar title. For example, you could have action_bar_title.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/your_desired_background" >

<TextView
        android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:maxLines="1"
            android:ellipsize="end"
            android:text="@string/app_name"
            android:textAppearance="?android:attr/textAppearanceMedium"/>

</RelativeLayout>

Then, in your Activity, you would want to call a method like this in onCreate():

private void setupActionBar() {
        ActionBar ab = getActionBar();
        ab.setDisplayShowCustomEnabled(true);
        ab.setDisplayShowTitleEnabled(false);
        ab.setIcon(R.drawable.your_left_action_icon);
        LayoutInflater inflator = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.action_bar_title, null);

        TextView titleTV = (TextView) v.findViewById(R.id.title);
        Typeface font = Typeface.createFromAsset(getAssets(),
                "fonts/your_custom_font.ttf");
        titleTV.setTypeface(font);

        ab.setCustomView(v);

            ab.setHomeAsUpEnabled(true);
    }

To take control of the left action item, you could do this in your Activity:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.yourRightActionItem:
                        //do something
                        return true;
        case android.R.id.home: //<-- this will be your left action item
            // do something
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

Of course, make sure you also do the other necessary setups for the Action items in your menu xml's as well. This will be for any right-sided action items.

Edit: Just saw in another comment you said that title would be an image. If that's the case, just replace the TextView I used in my example with your ImageView, and disregard the font customization code in the setupActionBar() method.

Edit2: I updated my Java code. You will need to do ab.setHomeAsUpEnabled(true), and then have a custom theme to implement an invisible (or perhaps @null although I don't know if Android will accept that) drawable for the home as up indicator. See here (taken from answer here:)

<style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
    <item name="android:homeAsUpIndicator">@drawable/my_fancy_up_indicator</item>
    <!-- or you could try @null -->
</style>

@urSus 2012-10-05 17:39:22

so the solution to the action item on the left would be to fake it using UP button as I wrote in the first comment, right?

@dennisdrew 2012-10-05 17:44:11

Yes, and you would want to add a custom style for android:homeAsUpIndicator so you don't get that left chevron indicator. I will update with more code.

@Massimo Variolo 2013-04-04 09:56:53

How can I hide the arrow without android:homeAsUpIndicator? It requires API level 11 and my minimum is 8

@dennisdrew 2013-04-05 15:35:42

Are you using ActionBarSherlock?

@J. K. 2013-10-12 09:14:21

How is it done with Sherlock? I want to support API10

@Pranav Mahajan 2014-09-03 10:25:47

Is it possible to do this once, say in Application class rather than doing this in every Activity?? How??

@Pranav Mahajan 2014-09-03 10:42:37

Actually we should create our own MyActivity extends Activity & do it once there.

@Kenji 2015-01-12 16:54:58

@dennisdrew thanks for solution. but is there a way that I put dropdown title to the center. ( not just title)?

@Sharone Lev 2019-09-06 09:15:57

This only worked for me after adding actionBar.setDisplayShowCustomEnabled(true);

Related Questions

Sponsored Content

34 Answered Questions

[SOLVED] How to center absolutely positioned element in div?

52 Answered Questions

17 Answered Questions

[SOLVED] How do I change the android actionbar title and icon

64 Answered Questions

[SOLVED] How do I center text horizontally and vertically in a TextView?

  • 2009-01-11 00:27:55
  • pupeno
  • 1199240 View
  • 1954 Score
  • 64 Answer
  • Tags:   android textview

21 Answered Questions

[SOLVED] How to get the ActionBar height?

23 Answered Questions

[SOLVED] How do I hide a menu item in the actionbar?

17 Answered Questions

[SOLVED] How to Set a Custom Font in the ActionBar Title?

9 Answered Questions

[SOLVED] How to customize the back button on ActionBar

7 Answered Questions

Sponsored Content