Get Toolbar's Navigation Icon View Reference
Solution 1:
You can make use of content description of the view and then use findViewWithText()
method to get view reference
publicstatic View getToolbarNavigationIcon(Toolbar toolbar){
//check if contentDescription previously was setbooleanhadContentDescription= !TextUtils.isEmpty(toolbar.getNavigationContentDescription());
StringcontentDescription= hadContentDescription ? toolbar.getNavigationContentDescription() : "navigationIcon";
toolbar.setNavigationContentDescription(contentDescription);
ArrayList<View> potentialViews = newArrayList<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 ViewnavIcon=null;
if(potentialViews.size() > 0){
navIcon = potentialViews.get(0); //navigation icon is ImageButton
}
//Clear content description if not previously presentif(!hadContentDescription)
toolbar.setNavigationContentDescription(null);
return navIcon;
}
Kotlin extension property:
val Toolbar.navigationIconView: View?
get() {
//check if contentDescription previously was setval 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 presentif (!hadContentDescription) {
navigationContentDescription = null
}
//Nav icon is always instantiated at this point because calling setNavigationContentDescription ensures its existencereturn potentialViews.firstOrNull()
}
Solution 2:
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:
ViewdrawerIcon= 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.
Solution 3:
If you just want to get the Drawable
representing the toolbar navigation icon, you can do this:
Drawabled= 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() {
intsize= mToolbar.getChildCount();
for (inti=0; i < size; i++) {
Viewchild= mToolbar.getChildAt(i);
if (child instanceof ImageButton) {
ImageButtonbtn= (ImageButton) child;
if (btn.getDrawable() == mToolbar.getNavigationIcon()) {
return btn;
}
}
}
returnnull;
}
Solution 4:
Improvised @Nikola Despotoski's answer
publicstatic View getNavigationIconView(Toolbar toolbar) {
String previousContentDescription = (String) toolbar.getNavigationContentDescription();
// Check if contentDescription previously was setboolean 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 presentif (!hadContentDescription)
toolbar.setNavigationContentDescription(previousContentDescription);
return navIcon;
}
Post a Comment for "Get Toolbar's Navigation Icon View Reference"