( ), , , AppBarLayout
/Toolbar
. , , , / , ImageButtons
. ( "" (!)..):
public static void colouriseToolbar(AppBarLayout appBarLayout, @ColorInt int background, @ColorInt int foreground) {
if (appBarLayout == null) return;
appBarLayout.setBackgroundColor(background);
final Toolbar toolbar = (Toolbar)appBarLayout.getChildAt(0);
if (toolbar == null) return;
toolbar.setTitleTextColor(foreground);
toolbar.setSubtitleTextColor(foreground);
final PorterDuffColorFilter colorFilter
= new PorterDuffColorFilter(foreground, PorterDuff.Mode.MULTIPLY);
for (int i = 0; i < toolbar.getChildCount(); i++) {
final View view = toolbar.getChildAt(i);
Log.d(Globals.TAG, "view: "+i+" "+view.getClass().toString());
if (view instanceof ImageButton) {
((ImageButton)view).getDrawable().setColorFilter(colorFilter);
}
if (view instanceof ActionMenuView) {
for (int j = 0; j < ((ActionMenuView) view).getChildCount(); j++) {
final View innerView = ((ActionMenuView)view).getChildAt(j);
if (innerView instanceof ActionMenuItemView) {
Log.d(Globals.TAG, "view (actionmenuitemviwe): "+i);
final Drawable[] drawables = ((ActionMenuItemView)innerView).getCompoundDrawables();
for (int k = 0; k < drawables.length; k++) {
final Drawable drawable = drawables[k];
if (drawable != null) {
final int drawableIndex = k;
innerView.post(new Runnable() {
@Override
public void run() {
((ActionMenuItemView) innerView).getCompoundDrawables()[drawableIndex].setColorFilter(colorFilter);
}
});
}
}
}
}
}
}
Drawable overflowIcon = toolbar.getOverflowIcon();
if (overflowIcon != null) {
overflowIcon.setColorFilter(colorFilter);
toolbar.setOverflowIcon(overflowIcon);
}
}