Home > OS >  Tag null in kotlin android
Tag null in kotlin android

Time:06-07

i'm trying change images using tag but this returns null and i don't know why. This is my code:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val iv = findViewById<ImageView>(R.id.iv)
    iv.setImageResource(R.drawable.morado)


    iv.setOnClickListener{
        val path = R.drawable.verde
        val s = iv.getTag(path.toString().toInt())
        Log.d("TAG", "onCreate: $s") //here $s is null but the file 'verde' exist
        //iv.setImageResource(a)
    }
}

Why $s is null? can anyone explain me that?

CodePudding user response:

The solution to do that was:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val iv = findViewById<ImageView>(R.id.iv)
    iv.setImageResource(R.drawable.morado)

    iv.setOnClickListener{
        iv.tag = R.drawable.verde
        Log.d("TAG", "onCreate: ${iv.tag.toString().toInt()}")
        iv.setImageResource(iv.tag.toString().toInt())
    }
}
  • Related