0

I've developed an application which contains a menu icon in my actionbar, I create the menu as below:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

and here's the code for the onOptionsItemSelected:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_home:
        startActivity(new Intent(this, MainActivity.class));
        return true;
    case R.id.menu_adv_search:
        startActivity(new Intent(this, AdvSearchActivity.class));
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

using my smart phone the menu icon is there (using LG phone), but when I test it on my tablet (Galaxy Tab 8) the menu icon is gone, but the functionality is still there. pressing the menu soft button at the bottom, the popup appears, but the icon is missing from the top action bar. How to fix it? any ideas?

arash moeen
  • 4,533
  • 9
  • 40
  • 85

2 Answers2

0

On devices with hardware menu buttons ( for example Galaxy series of samsung) the overflow menu behaves as the 'traditional' menu, by using the hardware menu button.

please use following attribute for each of your menu item

android:showAsAction="always"

if you are using actionbarcompat pack , then instead of above attribute use following

app:showAsAction="always"

don't forget to add the following namespace aswell

xmlns:app="http://schemas.android.com/apk/res-auto"
Sam
  • 4,046
  • 8
  • 31
  • 47
  • Thank you sam for your response, after a bit more search I've found that as you mentioned on devices with menu button hardware provided it will not work, although making the action as showAsAction="always" will show the item itself rather than the menu icon but I've found the solution at this link: http://stackoverflow.com/questions/9739498/android-action-bar-not-showing-overflow – arash moeen Nov 02 '14 at 08:55
0

As I found out, on devices with menu button hardware provided, the menu icon will be hidden and making the menu item showAsAction="always" will not bring the menu icon back it in my question I was intending to, so I've found the solution Here

and here's the code I've used from the above link to overcome this issue(if it can be called an issue):

private void getOverflowMenu() {

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

you can just put this function in your activity or base activity and call it in your onCreate function and it will do all the magic for ya.

Community
  • 1
  • 1
arash moeen
  • 4,533
  • 9
  • 40
  • 85