Search code examples
androidcolordrawable

setForeground programmatically to hex colour


I have learned how to set an ImageView's foreground colour programmatically here, but this sets the colour to a value in the R.color resource:

int color = R.color.black_trans_60;
frm.setForeground(new ColorDrawable(ContextCompat.getColor(mContext, color)));

However, I would like to directly set a String hex value to the colour -- is there a way to accomplish this?


Solution

  • Use ColorDrawable with Color.parseColor("#FFFFFF") like so:

    ColorDrawable cd = new ColorDrawable(Color.parseColor("#FFFFFF"));
            holder.image.setForeground(cd);