Android Toolbar overflow menu view id

android toolbar tutorial
android custom toolbar layout
android add button to action bar programmatically
android-toolbar title
android toolbar menu
android toolbar custom view example
material toolbar'' android
android action bar icons

What I am trying to do is showing a PopupWindow pointing to the overflow icon (the three dots) on the Toolbar. So I need to get a reference to the View object with the id of the icon. But what is the id?

The PopupWindow is used to tell the users that there are new entries added to the overflow menu. And suggest users to check it out.

The overflow menu item doesn't have a resource id. I found the overflow view by traversing the toolbar. The debugger showed an id of -1 and the Hierarchy Viewer showed no resource-id.

Here is how I found the overflow view without a resource id:

/**
 * Get the OverflowMenuButton.
 *
 * @param activity
 *     the Activity
 * @return the OverflowMenuButton or {@code null} if it doesn't exist.
 */
public static ImageView getOverflowMenuButton(Activity activity) {
  return findOverflowMenuButton(activity, findActionBar(activity));
}

static ImageView findOverflowMenuButton(Activity activity, ViewGroup viewGroup) {
  if (viewGroup == null) {
    return null;
  }
  ImageView overflow = null;
  for (int i = 0, count = viewGroup.getChildCount(); i < count; i++) {
    View v = viewGroup.getChildAt(i);
    if (v instanceof ImageView && (v.getClass().getSimpleName().equals("OverflowMenuButton") ||
        v instanceof ActionMenuView.ActionMenuChildView)) {
      overflow = (ImageView) v;
    } else if (v instanceof ViewGroup) {
      overflow = findOverflowMenuButton(activity, (ViewGroup) v);
    }
    if (overflow != null) {
      break;
    }
  }
  return overflow;
}

static ViewGroup findActionBar(Activity activity) {
  try {
    int id = activity.getResources().getIdentifier("action_bar", "id", "android");
    ViewGroup actionBar = null;
    if (id != 0) {
      actionBar = (ViewGroup) activity.findViewById(id);
    }
    if (actionBar == null) {
      return findToolbar((ViewGroup) activity.findViewById(android.R.id.content).getRootView());
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  return null;
}

static ViewGroup findToolbar(ViewGroup viewGroup) {
  ViewGroup toolbar = null;
  for (int i = 0, len = viewGroup.getChildCount(); i < len; i++) {
    View view = viewGroup.getChildAt(i);
    if (view.getClass() == android.support.v7.widget.Toolbar.class ||
        view.getClass().getName().equals("android.widget.Toolbar")) {
      toolbar = (ViewGroup) view;
    } else if (view instanceof ViewGroup) {
      toolbar = findToolbar((ViewGroup) view);
    }
    if (toolbar != null) {
      break;
    }
  }
  return toolbar;
}

Calling getOverflowMenuButton(activity) will return null in onCreate because the overflow menu isn't laid out yet. To get the overflow menu in onCreate I did the following:

findViewById(android.R.id.content).post(new Runnable() {
  @Override public void run() {
    ImageView overflow = getOverflowMenuButton(MainActivity.this);
  }
});

Set up the app bar, Overflow button has proper size too, but from misunderstood reasons, icon inside is moved closer when you select text in EditText view, Actionbar starts ActionMode with buttons to copy and paste selected text). android:id=”@+id/​toolbar” Contents in this project Overflow Menu on Action Bar Toolbar Tutorial : 1. Open the build.gradle( Module:app ) file of your project. 2. Add compile ‘com.android.support:design:26.0.0-alpha1’ inside the dependencies block. 3. Download All 5 icons from below and paste it into res -> drawable folder. These are the Overflow menu icons.

you want to create custom DropDown menu? consider this "native" way

or use android:showAsAction="never" in your menu.xml. doc of showAsAction attribute HERE. when one of MenuItems have set never value then you will get overflow three-dot icon automatically and these MenuItems will be hidding there

also you may try to use Hierarchy Viewer to investigate this id if really needed

Using the Android Toolbar (ActionBar) - Tutorial, In this post, we will walk through using custom views as menu items. a lot to help us create and interact with menu action items, those little icons on the right side of the toolbar. <item android:id="@+id/activity_main_update_menu_item"​  The Overflow Menu The overflow menu (also referred to as the options menu) is a menu that is accessible to the user from the device display and allows the developer to include other application options beyond those included in the user interface of the application. The lo

I found a library called TapTarget and a function TapTarget.forToolbarOverflow(). It presents a solution: https://github.com/KeepSafe/TapTargetView/blob/master/taptargetview/src/main/java/com/getkeepsafe/taptargetview/TapTarget.java#L96

The way how it finds the overflow view is not neat but should be stable.

How to make Android Toolbar follow Material Design guidelines, Creating and Managing Overflow Menus on Android user beyond the view components that are present in the user interface layout. While there are a number of different menu systems available to the Android application <item android:id="@+id/menu_red" android:orderInCategory="1" Toolbar; import android.view. Custom Overflow Menu with Icons. 7. In the above steps we learned how to show icons with text but suppose if we want to change the background color of overflow menu and also change the color of text view. We can achieve this thing by providing style to our overflow menu. Let’s see how we can do this:

Instead of using expensive and complicated layout traversal to find the overflow menu, I have achieved showing the PopupWindow under the overflow menu by using the Toolbar view as anchor and setting gravity to Gravity.END:

 /**
  * Sets the anchor view and shows the popup. In case of narrow display the menu items may be hidden in an overflow
  * menu, in that case anchorView may be null and the popup will be anchored to the end of the toolbar.
  */
 public void show(@Nullable View anchorView, @NonNull View toolbarView) {
     if (anchorView == null) {
         setDropDownGravity(Gravity.END);
         setAnchorView(toolbarView);
     } else {
         setAnchorView(anchorView);
     }
     show();
 }

Using Custom Views As Menu Items, This example demonstrates how do I change android overflow menu icon Toolbar android:id="@+id/toolbar" Toolbar; import android.view. Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); setSupportActionBar(myToolbar); And override onCreateOptionsMenu() to inflate your previously defined menu resource: @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.toolbar_menu, menu); return true; }

