Slowly learning Kotlin. Just generating a random number from a roll. If roll = 9 I want to make the button and seekbar invisible.
I'm using the toggleVisibility function to accomplish this, but the Kotlin compiler sees isVisible as a unresolved reference
package com.example.randomizer
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.SeekBar
import android.widget.TextView
import android.widget.VideoView
import java.util.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollButton = findViewById<Button>(R.id.rollButton)
val resultsTextView = findViewById<TextView>(R.id.resultsTextView)
val seekBar = findViewById<SeekBar>(R.id.seekBar)
val winText = "9 You Win !"
rollButton.setOnClickListener {
val rand = Random().nextInt(seekBar.progress)
resultsTextView.text = rand.toString()
if (rand == 9) {
resultsTextView.text = winText
seekBar.toggleVisibility()
rollButton.toggleVisibility()
}
}
}
fun View.toggleVisibility() {
if (this.isVisible()) {
this.visibility = View.INVISIBLE
} else {
this.visibility = View.VISIBLE
}
}
}
Compiler error:
unresolved reference isVisible
Did you define isVisible
for a View by yourself?
View class has no method called isVisible()
.
As @user2340612 said, it can be defined as:
fun View.isVisible(): Boolean {
return this.visibility == View.VISIBLE
}