androidandroid-layoutkotlinlayoutparams

First ImageView dissapeard after the second ImageView gets added to the RelativeLayout


After adding a second ImageView to the RelativeLayout, the first and the second ImageView do not show. How can I fix it?

val hideImage = ImageView(ctx)
hideImage.setImageResource(R.drawable.hiding_box)
hideImage.id = hiderImageIds[0]
params.leftMargin = 0
params.topMargin = 0
hiderLayout.addView(hideImage, params)

val hideImage2 = ImageView(ctx)
hideImage2.setImageResource(R.drawable.hiding_box)
hideImage2.id = hiderImageIds[1]
params.leftMargin = hiderImageWidth
params.topMargin = 0
params.addRule(RelativeLayout.RIGHT_OF, hiderImageIds[0])
hiderLayout.addView(hideImage2, params)

Solution

  • You use the same params object for both the ImageViews. So, I suspect, changing it for the 2nd image would change it for the 1st one. Use different LayoutParams.