0

I need to know how to get code for foreground color if I know hexadecimal color for that color.

<string name="name">Asavbasb <font fgcolor="#33b5e5">look</font></string>

This is code for my color in hexadecimal #33b5e5, but if I use it in fgcolor="33b5e5", selected text is white and not blue, so my question is how can I get code for that foreground color?

Adam
  • 2,152
  • 4
  • 35
  • 45
  • what do you mean by color whether it is foreground or background.. Hex color code is the code of color. Do you want red, green, blue combination? – Chintan Rathod Jun 11 '13 at 17:30
  • No, I need to change color of my string (text). You can see it in tag , but there is only option fgcolor and my hex code #33b5e5 is for blue color, but if I use that code in fgcolor, my string (text) is white and not blue. – Adam Jun 11 '13 at 17:32
  • For example if I use my text is red, but I need to make text blue in hex color code #33b5e5. – Adam Jun 11 '13 at 17:45

4 Answers4

1

Instead of above, why don't you use the fromHTML feature?

Something like:

myTextView.setText(Html.fromHtml("Asavbasb <font fgcolor='#33b5e5'>look</font>"));

Or if you really need the color code, you can get it with Color.parseColor(String);

gunar
  • 14,660
  • 7
  • 56
  • 87
  • You don't understand my question, problem is that I need to know how to get fgcolor code for this hex color code: #33b5e5. Because if I use fgcolor='#33b5e5', my text should be blue, but insted of it, the text is white, and I don't understand why. – Adam Jun 11 '13 at 17:47
  • Updated the answer with add of Color.parseColor – gunar Jun 11 '13 at 20:34
1

Have you tried changing your color to fgcolor="#ff33b5e5"?

I think with Android in-line color you need to declare the alpha channel.

  • Unfortunately, this broke in 4.x. See TWiStErRob's explanation and work-around in http://stackoverflow.com/a/11577658/338479 – Edward Falk Jul 27 '15 at 15:08
0

this is how you can do to create color from hex string and affect it to a TextView:

final int color = Color.parseColor("#33b5e5");
final TextView text = (TextView) findViewById(R.id.text);
text.setTextColor(color);
Esteam
  • 1,911
  • 1
  • 16
  • 24
0

Prior to 4.x, you could do this:

<string name="error"><font fgcolor="#ff33b5e5">Error!</font></string>

However, bug https://code.google.com/p/android/issues/detail?id=58192 broke this functionality because it introduced an integer parser that can't handle numbers with the highest bit set, and unfortunately you can't omit the opacity part of the color (which most people would prefer to set to ff as in this example.)

I just yesterday learned a clever work-around. What you do is negate the hex color value in two's complement. How you do this depends on your hex calculator, but the easiest way is to subtract your color from 0x100000000. In your case, that would result in 0x100000000 - 0xff33b5e5 = 0xcc4a1b. (Or you could just invert it, e.g. 00cc4a1a which would be close enough). You then negate this again with a minus sign:

<string name="error"><font fgcolor="-#cc4a1b">Error!</font></string>

and voilla! you have your desired color.

Kudos to TWiStErRob for figuring this out in https://stackoverflow.com/a/11577658/338479

ETA: I just discovered that this will crash your app if you do it on a 2.x system; it throws a NumberFormat exception

Community
  • 1
  • 1
Edward Falk
  • 9,991
  • 11
  • 77
  • 112