I need to display a different image according to a condition. The problem is that the previous image remains visible under the next image?
I have nowhere found using the correct command to delete the previous image before displaying the next one.
private fun showDecision(warning: String) {
var imgResId = 0
var resId = 0
// accessing our custom image which we have in drawable folder
when (warning) {
"wrong" -> {
imgResId = R.drawable.wrong
resId = imgResId
}
"notGood" -> {
imgResId = R.drawable.notGood
resId = imgResId
}
"bad" -> {
imgResId = R.drawable.bad
resId = imgResId
}
}
// image de l'arbitre
val decisionView = ImageView(this)
// setting height and width of imageview
decisionView.layoutParams = RelativeLayout.LayoutParams(392, 700)
decisionView.x = hut.x - 80 // setting margin from left
decisionView.y = aDecision.y // setting margin from top
// accessing our relative layout from activity_main.xml
decisionLayout = findViewById<RelativeLayout>(R.id.decision_layout)
decisionView.setImageResource(resId)
// Add ImageView to linearLayout
decisionLayout?.addView(decisionView) // adding image to the layout
decisionLayout?.visibility = View.VISIBLE
decisionShown = true
}
setBackgroundResource(0) does nothing.
Before that, in my onClick(view: View?) I use GONE as below
if (decisionShown) {
decision_layout.visibility = View.GONE
decisionShown = false
}
What did I not do correctly? Thank you
You can clear imageview content by calling setImageDrawable(null);
such usage is documented in the official docs: ImageView#setImageDrawable .