-1

in java i have

color_value="FFAAFF";

I have tried:

int color = Integer.parseInt(color_value);`

How can I convert it to int(hex) to add to im_view.setBackgroundColor(int color);?

  • 3
    Please [edit] your question to include the language this is in (Java?). Thanks for improving the question's reference value and making it more answerable! – Nathan Tuggy Oct 09 '15 at 02:08
  • Your question is no about convert from String to Int, it's about converting from HEX, to another pattern like RGB, etc, in what pattern do you need it, also what prog. lang are u working on? – Jonathan Solorzano Oct 09 '15 at 02:19

1 Answers1

0

As we have to guess the lang you're using, i'll assume it's java, what you actually need is to convert from HEX to RGB, if you are comfortable with AWT then you could use a built in func:

String color_value = "FFAAFF";
im_view.setBackgroundColor(Color.decode(color_value));

If don't want to use AWT, then you could do it like this:

/**
 * 
 * @param colorStr e.g. "#FFFFFF"
 * @return 
 */
public static Color hex2Rgb(String colorStr) {
    return new Color(
            Integer.valueOf( colorStr.substring( 1, 3 ), 16 ),
            Integer.valueOf( colorStr.substring( 3, 5 ), 16 ),
            Integer.valueOf( colorStr.substring( 5, 7 ), 16 ) );
}



im_view.setBackgroundColor(hex2Rgb(color_value));
Community
  • 1
  • 1
Jonathan Solorzano
  • 6,812
  • 20
  • 70
  • 131