From the course: Android Development Essential Training: 2 User Interface Design

Unlock the full course today

Join today to access over 22,400 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: 2 User Interface Design

Start my 1-month free trial

Display views programmatically

- [Instructor] Although you'll mostly declare views for your layouts in XML, there may be times where you'll want to create them programmatically during runtime. Let's take a look at how you do that. For this project, we have an activity main layout with only one child view, a linear layout. This linear layout is defined to take up the entire parent, and to stack any nested children vertically. The most critical piece of information that we'll need to add views programmatically is the ID of the layout. And we see that here on line number 10. It's my_layout. To get started, we need to open up the main activity class. We can navigate there by clicking on command and then highlighting over that item. Inside of our main activity class, we're going to create a private function that we'll use to dynamically create views. We can add that underneath our call to the onCreate. We'll just add two lines of space , and we'll create a new…

Contents