#TODO
- Color text menu items as well as coloring icon's in the menu
import android.support.v7.widget.Toolbar; | |
import android.view.MenuItem; | |
import android.widget.ImageView; | |
/** | |
* Created by braunster on 14.07.15. | |
*/ | |
public class ToolbarColorizer { | |
public static void colorToolbar(Toolbar toolbar, int color){ | |
toolbar.setNavigationIcon(ViewUtils.getTintedDrawable(toolbar.getNavigationIcon(), color)); | |
ImageView overflow = ViewUtils.getOverflowMenu(toolbar); | |
if (overflow != null){ | |
overflow.setImageDrawable(ViewUtils.getTintedDrawable(overflow.getDrawable(), color)); | |
} | |
MenuItem item; | |
for (int i = 0; i < toolbar.getMenu().size(); i++){ | |
item = toolbar.getMenu().getItem(i); | |
item.setIcon(ViewUtils.getTintedDrawable(item.getIcon(), color)); | |
} | |
} | |
} |
import android.content.res.Resources; | |
import android.graphics.PorterDuff; | |
import android.graphics.drawable.Drawable; | |
import android.support.annotation.ColorRes; | |
import android.support.annotation.DrawableRes; | |
import android.support.annotation.Nullable; | |
import android.support.v7.internal.widget.TintImageView; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
/** | |
* Created by braunster on 10/04/15. | |
* | |
*/ | |
public class ViewUtils { | |
@Nullable | |
public static Drawable getTintedDrawable(Resources res, | |
@DrawableRes int drawableResId, @ColorRes int colorResId) { | |
Drawable drawable = res.getDrawable(drawableResId); | |
int color = res.getColor(colorResId); | |
return getTintedDrawable(drawable, color); | |
} | |
/** | |
* Code has been taken from this blog post: http://blog.danlew.net/2014/08/18/fast-android-asset-theming-with-colorfilter/ | |
* You should read it. | |
*/ | |
@Nullable | |
public static Drawable getTintedDrawable(Drawable drawable, int color) { | |
if (drawable == null) | |
return null; | |
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); | |
return drawable; | |
} | |
/** | |
* Used this gist as base: https://gist.github.com/jaredrummler/ec7dfb73f3235ad8e951 | |
* Check it out for more options. | |
**/ | |
public static ImageView getOverflowMenu(final ViewGroup group) { | |
ImageView overflow = null; | |
for (int i = 0, l = group.getChildCount(); i < l; i++) { | |
final View v = group.getChildAt(i); | |
if (v instanceof TintImageView) | |
{ | |
if (v.getClass().getSimpleName().equals("OverflowMenuButton")) { | |
overflow = (ImageView) v; | |
} | |
} | |
else if (v instanceof ViewGroup) { | |
overflow = getOverflowMenu((ViewGroup) v); | |
} | |
} | |
return overflow; | |
} | |
} |