0

I get the following string value from the db: "rgb(105, 105, 105)"

I tried Color.parseColor() but it wasn't right option.

I try to use it with view.setBackgroundColor()

Is there a way for this in Java/Android?

L. Fourde
  • 49
  • 5
  • 1
    If you control the format in the database, I'd suggest you change it to one that `parseColor()` will handle; e.g., `#aabbcc`. If not, then you're going to have to parse that yourself, I believe. – Mike M. Feb 15 '18 at 03:34
  • plz search before asking anything https://stackoverflow.com/questions/18022364/how-to-convert-rgb-color-to-int-in-java – Hai Hack Feb 15 '18 at 03:46

2 Answers2

3

For "rgb(105, 105, 105)" this format you have to parse it manually. Try this code:

  try{

    String str = "rgb(105, 105, 105)";
    String splitStr = str.substring(str.indexOf('(') + 1, str.indexOf(')'));
    splitString = splitStr.split(",");

    int colorValues[] = new int[splitString.length];
    for (int i = 0; i < splitString.length; i++) {
        colorValues[i] = Integer.parseInt(splitString[i].trim());
    }

    int color = Color.rgb(colorValues[0], colorValues[1],colorValues[2]);
    view.setBackgroundColor(color); 

 }catch(Exception ex){

 }
Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
1

Short and Simple:

view.setBackgroundColor(Color.rgb(105, 105, 105));

Edit: Mr Abu has given answer with parsing. Its complete answer, use it. I will discard my answer.

Palak Darji
  • 1,084
  • 18
  • 28