Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert R.color to Color?

Android has 2 types of colors: R.color and color Layout uses R.color (I need holo_blue_light: 17170450 (0x01060012))

but functions (such as setColor()) have the other type of input int (i.e. CYAN: -16711681 (0xff00ffff)).

Negation of R.color returns incorrect colors. What should I do to convert them?

like image 619
Sieva Kimajeŭ Avatar asked Jul 13 '13 10:07

Sieva Kimajeŭ


2 Answers

Since getResources().getColor is now deprecated, you can use:

ContextCompat.getColor(getResources(), R.color.idOfColour)

old answer

Use

 getResources().getColor(R.color.idOfColour);

it returns the int color you are looking for. If the colour comes with Android you can get its id with android.R.color.colourId

like image 105
Blackbelt Avatar answered Sep 18 '22 18:09

Blackbelt


  1. Color from resources you get through

    getResources().getColor(R.color.color_id);

  2. Color that you had saved from a view (say background color or text color), which will look like your second example, you may get through

Color.parseColor(String color)

like image 33
Joel Bodega Avatar answered Sep 20 '22 18:09

Joel Bodega