Learning Kotlin: The TextView

Kotlin seems interesting and after learning more of its capabilities, I find it is worth using. So this is the first in a series of posts about learning various parts of the Android system with Kotlin. Such as what it looks like using a TextView, if anything changes, if we need some kind of Kotlin bridge, etc. From my understanding there should not be a need for bridges, since it is based on the JVM and was made to be interoperable.

So on to the Text View.

First lets see what it looks like trying to access the text view when it is in a basic layout. So to get started go ahead and create an xml file and have it be the main view for an activity, to be called view setContentView, such as below

class MainActivity : AppCompatActivity() {
	override fun onCreate(savedInstanceState: Bundle?) {

As you can see this is a file already in Kotlin, to get to this point you can create a new project and then use the convert to Kotlin option and configuration options found here

So to get a hold of a text view lets try following we would normally do.

    android:text="Hello World!"

And then in the activity code

    		var textView:TextView = findViewById(R.id.tv_id);

Which the above code for finding the text view will not compile. Even though this seems like valid Kotlin. But remember how in Java we need to cast to the right class? Well we need to still do that but in the Kotlin manner! So lets change the above code to the below code!

    		var textView:TextView = findViewById(R.id.tv_id) as TextView

And now we have a working reference to it. So lets try to set some text on it!

    	textView.setText("Hello world again")

Looks like Java right? Interestingly enough we get a warning from AS letting us know that we should "Use property access syntax". So instead of calling the get or set method lets try the below code.

    	textView.text = "Hello world 123"

Look happy Android Studio and no warning! Little things like this are interesting when switching to using Kotlin.

Also interesting if you highlight the .text portion and go to the definition, found by the key sequence CMD+B on a mac, you will find that it directs you to the decompiled .class file's setText method. So it seems that we are still calling setText but in a different way using Kotlin.

Next I wanted to investigate going Full Kotlin and use the below code to set the text.

    	textView.setText(text = "Hello world 123")

However, that will not work since we are calling a non kotlin function. When calling normal Kotlin functions this would work but sadly not with the Text View. Maybe some sort of bridge could be in order in the future? To learn more about Named params you can go here.

Next lets look at creating a TextView dynamically and adding it to the view. So first lets make the text view give it some text and then add it to a parent layout.


val moreText:TextView = TextView(this)
moreText.text = "Some other text"
addContentView(moreText, RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT))

So as you can see it is a little different then java in the construction of the TextView. No new keyword (duh) but the rest is pretty simple and to be expected.

A side note of something I found while looking at this was a library that creates an Android DSL called Anko which is an official Kotlin library that can be used for creating layouts.

Anyways this was a small investigation and more are to come. Hope you enjoyed!