VolodymyrH
VolodymyrH

Reputation: 2019

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?

Upvotes: 4

Views: 4719

Answers (1)

Ruckus T-Boom
Ruckus T-Boom

Reputation: 4786

You should use lateinit:

private lateinit var textView: TextView

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    textView = findViewById(R.id.text)
}

Upvotes: 15

Related Questions