Convert hex color value ( #ffffff ) to integer value

前端 未结 9 986
孤城傲影
孤城傲影 2020-12-13 01:26

I am receiving hex color values from a server (in this form, #xxxxxx , example #000000 for black)

How do I convert this to an integer value

相关标签:
9条回答
  • 2020-12-13 01:46

    I was facing the same problem. This way I was able to solved it. As CQM said, using Color.parseColor() is a good solution to this issue.

    Here is the code I used:

    this.Button_C.setTextColor(Color.parseColor(prefs.getString("color_prefs", String.valueOf(R.color.green))));
    

    In this case my target was to change the Button's text color (Button_C) when I change the color selection from my Preferences (color_prefs).

    0 讨论(0)
  • 2020-12-13 01:53

    I have the same problem that I found some color in form of #AAAAAA and I want to conver that into a form that android could make use of. I found that you can just use 0xFFAAAAAA so that android could automatically tell the color. Notice the first FF is telling alpha value. Hope it helps

    0 讨论(0)
  • 2020-12-13 01:54

    Answer is really simple guys, in android if you want to convert hex color in to int, just use android Color class, example shown as below

    this is for light gray color

    Color.parseColor("#a8a8a8");
    

    Thats it and you will get your result.

    0 讨论(0)
  • 2020-12-13 01:57
    Integer.parseInt(myString.replaceFirst("#", ""), 16) 
    
    0 讨论(0)
  • 2020-12-13 02:03

    The real answer is this simplest and easiest ....

    String white = "#ffffff";
    int whiteInt = Color.parseColor(white);
    
    0 讨论(0)
  • 2020-12-13 02:04

    Get Shared Preferences Color Code in String then Convert to integer and add layout-background color:

        sharedPreferences = getSharedPreferences(mypref, Context.MODE_PRIVATE);
        String sw=sharedPreferences.getString(name, "");
        relativeLayout.setBackgroundColor(Color.parseColor(sw));
    
    0 讨论(0)
提交回复
热议问题