android - How can I fix GradientDrawable cannot be cast to ColorDrawable issue? -


I have a code snippet to show you below and I'm trying to change the background (v) of the scene Color code will be found from a code TextView (dragged) and change the background of view (v) using this code. But as mentioned above, I get an error, how can I do this? Where's the problem? Thank you.

ColorDrawable CD = (ColorDrawable) dragged.getBackground (); Int colorCode = cd.getColor (); V.setBackgroundColor (colorCode);

If you want to specify only the background of one view to another, and not only Color You can use the following code

  Drawable drawable = dragged. Background (); If (Build.VERSION.SDK_INT & lt; Build.VERSION_CODES.JELLY_BEAN) {v.setBackgroundDrawable; } And {v.setBackground (drawable); }  

However, if you just want to get the color, you will have to first identify which type of drawable is being allocated to look at. Then use the example to deal with how to get the background color for the related drawbill.


Comments