From the course: Android Development Essential Training: The User Interface with Kotlin

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Display views programmatically

Display views programmatically - Android Tutorial

From the course: Android Development Essential Training: The User Interface with Kotlin

Start my 1-month free trial

Display views programmatically

- [Narrator] Each view group is a container that can have as many child view objects as are needed. Most of the time you'll probably declare your views in XML layouts, but there will also be times when you might want to add views at runtime using Java or Kotlin code. This version of my starting app has a linear layout that's nested within the constraint layout. It has an ID of linearLayout that I'll be able to address dynamically in my Kotlin code. I'll open my MainActivity class. I'll press the shift key twice for search everywhere and I'll start typing MainActivity and then I'll select it to open it. I'm going to create a private function called addTextView. It will receive one argument that I'll name label and I'll set the type as string. Within the function, I'll create a local variable named view and I will instantiate it using the TextView's constructor. I'll type the name of the class then press Enter or Return and that ensures that I have the required import at the top of the…

Contents