Professional Documents
Culture Documents
MAPD Manual (1 To 3) Complete
MAPD Manual (1 To 3) Complete
Environment
Upon successful completion of this lab the students will be able to:
Install Android Studio Bundle.
Get familiar with Android Studio User Interface.
1.1 Tools
Android Studio (Version: 2.3 or higher)
1.2 Background
1.2.1 Android
Android is an open source and Linux-based operating system developed by Google for
devices such as: Smartphones, Tablets, Android TV, Android Wear Devices, and Android
Auto.
Android Studio is the official Integrated Development Environment (IDE) for Android app
development, based on IntelliJ IDEA.
Android Studio offers features as: Powerful code editor, Developer tools, Gradle-based build
system, Fast and rich android emulator, Unified environment to develop apps for all android
devices, Extensive testing tools and frameworks, C++ and NDK support and many more.
When a new project is created or existing one is opened, the Main Window appears. The
Main Window is made up of several logical areas as:
1. Title bar: Displays the title of android project currently opened and the resource file
currently you are editing.
2. Menu bar: Horizontal bar at the top, containing most of the essential actions of
android studio. It organizes the actions in the form of drop-down lists.
3. Toolbar: Contains shortcut to frequently performed actions.
4. Navigation bar: Helps you navigate through your project and open files for editing.
5. Editor window: It is where you create and modify code. Depending on the current file
type, the editor can change. For example, when viewing a layout file, the editor
displays the Layout Editor.
6. Tool window bar: Runs around the outside of the IDE window and contains the
buttons that allow you to expand or collapse individual tool windows.
7. Tool windows: Instead of using preset perspectives, Android Studio follows your
context and automatically brings up relevant tool windows as you work.
8. Status bar: Displays the status of your project and the IDE itself, as well as any
warnings or messages.
9. Design editor: Displays your layout in a combination of the Design and Blueprint
views. The editor can preview your layout in different Android devices and versions.
You can dynamically resize the layout to be sure it works well on different screen sizes.
10. Palette: Provides a list of widgets and layouts that you can drag into your layout in
the editor.
11. Component tree: Shows the view hierarchy for your layout.
12. Properties: Provides property controls for the currently selected view.
1.3 Procedure
1.3.1 Downloading Android Studio Bundle
Android Studio Bundle contains all the required components for developing Android Apps.
Download the Android Studio Bundle from the following link:
https://developer.android.com/studio
Step 03: Choose installation location for Android Studio and Android SDK.
Step 06: Wait for the setup to download and install missing components.
Step 08: Write the Application name for new Android Studio Project.
Step 09: Choose Target Android Device (say, Phone and Tablet).
Step 10: Choose Minimum SDK (say, IceCreamSandwich) and press Next.
Step 03: Choose hardware by selecting device category and device definition (say,
Nexus5X from Phone).
EXERCISE
Exercise 1.1: Download and install Android Studio 2.3 on your system.
Exercise 1.2: Setup Android Smartphone Emulator using AVD manager with following
configurations:
Device Definition: Nexus 4
System image: Nougat AVD Name: My
Nexus 4
Exercise 1.3: Setup Android Tablet Emulator using AVD manager with following
configurations:
Device Definition: Pixel C
System image: Nougat
AVD Name: My Tablet
Creating basic Android Applications in
Android Studio
Upon successful completion of this lab the students will be able to:
Run Android Studio and develop simple android applications.
2.1 Tools
Android Studio (Version: 2.3 or higher)
2.2 Background
2.2.1 Android Project
A project in Android Studio contains everything that defines your workspace for an app, from
source code and assets, to test code and build configurations.
When you start a new project, Android Studio creates the necessary structure for all your files
and makes them visible in the Project window on the left side of the IDE.
2.2.2 Modules
A module is a collection of source files and build settings that allow you to divide your project
into discrete units of functionality. Your project can have one or many modules and one
module may use another module as a dependency. Each module can be independently built,
tested, and debugged.
You can add a new module to your project by clicking File > New > New Module. Android
Studio offers a few distinct types of module:
Android app module: Provides a container for your app's source code, resource files, and
app level settings such as the module-level build file and Android Manifest file. When you
create a new project, the default module name is "app".
Library module: Provides a container for your reusable code, which you can use as a
dependency in other app modules or import into other projects. Structurally, a library
module is the same as an app module, but when built, it creates a code archive file
instead of an APK, so it can't be installed on a device.
Google Cloud module: Provides a container for your Google Cloud backend code. This
module adds the required code and dependencies for a Java App Engine backend that
uses simple HTTP, Cloud Endpoints, and Cloud Messaging to connect to your app.
java: Contains the Java source code files, separated by package names, including JUnit test
code.
res: Contains all non-code resources, such as XML layouts, UI strings, and bitmap images,
divided into corresponding sub-directories.
When you select Project view, you can see a lot more files
and directories. The most important of which are the
following:
One APK file contains all the contents of an Android app and is the file that Android-powered
devices use to install the app.
Activities
Fragments
Services
Content Providers
Broadcast receivers
Intents
Manifest File
Resources
2.2.7 Activity
An activity is an application component that provides a screen
with which user can interact to do something, such as dial a
phone, compose email, take a phone or edit a contact.
XML Layout File: Defines layout of the activity using XML language.
JAVA Source File: Defines behavior of the activity.
XML layout file is placed in app>res>layout>activity_main.xml. JAVA source file is placed in
app>java>com.example.ali.demo>MainActivity.Java.
The user interface for each component of your app is defined using a hierarchy of View and
ViewGroup objects. Each view group is an invisible container that organizes child views, while
the child views may be input controls or other widgets that draw some part of the UI.
2.3 Procedure
Example: Create an App containing one activity to count a number up and down.
Step 01: Launch Android Studio.
Step 02: To create a new project, click File > New > New Project.
Step 03: Enter the name of the project then click Next.
Step 06: Enter the activity name, the layout name, and the activity title. Then click Finish.
<TextView
android:id="@+id/tvNum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="0"
android:textAlignment="center"
android:textSize="120sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:onClick="onCountUp"
android:padding="20dp"
android:text="Count Up"
android:textSize="24sp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:onClick="onCountDown"
android:padding="20dp"
android:text="COunt Down"
android:textSize="24sp" />
</LinearLayout>
package com.example.ali.counter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
int num = 0;
TextView tvNum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvNum = (TextView)findViewById(R.id.tvNum);
}
Step 09: Click Run button from toolbar and select emulator or hardware device to test
your application
EXERCISE
Exercise 2.1: Develop an Android App with one activity as shown below. It should contain 2
buttons to count up and down. As the number goes larger than 10 it should change its
color to Red and if the number goes below 0 it should turn its color to Blue. The default color
id Black. Also display the name of color in a TextView.
activity_main.xml
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/topAppBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/black"
app:title="Exercise 2.1"
app:titleCentered="true"
app:titleTextColor="@color/white" />
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="100dp"
android:paddingRight="100dp">
<TextView
android:id="@+id/tvCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/counter_value"
android:textColor="@color/black"
android:textSize="100sp" />
<TextView
android:id="@+id/tvCountTextColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="@string/blackColor"
android:textColor="@color/grey"
android:textSize="30sp"
android:textStyle="bold" />
<Button
android:id="@+id/btnCountUp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/count_up" />
<Space
android:layout_width="wrap_content"
android:layout_height="10dp"
android:scaleY="10" />
<Button
android:id="@+id/btnCountDown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/count_down" />
</LinearLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
MainActivity.java
package com.lab1.task1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCountUp.setOnClickListener(this::countUp);
btnCountDown.setOnClickListener(this::countDown);
Exercise 2.2:
Develop an Android App with one activity as shown below. It should contain a textview
displaying MUET and a seek bar. As you move the seek bar the text size of the text in text
view should increase and decrease.
activity_main.xml
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/topAppBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/black"
app:title="Exercise 2.2"
app:titleCentered="true"
app:titleTextColor="@color/white" />
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tvTextDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/displayText" />
<TextView
android:id="@+id/tvSeekBarLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingRight="50dp"
android:paddingBottom="20dp"
android:text="@string/seekbarLabel"
android:textColor="@color/grey"
android:textSize="20sp" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="20"
android:thumbTint="@color/amber" />
</LinearLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
mainActivity.java
package com.lab1.task2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
SeekBar seekBar;
int progress = 0;
TextView tvTextDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
Lab 03 : Creating Layouts using different layouts
supported by Android Studio
Upon successful completion of this lab the students will be able to:
Design Activity layouts using Linear Layouts, Constraint Layouts, Grid Layouts, Relative
Layouts in Android Studio.
3.1 Tools
Android Studio (Version: 2.3 or higher)
3.2 Background
3.2.1 Layout
A layout defines the visual structure for a user interface, such as the UI for an activity or app
widget. You can declare a layout in two ways:
Declare UI elements in XML: Defining each view component using XML language.
Instantiate layout elements at runtime: Create layout programmatically using JAVA language.
Step 02: Write the Application name “LinearLayoutExample” for new Android Studio Project.
Step 06: Set the activity name “LinearLayoutActivity” and press Finish.
Step 07: Open acivity_linear_layout.xml file and remove all the child views.
From To
<android.support.constraint.ConstraintLayout> <LinearLayout>
</android.support.constraint.ConstraintLayout> </LinearLayout>
Step 09: Drag the following Views and ViewGroups with following properties in to the Activity
using deign view:
View/View Group Property Value
id LinearLayout_Root
layout_width match_parent
LinearLayout layout_height match_parent
(Root) orientation vertical
padding 15dp
weightSum 3
id LinearLayout_Inner_1
layout_width match_parent
LinearLayout
layout_height 0dp
(Inner 1)
orientation vertical
layout_weight 2
id tvFullName
layout_width match_parent
TextView
layout_height wrap_content
(Full Name)
text Full Name
textSize 25sp
id LinearLayout_Inner_3
layout_width match_parent
LinearLayout
layout_height wrap_content
(Inner 3)
orientation horizontal
layout_marginTop 10dp
id etFirstName
layout_width 0dp
layout_height wrap_content
EditText
layout_weight 1
(First Name)
hint First Name
inputType textPersonName
textSize 24sp
id etLastName
EditText
layout_width 0dp
(Last Name)
layout_height wrap_content
layout_weight 1
hint Last Name
inputType textPersonName
textSize 24sp
id tvPhone
layout_width match_parent
TextView layout_height wrap_content
(Phone) text Phone
textSize 25sp
marginTop 20dp
id LinearLayout_Inner_4
layout_width match_parent
LinearLayout layout_height wrap_content
(Inner 4) orientation horizontal
layout_marginTop 10dp
weightSum 10
id etCode
layout_width 0dp
layout_height wrap_content
EditText
layout_weight 2
(Code)
hint Code
inputType number
textSize 24sp
id etNumber
layout_width 0dp
layout_height wrap_content
EditText
layout_weight 8
(Number)
hint Number
inputType number
textSize 24sp
id btnViewData
layout_width wrap_content
layout_height wrap_content
layout_gravity center_horizontal
Button inputType number
(View Data) text View Data
textSize 25sp
layout_marginTop 20dp
onClick onViewDataClick
textAllCaps false
id LinearLayout_Inner_2
layout_width match_parent
LinearLayout
layout_height 0dp
(Inner 2)
orientation vertical
layout_weight 1
id tvData
TextView layout_width match_parent
(Data) layout_height match_parent
textSize 25sp
Step 10: Write the following code in LinearLayoutActivity.java file:
package com.example.ali.linearlayoutexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
EditText etFirstName;
EditText etLastName;
EditText etCode;
EditText etNumber;
TextView tvData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linear_layout);
etFirstName = (EditText)findViewById(R.id.etFirstName);
etLastName = (EditText)findViewById(R.id.etLastName);
etCode = (EditText)findViewById(R.id.etCode);
etNumber = (EditText)findViewById(R.id.etNumber);
tvData = (TextView)findViewById(R.id.tvData);
}
tvData.setText(data);
}
}
Step 11: Click Run button from toolbar and select emulator or hardware device to test your
application.
Example: Design the following Activity in Android Studio using Relative Layouts.
3.2.8 Grid Layout
A layout that places its children in a rectangular grid. A Grid Layout is divided by invisible
lines that form a grid containing rows and columns of cells. Child views are then placed in
cells and may be configured to cover multiple cells both horizontally and vertically.
Gaps between components in a Grid Layout may be implemented by placing a special type
of view called a Space view into adjacent cells, or by setting margin parameters. The
grid is composed of a set of infinitely thin lines that separate the viewing area into cells.
A grid with N columns has N + 1 grid indices that run from 0 through N inclusive.
Property Description
android:columnCount The maximum number of columns to create
when automatically positioning children.
android:rowCount The maximum number of rows to create when
automatically positioning children.
android:layout_row The row number in which the view is to be
placed. Row numbers start at 0.
android:layout_column The column number in which the view is to be
placed. Column numbers start at 0.
android:layout_rowSpan Number of rows to span vertically.
The table has as many columns as the row with the most cells. A table can leave cells
empty. Cells can span columns.
3.2.11 Table Layout Properties
Property Description
android:collapseColumns This specifies the zero-based index of the
columns to collapse. The column indices must
be separated by a comma: 1, 2, 5.
android:shrinkColumns The zero-based index of the columns to shrink.
The column indices must be separated by a
comma: 1, 2, 5.
android:stretchColumns The zero-based index of the columns to stretch.
The column indices must be separated by a
comma: 1, 2, 5.
3.2.12 Procedure
Example: Design the following Activity in Android Studio using Grid Layout.
Next.
child views.
From To
<android.support.constraint.ConstraintLayout> <GridLayout>
</android.support.constraint.ConstraintLayout> </GridLayout>
Step 09: Drag the following Views and ViewGroups into the Activity using deign view:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:padding="20dp"
android:text="1"
android:textSize="30sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textSize="30sp"
android:padding="20dp"
android:layout_columnWeight="2"
android:layout_rowWeight="1"
android:layout_columnSpan="2"
android:layout_rowSpan="2"
android:layout_gravity="fill"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textSize="30sp"
android:padding="20dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:textSize="30sp"
android:padding="20dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_columnSpan="2"
android:layout_gravity="fill"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:textSize="30sp"
android:padding="20dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_rowSpan="2"
android:layout_gravity="fill"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:textSize="30sp"
android:padding="20dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:textSize="30sp"
android:padding="20dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"/>
</GridLayout>
Step 11: Click Run button from toolbar and select emulator or hardware device to test your
application.
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="30sp"
android:layout_column="1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_column="2"
android:layout_span="2"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="30sp"
android:layout_column="1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_column="2"
android:layout_span="2"/>
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:textSize="30sp"
android:layout_column="2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Del"
android:textSize="30sp"
android:layout_column="3"/>
</TableRow>
<TableRow android:layout_marginTop="50dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End"
android:textSize="30sp"
android:layout_column="3"/>
</TableRow>
</TableLayout>
3.2.13 Absolute Layout
A layout that lets you specify exact locations (x/y coordinates) of its children. Absolute
layouts are less flexible and harder to maintain than other types of layouts without
absolute positioning.
Property Description
android:layout_x This specifies the x-coordinate of the view.
Frame Layout is designed to block out an area on the screen to display a single item.
Generally, Frame Layout should be used to hold a single child view, because it can be
difficult to organize child views in a way that's scalable to different screen sizes without the
children overlapping each other. In Frame Layout, all the child views added are placed like
stack.
The most recent added are shown on top. This means that developers must pay attention to
the order of widgets, while adding them to layout.
3.2.16 Frame Layout Properties
Property Description
android:foregroundGravity Defines the gravity to apply to the foreground
drawable.
android:measureAllChildren Determines whether to measure all children or
just those in the VISIBLE or INVISIBLE state when
measuring.
3.2.17
Procedure
Example: Design the following Activity in Android Studio using Absolute Layout.
press Next.
press Finish.
Step 08: Change the root tag from Constraint Layout to Absolute
Layout:
From To
<android.support.constraint.ConstraintLayout> <AbsoluteLayout>
</android.support.constraint.ConstraintLayout> </AbsoluteLayout>
Step 09: Drag the following Views and ViewGroups in to the Activity using deign view:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="First"
android:textSize="30sp"
android:layout_x="50dp"
android:layout_y="50dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Second"
android:textSize="30sp"
android:layout_x="170dp"
android:layout_y="150dp" />
</AbsoluteLayout>
Step 11: Click Run button from toolbar and select emulator or hardware device to test your
application.
Exercise 3.1: Design the following layouts using Linear Layout in XML.
layout_1.xml
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/first"
android:gravity="center"
android:textStyle="bold"
android:textSize="30sp"
android:text="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/second"
android:gravity="center"
android:textSize="30sp"
android:textStyle="bold"
android:layout_weight="1.5"
android:text="2" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/third"
android:gravity="center"
android:textSize="30sp"
android:textStyle="bold"
android:layout_weight="1.5"
android:text="3" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/fourth"
android:gravity="center"
android:textSize="30sp"
android:textStyle="bold"
android:layout_weight="1"
android:text="4" />
</LinearLayout>
</LinearLayout>
layout_2.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/second"
android:gravity="center"
android:text="1"
android:textSize="30dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/third"
android:gravity="center"
android:text="2"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/fourth"
android:gravity="center"
android:text="3"
android:textSize="30dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/first"
android:gravity="center"
android:text="4"
android:textColor="@color/white"
android:textSize="30dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/fifth"
android:gravity="center"
android:text="5"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/sixth"
android:gravity="center"
android:text="6"
android:textSize="30dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
add_student.xml
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textField"
style="?attr/textInputFilledStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/name">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:singleLine="false" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/rollNumberField"
style="?attr/textInputFilledStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="@string/roll_number">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/addStudentButton"
style="@style/Widget.Material3.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/cancel"
app:cornerRadius="10dp" />
<Button
android:id="@+id/cancelButton"
style="@style/Widget.Material3.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/add"
app:cornerRadius="10dp" />
</LinearLayout>
</LinearLayout>
prize.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/you_won_cash"
android:textSize="35sp"
android:textColor="@color/first"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/wholePartTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_99"
android:textColor="@color/second"
android:textSize="40sp"
android:textStyle="bold" />
<TextView
android:id="@+id/decimalPartTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/_75"
android:textColor="@color/second"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/congratulations"
android:textSize="50sp"
android:textColor="@color/fourth"
android:textStyle="bold" />
</LinearLayout>
Exercise 3.2: Create a Weather App containing one activity. Whenever the Random
temperature button is pressed it generates random temperature between -5°C
to 45ºC. The temperature label changes, and small box changes color
according to the following rules:
temperature.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="16dp">
<TextView
android:id="@+id/temperatureLevel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="WARM"
android:textStyle="bold"
android:textSize="20sp" />
<View
android:id="@+id/coloredBox"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginStart="8dp"
android:background="#C63D3D" />
</LinearLayout>
<TextView
android:id="@+id/temperatureValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="40°C"
android:textSize="60sp"
android:gravity="center"
android:textStyle="bold"
android:paddingTop="50dp" />
<Button
android:id="@+id/randomTemperatureButton"
style="@style/Widget.Material3.Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Random Temperature"
app:cornerRadius="10dp" />
</LinearLayout>
Exercise 3.3: Create Math App containing one activity that allows you to perform
calculations by entering two numbers and selecting operation. The app
displays the result as a Toast message.
calculate.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<com.google.android.material.textfield.TextInputLayout
style="?attr/textInputFilledStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/first_number"
android:layout_marginBottom="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/firstNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="?attr/textInputFilledStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/second_number"
android:layout_marginBottom="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/secondNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
</com.google.android.material.textfield.TextInputLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="16dp">
<RadioButton
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/addition" />
<RadioButton
android:id="@+id/subtract"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/subtraction" />
<RadioButton
android:id="@+id/multiply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/multiplication" />
<RadioButton
android:id="@+id/divide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/division" />
</RadioGroup>
<Button
android:id="@+id/calculate"
style="@style/Widget.Material3.Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/calculate"
android:textColor="@android:color/white"
app:cornerRadius="10dp" />
</LinearLayout>
calculate.java
package com.lab3.task3;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.textfield.TextInputEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstNumber = findViewById(R.id.firstNumber);
secondNumber = findViewById(R.id.secondNumber);
add = findViewById(R.id.add);
subtract = findViewById(R.id.subtract);
multiply = findViewById(R.id.multiply);
divide = findViewById(R.id.divide);
calculate = findViewById(R.id.calculate);
calculate.setOnClickListener(v -> {
float num1 =
Float.parseFloat(firstNumber.getText().toString());
float num2 =
Float.parseFloat(secondNumber.getText().toString());
String operation = "";
if (add.isChecked()) {
operation = "add";
} else if (subtract.isChecked()) {
operation = "subtract";
} else if (multiply.isChecked()) {
operation = "multiply";
} else if (divide.isChecked()) {
operation = "divide";
}
Toast.makeText(MainActivity.this, resultMessage,
Toast.LENGTH_SHORT).show();
});
}
switch (operation) {
case "add":
result = firstNumber + secondNumber;
break;
case "subtract":
result = firstNumber - secondNumber;
break;
case "multiply":
result = firstNumber * secondNumber;
break;
case "divide":
result = firstNumber / secondNumber;
break;
}
return result;
}
}
Exercise 3.4: Using Relative Layout design the following layout in Android Studio:
relative_layout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toStartOf="@id/textview2"
android:background="#FFC107"
android:paddingTop="40dp"
android:paddingBottom="40dp"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:text="-" />
<TextView
android:id="@+id/textview2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#4CAF50"
android:paddingTop="40dp"
android:paddingBottom="40dp"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:text="0" />
<TextView
android:id="@+id/textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toEndOf="@id/textview2"
android:background="#2196F3"
android:paddingTop="40dp"
android:paddingBottom="40dp"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:text="+" />
<TextView
android:id="@+id/textview4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textview1"
android:layout_alignParentLeft="true"
android:layout_toStartOf="@id/textview5"
android:background="#FF5722"
android:paddingTop="40dp"
android:paddingBottom="40dp"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:text="-5" />
<TextView
android:id="@+id/textview5"
android:layout_width="297dp"
android:layout_height="wrap_content"
android:layout_below="@id/textview3"
android:layout_alignParentRight="true"
android:background="#4FC3F7"
android:paddingTop="40dp"
android:paddingBottom="40dp"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:text="0"/>
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textview4"
android:layout_alignParentLeft="true"
android:layout_toStartOf="@id/textView7"
android:background="#5D4037"
android:paddingTop="50dp"
android:paddingBottom="50dp"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:text="0" />
<TextView
android:id="@+id/textView7"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@id/textview4"
android:layout_alignParentRight="true"
android:background="#3F51B5"
android:paddingTop="50dp"
android:paddingBottom="50dp"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:text="+5" />
<TextView
android:id="@+id/textview8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView7"
android:layout_alignParentBottom="true"
android:background="#B71C1C"
android:gravity="center"
android:textStyle="bold"
android:textSize="20sp"
android:text="CLEAR ALL" />
</RelativeLayout>
Exercise 3.5: Design the following layout using Grid Layout in Android Studio:
grid_layout.xml
<TextView
android:id="@+id/textview1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_row="0"
android:layout_column="0"
android:layout_columnWeight="1"
android:background="#4CAF50"
android:gravity="center"
android:text="1"
android:textSize="30sp" />
<TextView
android:id="@+id/textview2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_row="0"
android:layout_column="1"
android:layout_columnWeight="1"
android:background="#FFC107"
android:gravity="center"
android:text="2"
android:textSize="30sp" />
<TextView
android:id="@+id/textview3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_row="0"
android:layout_column="2"
android:layout_columnWeight="1"
android:background="#CDDC39"
android:gravity="center"
android:text="3"
android:textSize="30sp" />
<TextView
android:id="@+id/textview4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_row="1"
android:layout_column="0"
android:layout_columnWeight="1"
android:background="#D32F2F"
android:gravity="center"
android:text="4"
android:textSize="30sp" />
<TextView
android:id="@+id/textview5"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_row="1"
android:layout_column="1"
android:layout_columnWeight="1"
android:background="#FF5722"
android:gravity="center"
android:text="5"
android:textSize="30sp" />
<TextView
android:id="@+id/textview6"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_row="1"
android:layout_column="2"
android:layout_columnWeight="1"
android:background="#607D8B"
android:gravity="center"
android:text="6"
android:textSize="30sp" />
<TextView
android:id="@+id/textview7"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_row="2"
android:layout_column="0"
android:layout_columnWeight="1"
android:background="#4CAF50"
android:gravity="center"
android:text="7"
android:textSize="30sp" />
<TextView
android:id="@+id/textview8"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_row="2"
android:layout_column="1"
android:layout_columnWeight="1"
android:background="#ACACAC"
android:gravity="center"
android:text="8"
android:textSize="30sp" />
<TextView
android:id="@+id/textview9"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_row="2"
android:layout_column="2"
android:layout_columnWeight="1"
android:background="#A1887F"
android:gravity="center"
android:text="9"
android:textSize="30sp" />
</GridLayout>
Exercise 3.6: Design the following login form using Table Layout in Android Studio:
table_layout.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:stretchColumns="1">
<TableRow android:padding="5dp">
<TextView
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_span="2"
android:gravity="center_horizontal"
android:text="@string/loginForm"
android:textColor="@color/second"
android:textSize="25sp"
android:textStyle="bold" />
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_column="0"
android:padding="10dp"
android:text="@string/userName"
android:textSize="16sp" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/usernameField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="50dp" />
</com.google.android.material.textfield.TextInputLayout>
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_column="0"
android:padding="10dp"
android:text="@string/password"
android:textSize="16sp" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/passwordField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="50dp" />
</com.google.android.material.textfield.TextInputLayout>
</TableRow>
<TableRow android:layout_marginTop="20dp">
<Button
android:id="@+id/loginButton"
style="@style/Widget.Material3.Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_span="2"
android:paddingLeft="100dp"
android:paddingRight="100dp"
android:text="@string/login"
android:textColor="@color/white"
android:textSize="18sp"
app:cornerRadius="10dp" />
</TableRow>
</TableLayout>
Exercise 3.7: Design the following login form using Table Layout in Android Studio:
table_layout.xml
<TableRow
android:layout_width="match_parent"
android:background="@color/first"
android:padding="3dp">
<TextView
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:gravity="center"
android:text="Version"
android:textStyle="bold" />
<TextView
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:gravity="center"
android:text="Version Name"
android:textStyle="bold" />
<TextView
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:gravity="center"
android:text="API Level"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:padding="2dp">
<TextView
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:gravity="center"
android:text="5.0" />
<TextView
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:gravity="center"
android:text="Android Lolipop" />
<TextView
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:gravity="center"
android:text="21" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:padding="2dp">
<TextView
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:gravity="center"
android:text="4.4" />
<TextView
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:gravity="center"
android:text="Android Kitkat" />
<TextView
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:gravity="center"
android:text="19" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:padding="2dp">
<TextView
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:gravity="center"
android:text="4.3" />
<TextView
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:gravity="center"
android:text="Android Jelly Bean" />
<TextView
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:gravity="center"
android:text="18" />
</TableRow>
</TableLayout>
Exercise 3.9: Design the following layout using Absolute Layout in Android Studio:
absolute_layout.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100dp"
android:layout_y="50dp"
android:text="Absolute Layout Example"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="120dp"
android:layout_y="100dp"
android:text="Button Absolute Layout" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="170dp"
android:text="Absolute Layout Example"
android:textSize="18sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="250dp"
android:text="Button 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="250dp"
android:layout_y="250dp"
android:text="Button 2" />
</AbsoluteLayout>
Exercise 3.10: Design the following login form using Absolute Layout in Android Studio:
absolute_layout.xml
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:ignore="Deprecated">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="70dp"
android:text="@string/user_name" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/usernameField"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_x="150dp"
android:layout_y="45dp"
android:padding="10dp">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="50dp" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="170dp"
android:text="@string/password" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/passwordField"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_x="150dp"
android:layout_y="145dp"
android:padding="10dp">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="50dp" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_x="120dp"
android:layout_y="250dp"
android:text="@string/log_in" />
</AbsoluteLayout>
Exercise 3.11: Design the following login form using Frame Layout in Android Studio:
frame_layout.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#264653" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center"
android:padding="10dp"
android:text="Android"
android:textColor="@color/white"
android:textSize="30sp"
android:textStyle="bold" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:padding="50dp"
android:src="@drawable/art" />
</FrameLayout>
Exercise 3.12: Design the following login form using Frame Layout in Android Studio:
frame_layout_2.xml
<View
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_gravity="top"
android:layout_marginTop="10dp"
android:background="@color/black" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Mobile Application Development, \nMUET-CSE"
android:textColor="@color/white"
android:textSize="25sp" />
<TextView
android:layout_gravity="bottom|right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/gray"
android:padding="10dp"
android:text="11/Aug/2023"
android:textColor="@color/white"
android:textSize="18sp" />
</FrameLayout>