Estimated time: 30 minutes
Welcome to the lab on building your first user interface in Android Studio. You will build a login screen in this lab. The final result would look something like this:
After completing this lab, you will be able to:
This is an instructional lab that does not require a Skills Network lab environment. You will follow this lab on your local machine using Android Studio.
User experience (UX) design for mobile applications is crucial because it directly impacts user satisfaction and engagement. Good UX design ensures that the app is intuitive, efficient, and enjoyable to use. Key elements include ease of navigation, aesthetic appeal, and responsiveness.
Creating a good UX involves careful planning and consideration of various elements such as layouts, widgets, themes, and styles. Android provides a range of tools and components to help you build a great user interface.
Themes and styles in Android help to maintain a consistent look and feel across the app. A style is a collection of attributes that specify the appearance for a single View. A theme is a style applied to an entire Activity or application, not just an individual View.
Extensible Markup Language (XML) is used to design the UI layout in Android. Each XML file represents a screen or a part of a screen. XML allows developers to separate the design from the logic, making the code easier to manage and maintain. You will not touch the XML file directly in this lab, but it is important to understand their role in Android application development.
You can skip this step if you already have a project from the previous lab.
Phone and Tablet
and
Empty Views Activity
as the project type.Finish
to create the project.Open the activity_main.xml
file in the
res/layout
directory. This is the main view of the project
that opens when the app is launched. Switch to the Design
view by selecting the "Design" on top of the window (indicated by number
3 in the screenshot).
Select and delete the existing text box that says "hello world"
in the middle of the screen. You can delete by simply selecting Delete
or backspace on the keyboard. Alternatively, right click and select
Delete
.
You should be left with an empty parent view.
In the Component Tree
on the left, right click on
the existing ConstraintLayout
and select
Convert view
.
You are shown different kinds of layouts. Pick
LinearLayout
on the screen.
Uh oh! It might look like you broke something when you picked
LinearLayout
. The problem tab shows something in red. The
issue is that our layout does not have an explicit direction. A linear
layout needs to have a vertical
or a
horizontal
direction stated explicitly. The error should
tell you the same. You can right click and select
Show Quick-Fixes
to see all the options to fix the
issue.
Fix the issue by picking Set orientation="vertical"
from the quick fix.
You should see the issue is fixed in the Problems
tab.
You can confirm the orientation
is vertical
in the attributes of the widget. The Attributes
view has a
list of everything you can set for this widget.
While we are on the attributes, another useful feature to know is
that you can search attributes for a field by using the search bar in
the Attributes
view.
Add a TextView:
Drag a TextView
from the Palette
to the
LinearLayout
.
In the Attributes
panel, set the id
to
login_title
.
Set the text
to Login Application
.
Set the textSize
to 24sp
.
Set the gravity
to center
.
Set layout_margin
to 16dp
.
At the end of this step, you should have a TextView
that
serves as the title text of the first screen of your application!
Drag an E-mail
from the Palette
to the
LinearLayout
, below the TextView
.
In the Attributes
panel, set the id
to
login_username
.
Set the hint
to Username
.
Confirm that the inputType
is
textEmailAddress
.
Set layout_margin
to 16dp
.
Set min-height
to 48dp
.
Drag another Password
from the Palette
to the LinearLayout
, below the
Username EditText
.
In the Attributes
panel, set the id
to
login_password
.
Set the hint
to Password
.
Confirm the inputType
is set to
textPassword
.
Set layout_margin
to 16dp
.
Set min-height
to 48dp
.
You might notice some problems (in red) in the Problem
view. If there are no issues, move to the next step.
You can right click on the issue and look for a quick fix:
In this case, clicking on the first solution will set the minHeight
to 48dp and remove the error.
Repeat the above steps for the password widget.
Drag a Button
from the Palette
to the
LinearLayout
, below the Password EditText.
In the Attributes
panel, set the id
to
login_button
.
Set the text
to Login
.
Set layout_margin
to 16dp
.
Set min-height
to 48dp
.
Drag a TextView
from the Palette
to the
LinearLayout
, below the Login
button.
In the Attributes
panel, set the id
to
login_register_link
.
Set the text
to
Register for a new account
.
Set the textColor
to a color resource or a hex value
to make it look like a link (e.g., #0000FF
for blue).
Set the textSize
to 16sp
.
Set layout_marginLeft
to 16dp
and
layout_marginTop
to 5dp
. Set
layout_width
to wrap_content
.
Ensure your AVD is set up from the previous lab.
Click the Run
button in Android Studio.
Select your AVD and wait for the application to launch.
Verify that the login screen appears as designed.
You have now successfully created the UI for a login screen using
LinearLayout
. This foundational skill will be crucial as
you continue to develop more complex UIs in your Android
applications.
UL