Home » Android » android – how to force overflow menu in Actionbar using Actionbarsherlock on < 4.0 devices

android – how to force overflow menu in Actionbar using Actionbarsherlock on < 4.0 devices

Posted by: admin June 15, 2020 Leave a comment

Questions:

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..

How to&Answers:

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