I have been looking a way to change the color of all elements in a toolbar working like an ActionBar dynamically.
Specifications:
- Using
parent="Theme.AppCompat.Light.NoActionBar"
on styles.xml - Appcompat v7 22
- setting
setSupportActionBar()
in myAppCompatActivity
- I got the colors from a POST request (usually #FF------ format)
I have read following post:
- How do I change the color of the ActionBar hamburger icon?
- How to change color of hamburger icon in material design navigation drawer
- Can't change navigation drawer icon color in android
- ActionBarDrawerToggle v7 arrow color
- Android Toolbar color change
- Android burger/arrow icon dynamic change color (this one worked in someway but I don't like using own image wihtout animation).
And others links related to this topic... none of them worked for me.
What I'm doing right now is searching for ImageButton on the toolbar (Get reference to drawer toggle in support actionbar), and applying setColorFilter()
to all of them like the following code:
for (int i = 0; i < toolbar.getChildCount(); i++){
if (toolbar.getChildAt(i) instanceof ImageButton) {
ImageButton ib = (ImageButton) toolbar.getChildAt(i);
ib.setColorFilter(Color.parseColor("#A74231"), PorterDuff.Mode.SRC_ATOP);
}
}
I'm changing background and text color with: toolbar.setBackgroundColor
and toolbar.setTitleTextColor
.
For menu icons (including overflow menu icon):
MenuItem item2 = mMenu.findItem(R.id.actionbar_group_moreoverflow);
item2.getIcon().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
the QUESTION: is there a better way to do it (change toolbar's elements color dynamically)?