Exercise 03 - Build UI with Layout Editor 2
Project name : E03 Build UI With Layout Editor 2
Introduction
The purpose of this tutorial is to learn to use Android Studio’s Layout Editor to create a fluid application UI. You will add a view layouts with a common components to display an employees data in UI. Designed application will run smoothly in different devices and screen sizes.
The Layout Editor enables Designer quickly build layouts just dragging UI elements to visual Design Editor, instead of writing layout XML by hand. The Design Editor can preview your layout on different Android devices and versions. Designer can dynamically resize the layout to be sure it works well on different screen sizes.
Create UI
Application UI will contain common TextView
and ImageView
components to display employee’s information.
- Create a new project and name it to E03 Build UI With Layout Editor 2
- Remember save your project in your android-exercises folder
- Download Employee images, unzip and copy images to your project drawable folder. You can just drag and drop images to that folder.
- Use Horizontal and Vertical LinearLayouts to organize components (look a below image), look component names from the Component Tree. You can use your own naming rules in used components, but remember use same values later in the code.
Click image to to see it bigger!
You can use for example following data in your strings.xml. Note how you can use placeholders in your string resources (line 7). Now three different string variables will be used in employee_info_text
string (later in UI).
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Modify strings.xml
Add sample employee's data
Now you have five employee images and you will hard code an employee details.
Create a three different arrays, which contains details of the employee: firstname
, lastname
and jobtitle
. Add a string based data to variables: first, last names and job title. Define these arrays inside your MainActivity
class.
1 2 3 |
|
Create showEmployeeData
function. This function will show employee’s data in the UI. Layout TextView‘s will be updated with correct employee’s first and last names, job titles and text from the string resources. Remember add a new basic_text
key to your string resources with some text value. Image will be loaded from the drawable resources with setImageRecourse
function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Note
You will need to import needed classes.
Call above showEmployeeData
function with parameter 0 from onCreate
after setContentView
is called. Now, the loaded employees data, will be shown in the application UI.
1 2 3 4 5 6 7 |
|
Test
Build and run your application and it should display the first employee data in UI.
Change selected employee in UI
Application should show a selected employee’s information, when a number textview’s are clicked. In other words, now textview’s are working like a buttons.
Modify all the text XML elements and add onClick
-attribute event handling to call the same numberClicked
-function from all the text 1-5.
1 2 3 4 5 6 7 8 |
|
Open MainActivity
Kotlin file and add a numberClicked
-function. This function will first get the text from the clicked TextView and convert it to integer. After that, earlier made showEmployeesData
function should be called with that integer value.
Click to see a solution - first try to code it by yourself
1 2 3 4 5 6 7 8 9 |
|
Now same showEmployeesData
function can be called from all the text numbers clicked.
Test
Build and run your application and it should display the first employee data in UI and other employees by the selection made by user.
Remember test your application in landscape mode. In conclusion, your app is now working both portrait and landscape mode.
Test and push
Test your application in emulator, take screenshots and commit/push your project and screenshots back to JAMKIT/GitLab. Remember move your exercise/issue ticket from Doing to In Review in Issues Board and write your learning comments to issue comments.