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?
You can similarity assign to Main Menu-> View-> Toolbar and show toolbar again on Android studio IDE. Alternatively, after the main menu opened, click VIEW-> Toolbar tab. Save this answer.
Customize Back Button in Action Bar We can easily Customize the Back Button by using the getSupportActionBar() library and setting the drawable file using setHomeAsUpIndicator in the java/kotlin file. actionBar. setHomeAsUpIndicator(R. drawable.
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()
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With