10

Please help, I made a custom menu (added support libraries) (name-> main_activity_actions.xml)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"  >

<item
    android:id="@id/search"
    android:icon="@drawable/search"
    android:title="@string/search"
    yourapp:showAsAction="ifRoom" />
<item
    android:id="@id/view_all"
    android:title="@string/view_all"
    yourapp:showAsAction="never"/>
<item
    android:id="@+id/action_settings"
    yourapp:showAsAction="never"
    android:title="@string/action_settings"/>

Now what should i do to put action_settings into three dots (of action bar), instead of hardware menu button (Without any hack).

MainActivity

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_activity_actions, menu);
    return true;
}

well i have found the hack but if there is any other way then let me know,
Hack
put this code in onCreate

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

for this you need to import

import java.lang.reflect.Field;
import android.view.ViewConfiguration;
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Ankesh Kushwah
  • 161
  • 1
  • 3
  • 12

3 Answers3

14

If you want to show the three dots, irrespective of device menu button! then you can call this method in your application class' onCreate method-

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();
    }
}
Attif
  • 1,158
  • 13
  • 17
  • Genious. This is the only answer from google which helped me! – Ayaz Alifov May 31 '15 at 14:55
  • +1. Thank you. I was facing this matter when upgraded to sdk compile api 24 and running the app in an android api version 14 and 16. You solve. – statosdotcom Jun 29 '16 at 00:07
  • It's not working on Samsung Galaxy note I, which has got menu button, but I still was expecting to see dots since you've mentioned `irrespective of device menu button`... please can you suggest something. Thanks. – Atul Aug 15 '16 at 14:21
5

Without any hack, you cannot do that on all devices. Those devices that have the hardware menu button (I'm not sure if absolutely all) will use it instead of the overflow button (...).

Which, sort of, is a good thing. Users of those devices are used to pressing the menu button to get to the menu. So for them, the lack of the overflow button is the normal behaviour.

For those devices that use the overflow button, Android will decide itself what to put where based on your hints in showAsAction tag. It depends on the screen size, orientation, among other things. This page has a table showing how many icons are displayed (the rest goes to overflow menu).

Szymon
  • 42,577
  • 16
  • 96
  • 114
1

Please test This code for Display SherlokActionbar :

public class MainActivity extends SherlockActivity {
private com.actionbarsherlock.view.MenuItem mGoItem;
private com.actionbarsherlock.view.MenuItem mClearItem;

private static final int listSMS_ITEM_ID = 1;
private static final int Distance_ITEM_ID = 5;
private static final int About_ITEM_ID = 2;
private static final int Search_ITEM_ID = 3;
private static final int HELP_ITEM_ID = 4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public boolean onCreateOptionsMenu(Menu menu) {

    mGoItem = menu.add(0, HELP_ITEM_ID, 0, null);
    mGoItem.setIcon(R.drawable.refresh).setShowAsAction(
            MenuItem.SHOW_AS_ACTION_ALWAYS);

    mGoItem = menu.add(0, Distance_ITEM_ID, 0, null);
    mGoItem.setIcon(R.drawable.ic_launcher).setShowAsAction(
            MenuItem.SHOW_AS_ACTION_ALWAYS);

    mGoItem = menu.add(0, listSMS_ITEM_ID, 0, null);
    mGoItem.setIcon(R.drawable.refresh).setShowAsAction(
            MenuItem.SHOW_AS_ACTION_ALWAYS);

    mGoItem = menu.add(0, Search_ITEM_ID, 0, null);
    mGoItem.setIcon(R.drawable.ic_launcher).setShowAsAction(
            MenuItem.SHOW_AS_ACTION_ALWAYS);

    return true;
}
// @Override
public boolean onOptionsItemSelected(
        com.actionbarsherlock.view.MenuItem item) {
    // TODO Auto-generated method stub
    /* return super.onOptionsItemSelected(item); */

    switch (item.getItemId()) {
    case listSMS_ITEM_ID:

        Toast.makeText(getApplicationContext(), "listSMS", 1).show();


        return true;

    case Search_ITEM_ID:

        Toast.makeText(getApplicationContext(), " Search", 1).show();

        return true;




    case Distance_ITEM_ID:

        Toast.makeText(getApplicationContext(), "  Distance", 1).show();
        return true;



        case HELP_ITEM_ID:
            Toast.makeText(getApplicationContext(), "  HELP", 1).show();
            //

            return true;


    }

    return false;
}
sr.farzad
  • 665
  • 3
  • 8
  • 23