You probably created a menu item xml in your menu ressources to have an overflow button, so you must use the id of the overflow button item that you specified in your menu xml

Creating and Managing Overflow Menus on Android, Bundle; import android.support.v7.view.menu.MenuBuilder Set the support action bar mToolbar = findViewById(R.id.toolbar); mToolbar. findViewById doesn't have to be run on onOptionsItemSelected in order to get the view of the action item. however, do note that sometimes action items get to be inside the overflow menu so you might get a null instead.

How to change android overflow menu icon programmatically?, It also provides a space for the app or activity's identity, thus enabling the user to View control – This can also be used to identify the app or the specific activity the user is on by the title. To add actions to the action bar, create a XML file in the res/menu <activity android:name="com.example.actionbar. The getItemId() method queries the ID for the selected menu item, which you should assign to each menu item in XML using the android:id attribute, as shown in the section about Defining a Menu in XML. When you successfully handle a menu item, return true. If you don't handle the menu item, you should pass the menu item to the superclass implementation.

Icon on overflow menu, Toolbar is a View included in a layout like any other View; As a regular View , the toolbar is as an Activity's ActionBar, meaning that your standard options menu actions will be display within. Toolbar android:id="@+id/toolbar" android:​minHeight="?attr/actionBarSize" Refer to this stackoverflow post for more details. (If there is not enough room in the app bar, the app adds the action to the overflow menu.) When the user taps the icon or menu item, the widget expands to fill the toolbar, allowing the user to interact with it. Figure 1. When the user clicks an action view's icon, the view's UI fills the toolbar.

Better User Interfaces with the Android Action Bar, Even in this simple form, the app bar provides useful information to the users, and helps to give Android apps a consistent look and feel. Figure 1. An app bar with the app title and overflow menu. Beginning with Android 3.0 (API level 11), all activities that use the default theme have an ActionBar as an app bar.

Comments
  • No the menu items are already there. I am trying to show a popup window to tell users that there are several new options added to the overflow menu. The popup window will make the new entries in the overflow menu more discoverable.
  • maybe try to use Hierarchy Viewer to investigate this id
  • It's a hacky solution that may not work on newer android version.
  • As long as you use androidx.appcompat.widget.Toolbar instead of the native android.widget.Toolbar it only depends on the support library version, not the Android version. That is something that can easily be tested each time the support library gets updated.