8

Possible Duplicate:
ActionBarSherlock & HoloEverywhere - Forcing Overflow?

I am using ActionbarSherLock library to display Actionbar on pre-Gingerbread devices..

I want Overflow menu on actionbar.. I have searched a lot and i come to know that devices that has hardware Menu Button then Overflow Menu will not display..

and i also looked into ActionbarSherlock sample demo for that solution,bt still i can't get solution..

I have created demo for actionbar,when emulate on device which has no Menu Button then it will display overflow menu but if device has Menu button then overflow menu will not displaying..

Following is my Configuration :-

 public class MainActivity extends SherlockActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_Sherlock);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater=getSupportMenuInflater();
        inflater.inflate(R.menu.activity_main, menu);

//        return true;
        return super.onCreateOptionsMenu(menu);
    }
}

Manifest :-

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="demo.menu.actionbar_using_lib"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

 <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Sherlock"
     >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" 
       >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Menu Configuration :-

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/save"
    android:title="@string/menu_save"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="ifRoom|withText"/>

 <item android:id="@+id/setting"
    android:title="@string/menu_settings"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="ifRoom|withText"/>


  <item android:id="@+id/search"
    android:title="@string/menu_search"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="never"/>


   <item android:id="@+id/refresh"
    android:title="@string/menu_refersh"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="never"/>

    <item android:id="@+id/edit"
    android:title="@string/menu_edit"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="never"/>


     <item android:id="@+id/delete"
    android:title="@string/menu_delete"
    android:icon="@drawable/ic_action_search"
    android:showAsAction="never"/>

I want to display Overflow menu..so plz Suggest me .. Thanks in Advance..

Community
  • 1
  • 1
Kinjal Shah
  • 536
  • 4
  • 13
  • Also related: http://stackoverflow.com/q/13179620/813951 – Mister Smith Nov 09 '12 at 11:32
  • @MisterSmith: You beat me to that link. ;-) – Siddharth Lele Nov 09 '12 at 11:33
  • @KinjalShah: What version of ABS are you using? – Siddharth Lele Nov 09 '12 at 11:40
  • @MisterSmith FYI i have also looked this link and i use android:theme="@style/Theme.Sherlock.ForceOverflow" but it give me error.. so have u any other solution?? – Kinjal Shah Nov 09 '12 at 11:43
  • @SiddharthLele I am using Latest version of ABS.. – Kinjal Shah Nov 09 '12 at 11:46
  • I've succesfully used the approach described in the possible duplicate link with an older ABS version. But you should be aware that, in 3.0+ devices using the actual action bar, if the device has a menu key, it is not shown. That's why this force thing has been removed in the last version of ABS. It's for better, unless you want to provide an inconsistent user experience. – Mister Smith Nov 09 '12 at 11:47
  • @MisterSmith So according to u what i have to do?? i have to use older version of ABS ? – Kinjal Shah Nov 09 '12 at 11:54
  • This is a problem of the actual action bar design. ABS just mimics it. IMHO the overflow menu should be always present in the actual action bar if there are items to display, because in some phones (like Samsung ones) the menu button is hidden unless you press over it. What to do now is up to you :) . – Mister Smith Nov 09 '12 at 12:01
  • @MisterSmith ok Thanks a lot for your fast Reply... – Kinjal Shah Nov 09 '12 at 12:07
  • @MisterSmith And i want to know that i have installed ActionbarSherlock Sample demo on my Galaxy Y device and it will display overflow menu.. but i have created demo,but it will not display overflow menu...why this is happening ? – Kinjal Shah Nov 09 '12 at 12:33
  • I've elaborated on this here: http://stackoverflow.com/a/13341458/813951 – Mister Smith Nov 12 '12 at 10:02

1 Answers1

15

NOTE: With this suggestion, I am not recommending using ForceOverFlow to any reader. This is simply listing a possibility of making it work (forcing it to work rather). To each his own. Some may want it and like it too. Others may not.

I am probably speculating, but perhaps, this may do it for you.

You can think of this a hack, but I used it before to force the Overflow menu in one of my apps earlier and it works.

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();
}

Also, the second link by MisterSmith has a solution of sorts with my answer in it. Commonsware has put down some thought about forcing the OverFlow menu here: How To Control use of OverFlow Menu in ICS

EDIT: While typing out this suggestion, you added a comment. To respond to that, I would like to point out that Jake Wharton took out .ForceOverFlow themes. I haven't tried it with version 4.2.0, but with a custom theme, it just might work. If you absolutely must use ForceOverFlow, you might have to use an older version. Read my answer here: https://stackoverflow.com/a/13180285/450534. Something might just make it work.

