9

I'm making an app and I'd like to be able to set various colors via user input(edittext) and hex values e.g. #eeeeee and so on. Problem is I cannot seem to figure out how to convert them.

If I do something in code like this it works fine: titlebar.setBackgroundColor(0xFF545455);

However if I retrieve a value via the edittext say "545455" I cannot get it work

          String tbColor = tb_color.getText().toString();             
          String value = "0xFF" + tbColor;  
          int setColor = Integer.valueOf(value);
          titlebar.setBackgroundColor(setColor);

Anyone have any ideas on how to accomplish this?

thejh
  • 44,854
  • 16
  • 96
  • 107
Paul
  • 1,714
  • 7
  • 22
  • 45
  • Possible duplicate of [Converting android color string in runtime into int](http://stackoverflow.com/questions/3849607/converting-android-color-string-in-runtime-into-int) – Shubham A. Feb 25 '16 at 05:49

4 Answers4

51

What about titlebar.setBackgroundColor(Color.parseColor("#545455"));

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Shadi
  • 2,236
  • 2
  • 22
  • 22
20

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String, int)

For example:

titlebar.setBackgroundColor(Integer.parseInt("545455", 16)+0xFF000000);
thejh
  • 44,854
  • 16
  • 96
  • 107
  • That produces a value of 5526613 and doesn't correctly set to the right color. – Paul Nov 19 '10 at 21:29
  • Worked perfectly! But please could you help me understand why adding: "+0xFF000000" made it work?? – Luis Pena May 19 '14 at 16:03
  • Luis Alberto, because the 545455 replaces the 000000 after the 0xFF is just like: 850 + 100000 = 100850 (but in hexadecimal format) BTW 0xFF defines the alpha (transparency) – Allan Ramírez Sep 10 '14 at 17:35
0

here is my function to get colors from ANY string. Eg: "Hello World!" will return you some green color of R G B: 84 181 132

public static int getColorFromString(String string) {
    int[] RGB = {0,0,0};
    int l = string.length();
    String sub_string_0 = string.substring(0, (int) Math.ceil((double) l / 3));                 // responsable for Red
    int l_0 = sub_string_0.length();
    String sub_string_1 = string.substring(l_0,  l_0 + (int) Math.ceil((double) (l - l_0)/2));  // responsable for Green
    String sub_string_2 = string.substring(l_0 + sub_string_1.length(), string.length());       // responsable for Blue

    String[] sub_string = new String[]{
            sub_string_0,
            sub_string_1,
            sub_string_2
    };
    for(int i = 0; i < sub_string.length; i++) {
        if(sub_string[i].length()==0)
            sub_string[i] = " ";                                                                // we asign a value (a space)
        Log.d("sub_string", i + " " + sub_string[i]);
        for (char c : sub_string[i].toCharArray()) {
            int c_val = Character.getNumericValue(c) - Character.getNumericValue('a');          // for 'a' -> 0     for 'z' -> 25
            if(c_val < 0)                                                                       //  spaces, numbers ...
                c_val= new Random().nextInt(25);                                                //add some salt - a random number
            Log.d("value ", c + " -> " + c_val);
            RGB[i] = RGB[i] + c_val;
        }
    }

    int letters_number = Character.getNumericValue('z') - Character.getNumericValue('a');       //  z - a    35 - 10

    // normalizing
    int R = 255 * RGB[0]/sub_string[0].length()/letters_number;
    int G = 255 * RGB[1]/sub_string[1].length()/letters_number;
    int B = 255 * RGB[2]/sub_string[2].length()/letters_number;
    Log.d("R G B", R +" " + G + " "  + B);

    return Color.rgb(R, G, B);
}

Note: The function does not return the same number(color) each time if your string includes special characters, spaces or numbers. There is some salt there - I asigned random numbers to those characters, just for fun...

Dan Alboteanu
  • 9,404
  • 1
  • 52
  • 40
0
StringBuffer hexString = new StringBuffer();
hexString.append(Integer.toHexString(0xFF);
System.out.print(hexString.toString());
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
jayesh kavathiya
  • 3,531
  • 2
  • 22
  • 25