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
Based on CQM's answer and on ovokerie-ogbeta's answer to another question I've come up with this solution:
if (colorAsString.length() == 4) { // #XXX
colorAsString = colorAsString.replaceAll("#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])", "#$1$1$2$2$3$3");
}
int color = Color.parseColor(colorAsString);
Try this, create drawable in your resource...
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@color/white"/>
<size android:height="20dp"
android:width="20dp"/>
</shape>
then use...
Drawable mDrawable = getActivity().getResources().getDrawable(R.drawable.bg_rectangle_multicolor);
mDrawable.setColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_IN);
mView1.setBackground(mDrawable);
with color... "#FFFFFF"
if the color is transparent use... setAlpha
mView1.setAlpha(x);
with x float 0-1 Ej (0.9f)
Good Luck
The real answer is to use:
Color.parseColor(myPassedColor)
in Android, myPassedColor
being the hex
value like #000
or #000000
or #00000000
.
However, this function does not support shorthand hex values such as #000
.