Android Studio Lab Note Bareeda
Android Studio Lab Note Bareeda
For this tutorial, you will design and implement a membership registration form. Whether
you’re in charge of recruiting members for an app, an event, or a club, a simple and well-
designed member registration form will speed up the registration process, hence encouraging
more users to sign up.
App Overview
Layouts in Android define the structure of the user interface (UI) of your application. The UI
consists of elements which are built using a hierarchy of views and view groups. A view
consists of UI components such as buttons, text boxes, checkboxes, radio buttons, images,
etc. See the diagram below representing a standard application layout.
Before you begin the design process, you need to give some thought to what kind of data you
want to collect from the user. Consider the types of data you want to collect and choose the
appropriate types of control.
Our membership form will be for a gym and will collect the following information:
full name
gender
current weight
height
goal weight
age
phone
address
Getting Started
Open Android Studio and create a new project with an empty activity. Wait for Android Studio
to finish creating your project, and then open app > res > layout > activity_main.xml. This
file defines the layout for the user interface (UI).
A UI in Android is defined in XML files. The easiest way to build a UI in Android Studio is with
the Android Studio Layout Editor. The Layout Editor writes the XML for you as you drag and
drop views to build your layout.
In this project, we will use the ConstraintLayout . A ConstraintLayout is a layout that provides a
flexible way for creating views.
This TextView control will display the form's description and purpose to the user. This control
displays a string resource called @string/member_title , which must be defined within
the /res/values/strings.xml string resource file.
1 <resources>
2 <string name="app_name">LEXO</string>
3 <string name="title">Membership Form</string>
5 </resources>
1 <TextView
2 android:id="@+id/title"
3 android:layout_width="wrap_content"
4 android:layout_height="wrap_content"
5 android:layout_marginTop="28dp"
6 android:text="@string/title"
7 android:textColor="#D500F9"
8 android:textSize="28sp"
9 app:layout_constraintEnd_toEndOf="parent"
10 app:layout_constraintHorizontal_bias="0.436"
11 app:layout_constraintStart_toStartOf="parent"
12 app:layout_constraintTop_toTopOf="parent" />
Notice the id attribute on the text view; it is recommended that each control element is
assigned an id so that it's easy to reference from the Java files.
1 <EditText
2 android:id="@+id/names"
3 android:layout_width="wrap_content"
4 android:layout_height="wrap_content"
5 android:layout_marginTop="96dp"
6 android:ems="10"
7 android:hint="@string/name"
8 app:layout_constraintEnd_toEndOf="parent"
9 app:layout_constraintHorizontal_bias="0.184"
10 app:layout_constraintStart_toStartOf="parent"
11 app:layout_constraintTop_toTopOf="parent" />
Advertisement
1 <TextView
2 android:id="@+id/gender"
3 android:layout_width="83dp"
4 android:layout_height="32dp"
5 android:layout_marginTop="172dp"
6 android:text="Gender"
7 android:textSize="18sp"
8 app:layout_constraintEnd_toEndOf="parent"
9 app:layout_constraintHorizontal_bias="0.118"
10 app:layout_constraintStart_toStartOf="parent"
11 app:layout_constraintTop_toTopOf="parent" />
Radio buttons allow the user to select one option from a set. They should be grouped together
to ensure only one radio button is selected at any given time; this is done by grouping them
inside a RadioGroup . Here is the XML for gender selection.
1 <RadioGroup
2 android:id="@+id/radioGroup"
3 android:layout_width="205dp"
4 android:layout_height="32dp"
5 android:layout_marginTop="172dp"
6 android:orientation="horizontal"
7 app:layout_constraintEnd_toEndOf="parent"
8 app:layout_constraintHorizontal_bias="0.834"
9 app:layout_constraintStart_toStartOf="parent"
10 app:layout_constraintTop_toTopOf="parent">
11
12
13 <RadioButton
14 android:id="@+id/radioButton"
15 android:layout_width="45dp"
16 android:layout_height="33dp"
17 android:text="M"
18 app:layout_constraintEnd_toEndOf="parent"
19 app:layout_constraintStart_toStartOf="parent"
20 app:layout_constraintTop_toTopOf="parent"
21 android:onClick="radioButtonhandler"/>
22
23 <RadioButton
24 android:id="@+id/female"
25 android:layout_width="45dp"
26 android:layout_height="33dp"
27 android:text="F"
28 app:layout_constraintEnd_toEndOf="parent"
29 app:layout_constraintHorizontal_bias="0.655"
30 app:layout_constraintStart_toStartOf="parent"
31 app:layout_constraintTop_toTopOf="parent"
32 android:onClick="radioButtonhandler"/>
33
34 <RadioButton
35 android:id="@+id/other"
36 android:layout_width="96dp"
37 android:layout_height="33dp"
38 android:text="Other"
39 app:layout_constraintEnd_toEndOf="parent"
40 app:layout_constraintHorizontal_bias="0.949"
41 app:layout_constraintStart_toStartOf="parent"
42 app:layout_constraintTop_toTopOf="parent"
43 android:onClick="radioButtonhandler"/>
44 </RadioGroup>
1 <EditText
2 android:id="@+id/current_weight"
3 android:layout_width="164dp"
4 android:layout_height="46dp"
5 android:layout_marginTop="260dp"
6 android:ems="10"
7 android:hint="@string/currrent_weight"
8 android:inputType="number"
9 app:layout_constraintEnd_toEndOf="parent"
10 app:layout_constraintHorizontal_bias="0.151"
11 app:layout_constraintStart_toStartOf="parent"
12 app:layout_constraintTop_toTopOf="parent" />
13
14 <EditText
15 android:id="@+id/height"
16 android:layout_width="157dp"
17 android:layout_height="44dp"
18 android:layout_marginTop="260dp"
19 android:ems="10"
20 android:hint="@string/height"
21 android:inputType="number"
22 app:layout_constraintEnd_toEndOf="parent"
23 app:layout_constraintHorizontal_bias="0.919"
24 app:layout_constraintStart_toStartOf="parent"
25 app:layout_constraintTop_toTopOf="parent" />
1 <EditText
2 android:id="@+id/goal_weight"
3 android:layout_width="175dp"
4 android:layout_height="52dp"
5 android:layout_marginTop="344dp"
6 android:ems="10"
7 android:hint="@string/goal_weight"
8 android:inputType="number"
9 app:layout_constraintEnd_toEndOf="parent"
10 app:layout_constraintHorizontal_bias="0.156"
11 app:layout_constraintStart_toStartOf="parent"
12 app:layout_constraintTop_toTopOf="parent" />
13
14 <EditText
15 android:id="@+id/age"
16 android:layout_width="140dp"
17 android:layout_height="48dp"
18 android:layout_marginTop="348dp"
19 android:ems="10"
20 android:hint="Age"
21 android:inputType="number"
22 app:layout_constraintEnd_toEndOf="parent"
23 app:layout_constraintHorizontal_bias="0.863"
24 app:layout_constraintStart_toStartOf="parent"
25 app:layout_constraintTop_toTopOf="parent" />
Advertisement
1 <EditText
2 android:id="@+id/Phone"
3 android:layout_width="wrap_content"
4 android:layout_height="wrap_content"
5 android:layout_marginTop="428dp"
6 android:ems="10"
7 android:hint="@string/phone"
8 android:inputType="phone"
9 app:layout_constraintEnd_toEndOf="parent"
10 app:layout_constraintHorizontal_bias="0.194"
11 app:layout_constraintStart_toStartOf="parent"
12 app:layout_constraintTop_toTopOf="parent" />
1 <EditText
2 android:id="@+id/address"
3 android:layout_width="wrap_content"
4 android:layout_height="wrap_content"
5 android:layout_marginTop="500dp"
6 android:ems="10"
7 android:hint="@string/address"
8 android:inputType="text"
9 android:maxLines="2"
10 app:layout_constraintEnd_toEndOf="parent"
11 app:layout_constraintHorizontal_bias="0.194"
12 app:layout_constraintStart_toStartOf="parent"
13 app:layout_constraintTop_toTopOf="parent" />
1 <CheckBox
2 android:id="@+id/conditions"
3 android:layout_width="wrap_content"
4 android:layout_height="wrap_content"
5 android:layout_marginTop="580dp"
6 android:backgroundTint="#D500F9"
7 android:buttonTint="#D500F9"
8 android:text="@string/conditions"
9 app:layout_constraintEnd_toEndOf="parent"
10 app:layout_constraintHorizontal_bias="0.964"
11 app:layout_constraintStart_toStartOf="parent"
12 app:layout_constraintTop_toTopOf="parent" />
1 <Button
2 android:id="@+id/button"
3 android:layout_width="wrap_content"
4 android:layout_height="wrap_content"
5 android:layout_marginTop="684dp"
6 android:backgroundTint="#D500F9"
7 android:onClick="submitbuttonHandler"
8 android:text="SUBMIT"
9 app:layout_constraintEnd_toEndOf="parent"
10 app:layout_constraintHorizontal_bias="0.425"
11 app:layout_constraintStart_toStartOf="parent"
12 app:layout_constraintTop_toTopOf="parent" />
5 @Override
6 protected void onCreate(Bundle savedInstanceState) {
7 super.onCreate(savedInstanceState);
8 setContentView(R.layout.activity_main);
9 EditText nameEditText = (EditText) findViewById(R.id.names);
10 String fullName = nameEditText.getText().toString();
11 }
Add the android:onClick attribute to each of the radio buttons in the XML file.
1 <RadioGroup
2 android:id="@+id/radioGroup"
3 android:layout_width="205dp"
4 android:layout_height="32dp"
5 android:layout_marginTop="172dp"
6 android:orientation="horizontal"
7 app:layout_constraintEnd_toEndOf="parent"
8 app:layout_constraintHorizontal_bias="0.834"
9 app:layout_constraintStart_toStartOf="parent"
10 app:layout_constraintTop_toTopOf="parent">
11
12
13 <RadioButton
14 android:id="@+id/radioButton"
15 android:layout_width="45dp"
16 android:layout_height="33dp"
17 android:text="M"
18 app:layout_constraintEnd_toEndOf="parent"
19 app:layout_constraintStart_toStartOf="parent"
20 app:layout_constraintTop_toTopOf="parent"
21 android:onClick="radioButtonhandler"/>
22
23 <RadioButton
24 android:id="@+id/female"
25 android:layout_width="45dp"
26 android:layout_height="33dp"
27 android:text="F"
28 app:layout_constraintEnd_toEndOf="parent"
29 app:layout_constraintHorizontal_bias="0.655"
30 app:layout_constraintStart_toStartOf="parent"
31 app:layout_constraintTop_toTopOf="parent"
32 android:onClick="radioButtonhandler"/>
33
34 <RadioButton
35 android:id="@+id/other"
36 android:layout_width="96dp"
37 android:layout_height="33dp"
38 android:text="Other"
39 app:layout_constraintEnd_toEndOf="parent"
40 app:layout_constraintHorizontal_bias="0.949"
41 app:layout_constraintStart_toStartOf="parent"
42 app:layout_constraintTop_toTopOf="parent"
43 android:onClick="radioButtonhandler" />
44 </RadioGroup>
Next, use the findByview() method to return an instance of the rest of the EditText elements
on the Oncreate() method. Then use the getText() method to get the values from the input
fields.
5 @Override
6 protected void onCreate(Bundle savedInstanceState) {
7 super.onCreate(savedInstanceState);
8 setContentView(R.layout.activity_main);
9 EditText nameEditText = (EditText) findViewById(R.id.names);
10 String fullName = nameEditText.getText().toString();
11
14
17
20
23
26
We will use the isChecked() method to check the current state of the checkbox. The state is a
boolean value, either true or false. If a checkbox is checked, it returns true; otherwise, it
returns false.
4 }
Conclusion
In this tutorial, you learned how to use various input controls to design a membership form
within an Android application. The EditText control is versatile and powerful, allowing for
many different types of text and input forms. Radio buttons allow the user to choose only one
of the given choices at a time. The CheckBox controls help limit the user’s input to a specific
set of responses. The Button control is a simple way to generate an event to process the form
input.
There are many other controls worth exploring for use within forms. There's a lot more we
could cover regarding good form design, how form controls fit into the Activity lifecycle, and
how input methods and such factor into things, but for now, we've focused on gaining a good
handle on the basics of form controls and how to use them.