While adding a TextView
and two ImageView
s to root ConstraintLayout
and setting constraints programmatically, something weird happens while I am setting Tag
s of the views.
Here is how I create aforementioned views and add them to view:
val confirmLocationText = TextView(context!!)
confirmLocationText.text = "Confirm?"
confirmLocationText.textSize = 20f
confirmLocationText.typeface = Typeface.DEFAULT_BOLD
confirmLocationText.measure(0, 0)
val confirmLocationTickImage = ImageView(context!!)
confirmLocationTickImage.setImageResource(R.drawable.tick)
confirmLocationTickImage.scaleType = ImageView.ScaleType.CENTER_CROP
val confirmLocationCancelImage = ImageView(context!!)
confirmLocationCancelImage.setImageResource(R.drawable.cancel)
confirmLocationCancelImage.scaleType = ImageView.ScaleType.CENTER_CROP
arrayOf(confirmLocationCancelImage, confirmLocationTickImage,confirmLocationText).forEach {
it.id = View.generateViewId()
it.tag = confirmInterfaceTag
it.alpha = 0f
viewToBeCreated?.eventMapFragmentRootConstraintLayout?.addView(it)
it.animate().alpha(1f).duration = confirmInterfaceFadeInFadeOutAnimation // Fade-In animation for Confirm Interface
}
And after this piece of code, I set constraints programmatically by using ConstraintSet
.
The problem occurs in the forEach
. It only runs twice but all three of views are added anyway (Although one of the ImageView
s don’t have assigned tag but animation works). When I remove confirmLocationCancelImage
(or confirmLocationTickImage
) from arrayOf
, it again runs twice but this time the removed ImageView
won’t be added.
When I change the code as below, it works but I still don’t understand why it doesn’t work as it above…
val confirmLocationText = TextView(context!!)
confirmLocationText.text = "Confirm?"
confirmLocationText.textSize = 20f
confirmLocationText.typeface = Typeface.DEFAULT_BOLD
confirmLocationText.measure(0, 0)
val confirmLocationTickImage = ImageView(context!!)
confirmLocationTickImage.apply {
setImageResource(R.drawable.tick)
id = View.generateViewId()
tag = confirmInterfaceTag
alpha = 0f
viewToBeCreated?.eventMapFragmentRootConstraintLayout?.addView(this)
animate().alpha(1f).duration = confirmInterfaceFadeInFadeOutAnimation
scaleType = ImageView.ScaleType.CENTER_CROP
}
val confirmLocationCancelImage = ImageView(context!!)
confirmLocationCancelImage.setImageResource(R.drawable.cancel)
confirmLocationCancelImage.scaleType = ImageView.ScaleType.CENTER_CROP
arrayOf(confirmLocationCancelImage,confirmLocationText).forEach {
it.id = View.generateViewId()
it.tag = confirmInterfaceTag
it.alpha = 0f
viewToBeCreated?.eventMapFragmentRootConstraintLayout?.addView(it)
it.animate().alpha(1f).duration = confirmInterfaceFadeInFadeOutAnimation // Fade-In animation for Confirm Interface
}
So I don’t understand:
-
Why
forEach
runs twice? -
When it runs twice, why three of the views are added?
-
And when it runs twice and three of the views are added then why one of them misses tag?
I hope it is not a silly question but seemed weird and annoying to me. Thanks for the help in advance!