Get Toolbar's navigation icon view reference
Asked Answered
E

4

19

I would like to highlight drawer icon in my Toolbar (working on a tutorial). For that, I need its position. How do I get a reference to drawer's navigation icon (hamburger) view?

Examine answered 28/4, 2015 at 15:4 Comment(4)
((ViewGroup)yourtoolbar).get(0); it might be 0 or 1 but im sure its zero, btw is this what you want? it will return a View and that View is your drawer icon maybe an imageButtonFlatiron
What do you mean "highlight" the drawer icon?Goblet
Are you sure you need the view to highlight? you can get the drawable used for the icon for example.Logical
@Flatiron Why is casting toolbar to ViewGroup required? Toolbar extends ViewGroup, so it isn't, or am I missing something? Is this what you meant: View drawerIcon = toolbar.getChildAt(0); It does not return a correct view (it returns a view which is an image in the middle of my toolbar). @JaredBurrows It is not important, but if you're interested, I developed a tutorial module which takes a view as a parameter and highlights it by making the screen dark, while leaving the circle around the target view visible. I want navigation icon in toolbar to be highlighted.Examine
B
29

You can make use of content description of the view and then use findViewWithText() method to get view reference

 public static View getToolbarNavigationIcon(Toolbar toolbar){
        //check if contentDescription previously was set
        boolean hadContentDescription = !TextUtils.isEmpty(toolbar.getNavigationContentDescription());
        String contentDescription = hadContentDescription ? toolbar.getNavigationContentDescription() : "navigationIcon";
        toolbar.setNavigationContentDescription(contentDescription);
        ArrayList<View> potentialViews = new ArrayList<View>();
        //find the view based on it's content description, set programatically or with android:contentDescription
        toolbar.findViewsWithText(potentialViews,contentDescription, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
        //Nav icon is always instantiated at this point because calling setNavigationContentDescription ensures its existence 
        View navIcon = null;
        if(potentialViews.size() > 0){
            navIcon = potentialViews.get(0); //navigation icon is ImageButton
        }
         //Clear content description if not previously present
        if(!hadContentDescription)
            toolbar.setNavigationContentDescription(null);
        return navIcon;
     }

More

Kotlin extension property:

val Toolbar.navigationIconView: View?
        get() {
            //check if contentDescription previously was set
            val hadContentDescription = !TextUtils.isEmpty(navigationContentDescription)
            val contentDescription = if (hadContentDescription) navigationContentDescription else "navigationIcon"
            navigationContentDescription = contentDescription
            val potentialViews = arrayListOf<View>()
            //find the view based on it's content description, set programatically or with android:contentDescription
            findViewsWithText(potentialViews, contentDescription, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION)
            //Clear content description if not previously present
            if (!hadContentDescription) {
                navigationContentDescription = null
            }
            //Nav icon is always instantiated at this point because calling setNavigationContentDescription ensures its existence
            return potentialViews.firstOrNull()
        }
Bullish answered 29/4, 2015 at 22:57 Comment(3)
Interesting, but why do we have to do this. Why isn't there something like toolbar.getNavigationIconView()Examine
hadContentDescription is true when isEmpty, besides that "misspell" works well :)Allstar
@Allstar You're right, I have edited the answer and fixed the bug.Archduchy
E
8

After looking into Toolbar's child views in debug mode, I saw that drawer icon can be found there, as an ImageButton. (Thanks Elltz)

I use a Toolbar with custom xml layout with 2 children (LinearLayout and ImageView), so my Toolbar had 4 children in the end, with these positions:

[0] LinearLayout(from custom xml)
[1] ImageView(from custom xml)
[2] ImageButton(drawer icon)
[3] ActionMenuView(menu icon)

Knowing this, I can now use:

View drawerIcon = toolbar.getChildAt(2);

to get a reference to drawer menu icon. In my case, the position is 2. This position should be equal to the number of child view's in your custom toolbar layout.

If someone finds better solution please let me know.

Examine answered 29/4, 2015 at 22:52 Comment(0)
S
8

If you just want to get the Drawable representing the toolbar navigation icon, you can do this:

Drawable d = mToolbar.getNavigationIcon();

You can get a reference to the ImageButton used for the toolbar's navigation icon by having a method as this:

public ImageButton getToolbarNavigationButton() {
    int size = mToolbar.getChildCount();
    for (int i = 0; i < size; i++) {
        View child = mToolbar.getChildAt(i);
        if (child instanceof ImageButton) {
            ImageButton btn = (ImageButton) child;
            if (btn.getDrawable() == mToolbar.getNavigationIcon()) {
                return btn;
            }
        }
    }
    return null;
}
Smoky answered 11/7, 2016 at 23:44 Comment(0)
M
0

Improvised @Nikola Despotoski's answer

public static View getNavigationIconView(Toolbar toolbar) { 

    String previousContentDescription = (String) toolbar.getNavigationContentDescription();
    // Check if contentDescription previously was set
    boolean hadContentDescription = !TextUtils.isEmpty(previousContentDescription);
    String contentDescription = hadContentDescription ? 
            previousContentDescription : "navigationIcon";
    toolbar.setNavigationContentDescription(contentDescription);

    ArrayList<View> potentialViews = new ArrayList<>();
    // Find the view based on it's content description, set programmatically or with
    // android:contentDescription
    toolbar.findViewsWithText(potentialViews, contentDescription,
            View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);

    // Nav icon is always instantiated at this point because calling
    // setNavigationContentDescription ensures its existence
    View navIcon = null;
    if (potentialViews.size() > 0) {
        navIcon = potentialViews.get(0); //navigation icon is ImageButton
    }

    // Clear content description if not previously present
    if (!hadContentDescription)
        toolbar.setNavigationContentDescription(previousContentDescription);

    return navIcon;
}
Mcgray answered 26/7, 2018 at 6:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.