Issue
I am trying to create a rectangle drawable inside an image view.
val backgroundRect = GradientDrawable()
backgroundRect.shape = GradientDrawable.RECTANGLE
backgroundRect.setBounds(0,0, 100, 100)
backgroundRect.setStroke(5, Color.parseColor("#585858"))
backgroundRect.setColor(Color.parseColor("#FF9009"))
val image = ImageView(context)
image.layoutParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)
image.setImageDrawable(backgroundRect)
rootView.addView(image)
The problem is if I set the ImageView
layout params as WRAP_CONTENT
then nothing displays but if I set the params as
image.layoutParams = ConstraintLayout.LayoutParams(100, 100)
then the ImageView
is displayed. I tried creating the rectangle drawable using xml and if I set it there the image is still shown.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dummyRectangle"/>
Do I need to always specify the layout params variable or is there another way to set it?
Solution
Just set a size to your GradientDrawable
backgroundRect.setSize(100,100);
Because your view has no size yet. And when you used wrap_content you did not have any content to wrap.
Answered By - Anisuzzaman Babla
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.