user2968367
user2968367

Reputation: 53

How to get Toolbar background color programmatically?

I use v7 toolbars (android.support.v7.widget.Toolbar) throughout my application. There is an easy method for setting the background color:

mToolbar.setBackgroundColor(Color.BLACK); // works!

But there is no method for getting the background color:

mToolbar.getBackgroundColor(); // Cannot Resolve Method

I want to get the background color of the toolbar so I can check if the toolbar is white. If it is white, I want to change the color of my menu icons, which are by default white, to black.

What would be the easiest way of getting the toolbar background color, if we can assume I've set it programmatically with setBackgroundColor()?

Upvotes: 3

Views: 5745

Answers (1)

ywwynm
ywwynm

Reputation: 11695

int color = ((ColorDrawable) mToolbar.getBackground()).getColor();
// do your stuff

Upvotes: 13

Related Questions