I would like the equivalent code for the line below in Kotlin:
TextView tv = view.findViewbyId(R.id.textView);
Any help?
I would like the equivalent code for the line below in Kotlin:
TextView tv = view.findViewbyId(R.id.textView);
Any help?
very simply, just do following action
val valueName = view!!.findViewById<ObjectName>(R.id.ObjectId)
also with this mark !!
kotlin will be checked null state
finally see following example
val btn = view!!.findViewById<Button>(R.id.btn)
Why don't you simply use Kotlin Android Binding? Which is an Extension from Kotlin helps to bind the view without any "bindView" or findViewById code to interfere business logic.
Once you have dig into it, you would definitely find it great and less code written afterwards.
Worth to have a look. https://kotlinlang.org/docs/tutorials/android-plugin.html
But you can still use the original one with kotlin
val lblLabel = findViewById(R.id.text) as TextView
Initialization of view in fragment :
val manageAddress=view.findViewById<TextView>(R.id.textView)
that's it
val tv: TextView = findViewById(R.id.textView) as TextView
if you want to declare textview globally, use lateinit var tv: TextView
like this you can do in kotlin
val lv = findViewById(R.id.textView) as TextView
You can use:
val myLabel = findViewById(R.id.text) as TextView
However you can use the Kotlin Android Extensions plugin.
Just add to your build.gradle
apply plugin: 'kotlin-android-extensions'
Then you can avoid the use of the findViewById
method just using for example:
textView.text = "My text"
You can access the view without finding it or using third
party libraries just using the id
assigned in the xml file.
If you use Kotlin Android Extensions (apply plugin: 'kotlin-android-extensions'
, see https://antonioleiva.com/kotlin-android-extensions/), you can write textView
and IDE will offer several variants of that view in different layouts. After you choose a right one, you can write something like:
textView.text = "This is a text"
In imports you will see something like import kotlinx.android.synthetic.main.fragment_dialog.*