Skip to content

Instantly share code, notes, and snippets.

@omidMirrajei
Created December 5, 2019 01:08
Show Gist options
  • Save omidMirrajei/5a60305b097fcf1171fec993de4374bb to your computer and use it in GitHub Desktop.
Save omidMirrajei/5a60305b097fcf1171fec993de4374bb to your computer and use it in GitHub Desktop.
Initialize view
//Sample 1
private var button: Button = findViewById(R.id.button)
//Sample 2
private var button: Button? = null
private fun init(){ button = findViewById(R.id.button) }
//Sample 3 (The lateinit keyword stands for late initialization. lateinit modifier is allowed only on mutable properties)
private lateinit var button:Button
private fun init(){ button = findViewById(R.id.button) }
//Sample 4
private val button:Button by lazy {
findViewById(R.id.button) as Button
// or
findViewById<Button>(R.id.button)
}
// we need use to init button
button.setOnClickListener{ }
//Sample 5 (use view id directly)
button.setOnClickListener {textView.text = "Hello Kotlin"}
//Sample 6
var textView: TextView? = null
textView = findViewById(R.id.textView)
textView?.text = "Sample Text"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment