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 - Android Tutorial
From the course: Android Development Essential Training: 2 User Interface Design
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
Position views with LinearLayout4m 44s
-
Manage positions with constraints4m 52s
-
Distribute views with constraint chains4m 37s
-
Display views programmatically5m 20s
-
Get view references with findViewById()2m 34s
-
Get view references with view binding5m 35s
-
Challenge: Get in line50s
-
Solution: Get in line3m 33s
-
-
-
-
-