basickarl
basickarl

Reputation: 40464

Android: Get Color object from Resource

Color color = new Color(context.getResources().getColor(R.color.bus_departures_hover));

As seen, I am attemping to create a Color object from a resource. This ain't working though!

Upvotes: 5

Views: 5281

Answers (3)

Cafer Mert Ceyhan
Cafer Mert Ceyhan

Reputation: 1756

Kotlin way (API REQUIRED 26);

Color.valueOf(ContextCompat.getColor(context, R.color.color_white))

Upvotes: 1

Janx from Venezuela
Janx from Venezuela

Reputation: 1197

What has worked for me is:

Color c = new Color(ContextCompat.getColor(context, R.color.yourColor));

Upvotes: 10

Raúl García
Raúl García

Reputation: 344

It seems there's no API constructor that receives an int http://developer.android.com/reference/android/graphics/Color.html#Color()

You may use

 int color= getResources().getColor(R.color.bus_departures_hover);

And use the color value in a setter.

Upvotes: 0

Related Questions