Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare variables in Android (Kotlin) using Google code style?

I started to build the app in Kotlin and I want to know how to correctly initialize variables. For example in Java it was like:

 private TextView mSomeTextView;

And then we call findViewById in some methods. But in Kotlin I can't just write something like that, I need to:

private val textView: TextView = findViewById(R.id.text)

I write it under onCreate as I used to. Question: is it right place for it? If no -- where and how should I do it?

like image 549
VolodymyrH Avatar asked Dec 08 '22 16:12

VolodymyrH


1 Answers

You should use lateinit:

private lateinit var textView: TextView

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    textView = findViewById(R.id.text)
}
like image 196
Ruckus T-Boom Avatar answered May 23 '23 19:05

Ruckus T-Boom