android - Programatically make an imageView visible/invisible using a variable -


i have kotlin function, works expected when imageview explicitly called. when variable 'cellname' used code not compile.

my objective here programatically generate imageview name , set/unset visibility. please tell me going wrong.

tia

   fun game(view: view) {         var col = 1         var row = 0         var cellname = ""          object : countdowntimer(10000, 1000){             override fun onfinish() {                 toast.maketext(applicationcontext, "time's up", toast.length_long).show()                 textview9.text = "time: 0"             }              override fun ontick(p0: long) {                 textview9.text = "time: "+ p0 /1000                 // hide current position                 cellname = "imageview"+row+col                 imageview01.setvisibility(view.invisible)                 // next random position                 // show new position             }         }.start()     } 

so: imageview01.setvisibility(view.invisible) --> works cellname.setvisibility(view.invisible) -- not compile

you can generate view id (aka r.id constant) using resources.getidentifier().

a sample code (assuming you're running inside activity) might be:

int row = 0; int col = 1; string name = "imageview" + row + col;  int id = getresources().getidentifier(name, "id", getpackagename());  imageview imageview = findviewbyid(id); imageview.setvisibility(view.invisible); 

sorry in java, not kotlin. can adapt it.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -