4

I'm trying to make an ActionBar that has:

  • Home button as up enabled with title (to the left)
  • TextView (centered)
  • Overflow Menu items (to the right)

I was following this answer to an almost identical question. The difference is that I would like to show a title. I tried actionBar.setTitle("Title"), but it gets cut off by the LinearLayout and isn't visible.

How can I create a custom View in the center of my ActionBar with the above elements?

Is it possible to do without overriding the entire ActionBar?

Edit:

I set up the ActionBar here:

final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setTitle("Title");
actionBar.setCustomView(R.layout.some_layout);

Here is the some_layout.xml file:

<?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:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some Text" />

</LinearLayout>

Here is the resulting ActionBar I get from this (I set the LinearLayout background to green and text color to white):

action bar

Here is what I'm trying to get (without the green background):

enter image description here

Community
  • 1
  • 1
lschlessinger
  • 1,934
  • 3
  • 25
  • 47

1 Answers1

0

Try this:

        ActionBar ab = getSupportActionBar();
    ab.setCustomView(R.layout.registration_actionbar);
    ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
            | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
Price
  • 2,683
  • 3
  • 17
  • 43
  • What was the result? I used this in my project to retain the default app title while providing a custom layout for the action bar of an activity. – Price Jun 01 '14 at 18:54
  • Nothing changed in my ActionBar. The custom layout that's in the center had a width that filled all the way to the home icon. I'll put my layout and ActionBar code in an edit. – lschlessinger Jun 01 '14 at 18:57
  • Just checked my code, I could retain the app icon but had to add a textview for the app title in my custom layout – Price Jun 01 '14 at 19:15
  • That's the exact problem I'm having. I don't want to add the TextView with a title though because I just want to keep it as a MenuItem to handle clicks. – lschlessinger Jun 01 '14 at 19:19
  • After looking closely at my past code, it seems that I used a textview only to customise the formatting of the app title for this activity, but the default title still appeared underneath it. So you can leave that area of the custom layout blank and your title should appear fine. – Price Jun 02 '14 at 07:55
  • Another option would be to add an onclicklistener for the textview that you use for a custom title in your custom actionbar layout – Price Jun 02 '14 at 08:02
  • 1
    Can you include that example in your answer? – lschlessinger Jun 04 '14 at 01:30