Issue
i remake my old java code on kotlin now and i have one problem, i need a kotlin analog of this code:
Drawable[] layers = new Drawable[]{
colorDrawable,
border
};
LayerDrawable drawable = new LayerDrawable(layers);
In kotlin i have now:
val border = GradientDrawable()
border.shape = GradientDrawable.RECTANGLE
border.setStroke(CommonsUtils.dpToPx(holder.itemView, 1), Color.parseColor("#DEDEDE"))
border.cornerRadius = CommonsUtils.dpToPx(holder.itemView, 2).toFloat()
val colorDrawable = try {
Color.parseColor(color.hex)
} catch (e: Exception) {
e.printStackTrace()
ColorDrawable(Color.parseColor("#FFFFFF"))
}
val layers = arrayOf(colorDrawable, border)
val drawable = LayerDrawable(layers as Array<out Drawable>)
But arrayOf returns Array of "Any" instead Array of "out Drawable" and i have class cast exception Object[] to Drawable[].
Solution
Change to this:
val colorDrawable = try {
ColorDrawable(Color.parseColor(color.hex))
} catch (e: Exception) {
e.printStackTrace()
ColorDrawable(Color.parseColor("#FFFFFF"))
}
Color.parseColor(color.hex)
returns int
not Drawable
Answered By - user8959091
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.