简体   繁体   中英

Android burger/arrow icon dynamic change color

I want to change color of burger/arrow icon of navigation drawer. I know I can change it in styles, but I want change it dynamically in java. Did anybody know how to do this?

Using appcompat-v7:23.0.1 next code worked for me:

int color = Color.parseColor("#FFEEEE00");
final PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP);

for (int i = 0; i < toolbar.getChildCount(); i++) {
    final View v = toolbar.getChildAt(i);

    if (v instanceof ImageButton) {       
        ((ImageButton) v).setColorFilter(colorFilter);
    }
}

Use it in public boolean onCreateOptionsMenu(Menu menu)

You can use setTint of the new DrawableCompat class (from support v4 lib)

// Get the icon you want as a drawable
Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_menu, null);
// "Enable" tinting process
drawable = DrawableCompat.wrap(drawable);
// Tint it
DrawableCompat.setTint(drawable, Color.BLACK);
// Set it as action bar icon
actionBar.setHomeAsUpIndicator(drawable);

For more details about drawable tinting see Chris Bane post about support lib V22.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM