Issue
I'm trying to set the background tint of a button programmatically, not to a color resource as done here, but instead to a hex value. At the moment, I have converted a hex value into a ColorDrawable, but do not know how to use this to set the background tint with the .setBackgroundTintList()
method of my button. Note that this is being done in a Fragment and the context is stored in a global variable called mContext
.
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#FFFFFF"));
Solution
on API +21
btn.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#buttonColor")));
or Compat
Drawable drawable = new ColorDrawable(Color.parseColor("color"));
// Wrap the drawable so that future tinting calls work
// on pre-v21 devices. Always use the returned drawable.
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable,Color.parseColor("colorTint"));
//or tint list
//DrawableCompat.setTintList(drawable,ColorStateList.valueOf(Color.parseColor("#ffffff")));
btn.setBackground(drawable); //apply drwable with tint to the ctn
Answered By - Bishoy Kamel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.