ShowcaseView icon indicating copy to clipboard operation
ShowcaseView copied to clipboard

NavigationView menu item targeting

Open Burtan opened this issue 9 years ago • 7 comments

Hi, the NavigationView inflates its menu from XML similiar to ActionBars/Toolbars. To target a special menu item with SCV, you would probably start using ActionItemTarget. However, the ActionItemTarget only looks for menu items in the ActionBar. I've tried getting the View for a menu entry (e.g. the TextView), but I have not find a way to do so, yet. I will keep you updated with my progress.

Burtan avatar Nov 16 '15 13:11 Burtan

:+1: I will have an investigate too, it may require a similar method to the ToolbarActionItemView in the sample

https://github.com/amlcurran/ShowcaseView/blob/master/sample/src/main/java/com/github/amlcurran/showcaseview/sample/ToolbarActionItemTarget.java

amlcurran avatar Nov 16 '15 13:11 amlcurran

Here is my progress.

The menu from NavigationView is inflated when onCreateOptionsMenu is called, equal to ActionBar/ToolBar menus. So you can query its childs afterwards. However, findViewById will not work, as the MenuItemViews all get assigned -1 as id. So you must know the index of the menu you want to showcase:

    NavigationMenuView navView = (NavigationMenuView) navigationView.getChildAt(0);
    ViewTarget target = new ViewTarget(navView.getChildAt(1));

Burtan avatar Nov 16 '15 14:11 Burtan

@Burtan - Awesome progress. navView.getChildAt(1) will return NavigationMenuView. How do you plan to get view of the menuitem you want?

mohitagrawal avatar Nov 18 '15 21:11 mohitagrawal

You have to use

navView.getChildAt(i));

to get the specific View you want to showcase. However, the childs also include the header and all other views added to the NavigationView. Thus you have to kinda trial and error which position is the view you want. You could also loop throw all childs and check for the first NavigationMenuItemView. I could imagine that the menuitemviews are clustered and not interrupted by any other views.

Something like this (not tested)

int itemViewsStartingPosition = 0;
for (int i = 0; i < navView.getChildCount(); i++) {
    if (navView.getChildAt(i) instanceof NavigationMenuItemView) {
        itemViewsStartingPosition = i;
        break;
    }
}

Burtan avatar Nov 19 '15 09:11 Burtan

Yeah unfortunately NavigationView has no external API (what a surprise...), so this isn't currently possible in any other way from @Burtan's example above. Will raise an issue with Google, and post it here

amlcurran avatar Nov 21 '15 15:11 amlcurran

it works. if you use handmaded drawer, use

        ViewTarget target = new ViewTarget(mDrawerList.getChildAt(i));

djdance avatar Feb 13 '16 19:02 djdance

still nothing?

markkko avatar Apr 10 '17 13:04 markkko