0

I searched a lot but cannot find a solution.

From the API it returns an Int as a color value 2813300. This is a tone of Green LiNK to COLOR

I try to use this Int as a Color in my project but I cannot convert it to android.graphics.Color.

When I try to use the int it is not working.

How can I convert these numbers to Color?

Examples:

RED = 16711936 (Link)

GREEN = 2813300 (Link)

Alper Aslan
  • 61
  • 2
  • 7

2 Answers2

5

Integer.toString will convert your int value to Hex value

 Integer.toString(2813300, 16)

result :

2AED74

And you can use the Hex color like this :

String Hex  = Integer.toString(2813300, 16);
myLayout.setBackgroundColor(Color.parseColor("#" + Hex));
anehme
  • 536
  • 1
  • 6
  • 18
1

At first, I found what I did wrong.

My code was calling another method which gets the color from resources when I use 2813300 it could not be found in resources.

Below code was not working

myLayout.setBackground(ContextCompat.getColor(context!!, color), PorterDuff.Mode.SRC_IN)

When I changed it to below it worked.

myLayout.setBackground(2813300-1677216, PorterDuff.Mode.SRC_IN)

(answer from here) - if you want to know why i used -16777216 visit the link

var color = 2813300 - 16777216
dialogBody.background.setColorFilter(color, PorterDuff.Mode.SRC_IN)

But also the previous answer is perfect to use. Thank you Anehme

Alper Aslan
  • 61
  • 2
  • 7