2

This question is a follow-up to Android Fullscreen dialog confirmation and dismissive actions which uses a dark theme as per the image below, and so the icons are displayed in white.

enter image description here


I want to display the "X" icon in black, because I am using a light theme.

The other question has an accepted solution for changing the ActionBar icon that looks like:

getSupportActionBar().setHomeAsUpIndicator(android.R.drawable.ic_menu_close_clear_cancel);

That is displaying a white X, even though I am using a light theme.


I seem to remember there is a way to use that provided icon, and to theme it to display in black on a light background...?

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255

1 Answers1

3

Tinting the colour of the stock icons in this way is built into support-v4 library, and described by Chris Banes on his blog:

The Drawable tinting methods added in Lollipop are super useful for letting you dynamically tint assets. AppCompat had its own baked in implementation in the v21 support library and we’ve now extracted that into DrawableCompat in support-v4 for everyone to use.

Drawable drawable = ...;

// Wrap the drawable so that future tinting calls work
// on pre-v21 devices. Always use the returned drawable.
drawable = DrawableCompat.wrap(drawable);

// We can now set a tint
DrawableCompat.setTint(drawable, Color.RED);
// ...or a tint list
DrawableCompat.setTintList(drawable, myColorStateList);
// ...and a different tint mode
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);

Which makes my exact code quite simple:

// set X close icon in black
Drawable drawable = getResources()
        .getDrawable(R.drawable.abc_ic_clear_mtrl_alpha);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, Color.BLACK);
getSupportActionBar().setHomeAsUpIndicator(drawable);

Related questions:

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255