1

Android toolbar menu items show toast hints on long press. The toast message is defined in xml with "title" attribute:

<item
    android:id="@+id/openRightMenu"
    android:orderInCategory="300"
    android:title="@string/navigation_drawer_right_desc"
    android:icon="@drawable/menu24_2"
    app:showAsAction="always"
    />

Can i set the same behaviour for the toolbar's navigation icon? I set the icon with the following code

        toolbar.setNavigationIcon(R.drawable.locations_icon);

but i can't set the description text here

Dmitrii G.
  • 895
  • 1
  • 7
  • 21
  • Not sure I understand the question but I think you will want to add a `textview` for the text and then call `textview.settext()` in your code. See this answer http://stackoverflow.com/questions/19452269/android-set-text-to-textview – EnduroDave Dec 03 '15 at 23:03
  • @Drdavidpier refer to this question http://stackoverflow.com/questions/26490126/appcompat-style-background-propagated-to-the-image-within-the-toolbar step 2. There is the hint i am talking about – Dmitrii G. Dec 03 '15 at 23:07
  • It would have to be done in your code, I believe. I don't think there's any XML attribute you can set for it. Is that acceptable? – Mike M. Dec 03 '15 at 23:26
  • Here is an example of showing a toast message under a view in the ActionBar/Toolbar: https://github.com/Mirkoddd/TabBarView/blob/master/TabBarViewLibrary/src/com/mirko/tbv/CheatSheet.java – Jared Rummler Dec 03 '15 at 23:28

1 Answers1

2

There are two ways I've used to get a Toolbar's Navigation Button View. The first uses reflection on the Toolbar class, and the second iterates over a Toolbar's child Views until it finds an ImageButton.

The reflective method:

private View getNavButtonView(Toolbar toolbar) {
    try {
        Class<?> toolbarClass = Toolbar.class;
        Field navButtonField = toolbarClass.getDeclaredField("mNavButtonView");
        navButtonField.setAccessible(true);
        View navButtonView = (View) navButtonField.get(toolbar);

        return navButtonView;
    }
    catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }

    return null;
}

And the iterative method:

private View getNavButtonView(Toolbar toolbar) {
    for (int i = 0; i < toolbar.getChildCount(); i++)
        if (toolbar.getChildAt(i) instanceof ImageButton)
            return toolbar.getChildAt(i);

    return null;
}

Please note that if you use the iterative method, it should be called immediately after setting the Navigation Icon, which should be called before any other Views are added or set on the Toolbar.

After we've gotten the View, we just need to set an OnLongClickListener on it, and show the Toast with an appropriate offset. For example:

toolbar.setNavigationIcon(R.drawable.ic_launcher);
View navButtonView = getNavButtonView(toolbar);

if (navButtonView != null) {
    navButtonView.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast toast = Toast.makeText(v.getContext(),
                                             "Navigation Button View",
                                             Toast.LENGTH_SHORT);

                int[] loc = new int[2];
                v.getLocationOnScreen(loc);

                toast.setGravity(Gravity.TOP | Gravity.LEFT,
                                 loc[0] + v.getWidth() / 2,
                                 loc[1] + v.getHeight() / 2);
                toast.show();

                return true;
            }
        }
    );
}
Mike M.
  • 38,532
  • 8
  • 99
  • 95