0

I have in my application a ListView activity called ResultListViewActivity and which display contents from a database. The content displayed in the listview depends on a button clicked in a previous activity. So if the user clicks the "Button 1" in the Main Activity, it will display a specific content in ResultListViewActivity, and so on.

I want to add a menu to this activity : the thing is, the items of this menu are also supposed to change according to the button clicked before. There are 9 cases, each cases has a different number of items. So far I have this code :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Bundle bundle = getIntent().getExtras();
    int filterVariable = bundle.getInt("filterVariable");
    switch (filterVariable) 
    {
    case 1:
        getSupportMenuInflater().inflate(R.menu.filter_fastfood, menu);
        break;

    case 2:
        getSupportMenuInflater().inflate(R.menu.filter_restaurants, menu);
        break;

    case 3: 
        getSupportMenuInflater().inflate(R.menu.filter_coffeeshops, menu);
        break;

    case 4:
        getSupportMenuInflater().inflate(R.menu.filter_bars, menu);
        break;

// [... and so on]
    }
    return super.onCreateOptionsMenu(menu);
}

It works well, for every case I indeed have my different items in the menu. But I don't want these items to be reachable through the "menu button" of my device (which is absolutely not instinctive and hard for the user to guess). These items are importants so I would like to have a button on the upper right corner, which would open this menu instead (drop down menu / pop up menu).

Do you know how such a thing can be done ? I have seen this in some google application but couldn't find tutorial showing which tools to use for doing this.
Thanks !

ps: I am using actionbarsherlock in my application.

Phalanx
  • 1,217
  • 6
  • 25
  • 36

1 Answers1

1

If you are using the latest version 4.2.0 of ABS, then support for .ForceOverflow themes has been removed.

Source: Version 4.2.0 Changelog

Extract of the Change Log:

Add SearchView widget for standard search interaction (API 8+ only)
Fix: ShareActionProvider in the split action bar no longer fills the entire screen.
Fix: ShareActionProvider now does file I/O on a background thread.
Fix: Automatically correct ColorDrawable not respecting bounds when used as a stacked background.
Fix: Ensure fragments collection is present before dispatching events.
Fix: XML-defined onClick searches the correct context for the declared method.
Fix: Ensure action mode start/finish callbacks are invoked on the activity for the native action bar.
Fix: Allow tab callbacks to have a fragment transaction instance for any FragmentActivity.
Fix: Ensure CollapsibleActionView callbacks are dispatched in both native and compatbility action bars.
Fix: Remove .ForceOverflow themes. These never should have been included.

To workaround that, you will need to download an older version (version 4.1.0) of ABS that had it working. You can get a list of download as per their release history here: http://actionbarsherlock.com/download.html

This is from a production app, tested and functioning.

You will also need to make a minor change in your applications default theme. For example:

<style name="MyTheme" parent="@style/Theme.Sherlock.ForceOverflow">
    <item name="android:windowBackground">@color/background</item>
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
    <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
</style>

<style name="MyTheme.ForceOverflow">
    <item name="absForceOverflow">true</item>
</style>

Note the use of @style/Theme.Sherlock.ForceOverflow in the parent attribute for the primary theme. Also not the additional <style> with the name of MyTheme.ForceOverflow. Doing the above will get the Overflow menu working for you.

You will also need to add this attribute to your menu items:

android:showAsAction="ifRoom|withText"

Now, to force the device into thinking that it has a physical menu key, use this code:

NOTE: This is a hack and for the obvious lack of access to multiple devices, I have personally never tested if it works on every Android device that exists. Use at your own risk etc. FYI, I have used this before and it worked with no complaints till date.

try {
    ViewConfiguration config = ViewConfiguration.get(MainPage.this);
    Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
    if (menuKeyField != null) {
        menuKeyField.setAccessible(true);
        menuKeyField.setBoolean(config, false);
    }
} catch (Exception e) {
    e.printStackTrace();
}

Forcing the Overflow Menu to appear in the ActionBar as against not showing an indicator that more options exist is a personal preference. I am answering this from the stand point of a technically possible solution as against promoting the use of such a feature. Whether it is advisable from the UX point of view is another subject matter.

Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • Wow thanks, I am a bit disappointed and upset that it has to be a hack and there is no cleaner way to do this. When I checked the ActionBarSherlock demo on the play store, there was this demonstration of "Sub-menus" doing exactly what I wanna do and it was implying that it was an obvious thing to do. – Phalanx Mar 20 '13 at 01:50
  • @Phalanx: read the second edit in this answer of mine: http://stackoverflow.com/a/13307583/450534 – Siddharth Lele Mar 20 '13 at 02:10
  • Thx, I took a look but I feel it's gonna be a too complicated path to take as I am still a beginner. I am gonna explore this way instead : http://stackoverflow.com/questions/10483612/popup-menu-on-click-of-a-button-in-action-bar Seems to fit with my needs ! – Phalanx Mar 20 '13 at 02:32
  • @Phalanx: it may sound complicated, but it really is a one time thing. But good luck anyway. :-) – Siddharth Lele Mar 20 '13 at 02:50