Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Action Bar Three Dots not displayed

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;
like image 718
Ankesh Kushwah Avatar asked Nov 02 '13 11:11

Ankesh Kushwah


People also ask

What are the three dots on Android called?

The kebab menu, also known as the three dots menu, and the three vertical dots menu, is an icon used to open a menu with additional options. The icon is most often located at the top-right or top-left of the screen or window. The picture shows an example of the kebab menu icon in Google Chrome.

How do I replace my action bar toolbar?

To replace an app's default action bar with a Toolbar : Create a new custom theme and modify the app's properties so that it uses this new theme. Disable the windowActionBar attribute in the custom theme and enable the windowNoTitle attribute. Define a layout for the Toolbar .

What are the three dots on a mobile app called?

Ellipsis = “More actions” More and more apps are now using a midline ellipsis (⋯) to indicate a menu with more actions. It basically means “Hey, there's more stuff you can do here.” In many Android apps, you'll often see a vertical ellipsis (⋮) to mean the same thing.


3 Answers

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

like image 73
Szymon Avatar answered Oct 14 '22 16:10

Szymon


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();
    }
}
like image 21
Attif Avatar answered Oct 14 '22 14:10

Attif


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;
}
like image 44
sr.farzad Avatar answered Oct 14 '22 16:10

sr.farzad