EDIT 2: As pointed out by the OP in a comment, the Demos Sample APK, in fact, does ForceOverFlow the action bar in Action Modes. I have a feeling, after checking the relevant Java files on github, that the answer to that lies perhaps in 3 Java files.

  1. The Activity ActionModes adds menu items in a very unconventional manner: https://github.com/JakeWharton/ActionBarSherlock/blob/master/samples/demos/src/com/actionbarsherlock/sample/demos/ActionModes.java (Line 53)
  2. The ActionMode Java file in the ABS Library: https://github.com/JakeWharton/ActionBarSherlock/blob/master/library/src/com/actionbarsherlock/view/ActionMode.java
  3. The MenuItem Java file again part of the ABS Library: https://github.com/JakeWharton/ActionBarSherlock/blob/master/library/src/com/actionbarsherlock/view/MenuItem.java
Community
  • 1
  • 1
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • I have used this above code and emulate on 4.0 device ,it displays overflow menu (although it has menu button) but when i emulate on 2.3.3 device ,not displaying overflow menu... – Kinjal Shah Nov 09 '12 at 11:59
  • @KinjalShah: I suspect that may be because the necessary themes have been removed from the new version 4.2.1 – Siddharth Lele Nov 09 '12 at 12:01
  • and i read your answer , i got an idea that what i have to do..but i have still confusion that should i use older version of ABS ? – Kinjal Shah Nov 09 '12 at 12:05
  • Well, for one, you have to be aware of the pitfalls of using an older version. Read the [ChangeLog](https://github.com/JakeWharton/ActionBarSherlock/blob/master/CHANGELOG.md#readme). There are several fixes in the new version. If they are not critical to the functioning of your app, then I suppose it is worth a shot. But you must really ask yourself this. Is ForceOverFlow really worth it? – Siddharth Lele Nov 09 '12 at 12:08
  • @KinjalShah: Glad to have helped. :-) – Siddharth Lele Nov 09 '12 at 12:14
  • and i want to know that i have installed ActionbarSherlock Sample demo on my Galaxy Y device and it will display overflow menu.. but i have created demo,but it will not display overflow menu...why this is happening ? – Kinjal Shah Nov 09 '12 at 12:23
  • On pre-honeycomb devices? That is strange. You sure an older sample is not installed? – Siddharth Lele Nov 09 '12 at 12:30
  • Ya i am sure its not an older version.. http://actionbarsherlock.com/download.html from this link,i have installed Demos Sample.. – Kinjal Shah Nov 09 '12 at 12:36
  • What Android OS are you trying this one? – Siddharth Lele Nov 09 '12 at 12:38
  • Android OS is 2.3.3 and yes FYI in ActionbarSherlock Sample demo, click on Actionmode at that time it will display overflow menu and click on ActionItem , it will not display overflow menu.. – Kinjal Shah Nov 09 '12 at 12:45
  • @KinjalShah: Check EDIT 2. I may have found how the `OverFlow` menu has been possible in the sample. I think, I am on the right track. – Siddharth Lele Nov 09 '12 at 13:13
  • Perhaps @JakeWharton may be able to help here. – Siddharth Lele Nov 09 '12 at 13:14
  • What package is field coming from? – ryandawkins May 04 '13 at 04:56
  • @RyanDawkins: This is the import needed for the `Field` instance: `java.lang.reflect.Field` – Siddharth Lele May 04 '13 at 04:59
  • Alright, that's what I did. The hack still isn't working for me though. I'm using the latest version of actionbar sherlock. – ryandawkins May 04 '13 at 05:02
  • @RyanDawkins: I doubt the hack would work because with version 4.2.0+, ABS stopped supporting the `Overflow Menu`. Perhaps, if you could download the version prior to that, copy the relevant styles, attributes and themes (for the `Overflow Menu`) and plug them in the new version, _in theory_, it should work. – Siddharth Lele May 04 '13 at 05:07
  • Which directory would the styles be in? – ryandawkins May 04 '13 at 05:15
  • @RyanDawkins: In the `values` folder. A search for the term _Overflow_ should highlight them for you. Copy those in your copy of the latest ABS. Again, this is just a theory. I haven't really done any of this. I am sure the dev's had a good reason to take them out. But, if I was to need it anyway, this is what I would try. – Siddharth Lele May 04 '13 at 05:19
  • Since overflow is not supported anymore, is the actionbar just not acceptable for any device with a menu key? I wanted to use it for tab navigation. – ryandawkins May 04 '13 at 05:20
  • @RyanDawkins: I am sure what you mean by that. Why would removing one feature from ABS make the `ActionBar` unacceptable? – Siddharth Lele May 04 '13 at 05:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29400/discussion-between-ryandawkins-and-iceman) – ryandawkins May 04 '13 at 05:26