Java – Android hamburger/arrow icons change color dynamically

Android hamburger/arrow icons change color dynamically… here is a solution to the problem.

Android hamburger/arrow icons change color dynamically

I want to change the color of the hamburger/arrow icon for drawer navigation. I know I can change it in styles, but I want to change it dynamically in java. Does anyone know how?

Solution

Using appcompat-v7:23.0.1 the 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).

Related Problems and Solutions