Sai
Sai

Reputation: 15738

Parsing HEX '0x0976b8' to color android

I tried below but it throws an saying error during parsing the color. Thanks in advance.

hexColorStr = "0x0976b8"
view.setBackgroundColor(Color.parseColor(String.format("#%06X", Integer.parseInt(hexColorStr)))

UPDATE: For some reason removing 0x in "0x0976b8" worked for me.

view.setTextColor(Color.parseColor("0x0976b8".replace("0x","#")));

Is there any reason why i have to remove 0x form "0x0976b8" to make it work. Thanks in advance..

Upvotes: 2

Views: 209

Answers (3)

rafsanahmad007
rafsanahmad007

Reputation: 23881

try:

view.setBackgroundColor(Integer.parseInt(hexColorStr))

your string is hexadcimal format it will return

int color = 0xFFFF0000;

If you provide 6 hex digits, that means RGB (2 hex digits for each value of red, green and blue).

If you provide 8 hex digits, it's an ARGB (2 hex digits for each value of alpha, red, green and blue respectively).

see the Documentation

Upvotes: 2

zMabrook
zMabrook

Reputation: 942

try

view.setBackgroundColor(Color.parseColor("#0976b8"));

Upvotes: 0

dev.bmax
dev.bmax

Reputation: 10621

Parse color expects a certain format: #RRGGBB #AARRGGBB.

Change to view.setBackgroundColor(Color.parseColor("#0976b8"));

Upvotes: 1

Related Questions