It seems that the class Toolbardynamically creates a child View, so I believe that we will have to look for the logo Viewourselves. After you have installed the logo, but before adding any other (if this is true) the logo is the only ImageViewchild element Toolbarthat we can do this:
private ImageView getLogoView(Toolbar toolbar) {
for (int i = 0; i < toolbar.getChildCount(); i++)
if(toolbar.getChildAt(i) instanceof ImageView)
return (ImageView) toolbar.getChildAt(i);
return null;
}
Another possibility is to use reflection in the classroom Toolbar. This method can be used at any time after installing the logo.
private ImageView getLogoView(Toolbar toolbar) {
try {
Class<?> toolbarClass = Toolbar.class;
Field logoViewField = toolbarClass.getDeclaredField("mLogoView");
logoViewField.setAccessible(true);
ImageView logoView = (ImageView) logoViewField.get(toolbar);
return logoView;
}
catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
source
share