0

i was trying to make the custom overflow button with new drawable source start an activity instead of showing drop down menu. I've read somewhere that I have to make sure there's only one item in onOptionItemSelected, so here's my code:

public boolean onOptionsItemSelected(MenuItem item) {
    // Inflate the menu; this adds items to the action bar if it is present.
    switch(item.getItemId()){
        case R.id.logout_button:
            Intent i = new Intent(PetVacActivity.this, LoginActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            SharedPreferences sharedpreferences = getSharedPreferences(LoginActivity.MyPREFERENCES, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.clear();
            editor.commit();

            startActivity(i);
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }

}

but unfortunately, my app still showing the overflow button, but there's only one blank drop down menu like this

enter image description here instead of directly start my activity, i have to click the dropdown menu first to activate my intent

3
  • are you using toolbar ? Commented Dec 9, 2015 at 10:05
  • menu xml : android:showAsAction="ifRoom" on all items Commented Dec 9, 2015 at 10:09
  • @Arslan yes, i tried hideoverflowmenu() but the drop down still visible Commented Dec 9, 2015 at 10:56

5 Answers 5

3

In menu.xml u can add

android:showAsAction="" 

with these values

never,ifRoom,always,withText,collapseActionView 

you can use them in | combinations.

in you case you can use

android:showAsAction="always"
Sign up to request clarification or add additional context in comments.

2 Comments

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" > <item android:id="@+id/logout_button" android:icon="@drawable/logout" android:showAsAction="always"/> </menu>
finally get it working! I only had to change "android:showAsAction" to "app:showAsAction"
1

finally get it working! I only had to change "android:showAsAction" to "app:showAsAction". Thank you for your attention :)

Comments

0

Just call hideoverflowMenu function of toolbar like this.

toolbar.hideOverflowMenu();

Comments

0

You can use submenu and with own icon

SubMenu subMenu = menu.addSubMenu("MyOverflow").setIcon(R.drawable.ic_my_overflow);
MenuItemCompat.setShowAsAction(subMenu.getItem(), MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
MenuItem menuItem = subMenu.add("SubMenuItem1");
MenuItem menuItem2 = subMenu.add("SubMenuItem1");

Comments

0

You could try one of these two:

  1. Using the menu defined in an xml file

Menu xml file named 'my_menu' (using the appcompat library):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/log_out_menu_item"
          android:icon="@drawable/ic_log_out"
          android:title=""
          app:showAsAction="ifRoom"/>
</menu>

Activity code:

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
        case R.id.log_out_menu_item:
            // start new activity here
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
  1. Adding the menu item by code

Activity code:

private static final int LOG_OUT_MENU_ITEM_ID = 1;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem menuItem = menu.add(0, LOG_OUT_MENU_ITEM_ID, 0, "");
    menuItem.setIcon(R.drawable.ic_launcher_actionbar);
    MenuItemCompat.setShowAsAction(menuItem, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    // Inflate the menu; this adds items to the action bar if it is present.
    switch(item.getItemId()){
        case LOG_OUT_MENU_ITEM_ID:
            // start new activity here
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.