Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ActionBar - Push custom view to bottom of screen

I've got a split ActionBar, and I'm trying to add functionality almost identical to google play's 'Now playing'..

I can get menu items to appear at the bottom of the screen, using the onCreateOptionsMenu, but I can't seem to get a custom view to appear at the bottom of the screen when using actionBar.setCustomView. The custom view just sits underneath the top ActionBar.

Does anyone know how to force the customview to the bottom, or to add custom views to the onCreateOptionsMenu?

Here are the snippets of my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

    //Custom view to add
    actionBar.setCustomView(R.layout.albumitem);
            //This view shows up just under the top actionbar at the moment,
              despite menu items being at the bottom

The menu options code: (These are showing down the bottom where I'd like my custom view to be)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.option_menu, menu);

    return (super.onCreateOptionsMenu(menu));
}

I believe that the now playing menu at the bottom of the google play music app is a custom view inside a split action bar:

Google Play Music

like image 553
Tim Malseed Avatar asked Aug 05 '12 11:08

Tim Malseed


People also ask

How do I add items to Action Bar?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.


1 Answers

So I managed to find a dirty solution to this.. Well, dirty in my amateurish opinion..

In the oncreateOptionsMenu, I've inflated a menu called option_menu like so:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.option_menu, menu);

And in the xml for that option_menu item, I've implemented an actionViewClass, and in this case I've used relative layout (I'm guessing I could've just used View..?):

<item
    android:id="@+id/layout_item"
    android:actionViewClass="android.widget.RelativeLayout"
    android:showAsAction="always|withText"
    android:title="Text 1"/>

</menu>

So then I inflated an xml layout, and added it to the layout_item defined in my menu:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.option_menu, menu);
        relativeLayout = (RelativeLayout) menu.findItem(R.id.layout_item)
                .getActionView();

        View inflatedView = getLayoutInflater().inflate(R.layout.now_playing,
                null);

        relativeLayout.addView(inflatedView);

        return (super.onCreateOptionsMenu(menu));
    }

Please feel free to edit/enhance this answer as you see fit.

like image 121
Tim Malseed Avatar answered Oct 13 '22 05:10

Tim Malseed