Nikhil S
Nikhil S

Reputation: 72

Android converting color string to resource int

I am trying to set color from a json color array to my custom menu background.

addMenuItem(int bgColor);

json color array =>

[
  [
    "B71C1C",
    "C62828"],[
    "B71C1C",
    "C62828"]
]

Color String is getting correctly from json

String colorString = "#" + mColors.getJSONArray(i%2).getString(i%2); gives #B71C1C;

While convert it to integer int parseColor = Color.parseColor(colorString); and passing to above menu background;

ie;addMenuItem(parseColor);

I'm getting

android.content.res.Resources$NotFoundException: Resource ID #0xffb71c1c

I think addMenuItem will accept int only from resource. How can I set color from my JSON color file.

Upvotes: 1

Views: 471

Answers (1)

0xDEADC0DE
0xDEADC0DE

Reputation: 2473

That's because you are throwing in a number and the Android framework tries to find a resource for that (R.color) which doesn't exist. You should convert that number to a Color object, using Color.argb(int,int,int,int) for example

Upvotes: 4

Related Questions