Android Programming
Android Programming
Android Programming
(IT-606)
For
Third Year Students
Department: Information Technology
1
Department of Information Technology
Vision of IT Department
Our graduates will exhibit team-work and leadership qualities to meet stakeholder business
objectives in their careers.
Our graduates will evolve in ethical and professional practices and enhance socioeconomic
contributions to the society.
2
Program Outcomes (POs)
Engineering Graduates will be able to:
3
Course Outcomes
CO3 : Use Intents for activity and broadcasting data in Android App.
CO5 : Experiment with Camera and Location Based service and develop Android App with
Security features.
4
Course Course Outcomes CO
Attainment
Experiment with
Camera and Location
Based service and
CO1 2 0 0 0 0 0 0 0 0 0 0 0 1 1
develop Android
App with Security
features.
Design and
Implement User
CO2 Interfaces and 0 2 0 1 1 0 0 0 1 0 0 0 1 1
Layouts of Android
App.
Use Intents for
activity and
CO3 1 1 2 1 2 0 0 0 0 0 0 0 2 1 0
broadcasting data in
Android App.
Design and
Implement Database
CO4 1 1 0 1 0 0 0 0 0 0 0 0 1 1
Application and
Content Providers.
Experiment with
Camera and Location
Based service and
0 1 2 0 1 1 0 0 1 0 0 0 2 1 0
develop Android
CO5
App with Security
features.
5
List of Program
6
1. Introduction to android.
Applications
Application Framework
Android Runtime
Platform Libraries
Linux Kernel
Applications –
The top layer of the Android architecture is called Applications. Only this layer will
have the installed third-party apps (such games and chat apps) and pre-installed apps
(including contacts, camera, gallery, and home) from the play store. With the aid of
the classes and services the application framework offers, it operates inside the
Android run time.
Application framework –
A number of crucial classes are provided by the Application Framework and are
needed to construct an Android application. It assists in controlling the user interface
with application resources and offers a general abstraction for hardware access.
Generally speaking, it offers the services that enable us to develop a certain class and
make it useful for the building of applications. It contains a variety of services, such as
package manager, view system, activity manager, notification manager, and others,
that are useful for developing our application in accordance with the requirements.
1
Application runtime –
One of the most crucial components of Android is the Android Runtime
Environment. It includes elements such as the Dalvik virtual machine (DVM)
and core libraries. It primarily serves as the foundation for the application
framework and, with the aid of the core libraries, powers our application.
Similar to Java Virtual Machine (JVM), Dalvik Virtual Machine (DVM) is a
register-based virtual machine designed specifically for Android and
optimized to run many instances of the program on a device seamlessly. The
Linux kernel layer is responsible for threading and low-level memory
2
management. Because of the fundamental libraries, we can create Android
applications using the standard Java or Kotlin programming languages.
Platform libraries –
To enable Android development, the Platform Libraries comprise a number
of Java-based and C/C++ essential libraries, including Media, Graphics, Surface
Manager, OpenGL, and others.
• Support for playing and recording audio and video formats is offered by the
media library.
• For 2D and 3D computer graphics, there are two cross-language and cross-platform
application program interfaces (APIs): SGL and OpenGL.
• FreeType offers font support, while SQLLite offers database functionality.
• Web-Kit The entire feature set for displaying web content and streamlining page
loading is provided by this open source web browser engine.
• A secure technology called Secure Sockets Layer creates an encrypted connection
between a web server and a web browser.
Linux Kernel –
The core of the Android architecture is the Linux kernel. It controls all of the
drivers that are needed during runtime, including those for displays, cameras,
Bluetooth devices, audio devices, memory cards, and more. An abstraction layer
between the hardware of the device and the other elements of the Android
architecture will be provided by the Linux Kernel. Memory, power, device
management, and other things fall under its purview. The Linux kernel's
characteristics are:
• Security: The protection of the application from the system is handled by the
Linux kernel.
3
• Driver Model: This guarantees that the program functions correctly on the
hardware and that the hardware manufacturers are in charge of integrating their
drivers into the Linux build.
4
2. Development of hello world application.
The first step in designing an Android application that displays Hello World is to use
Android Studio to construct a basic Android application. The screen that appears when
you click on the Android Studio icon is as follows.
5
Set Up the Specifics of the Hello World Project
By setting up the project's name, location, and API version, we'll complete its creation.
6
Modify the application's name. You can either leave the project's default location
alone or change it to your chosen directory.
Make sure that API 15: Android 4.0.3 IceCreamSandwich is selected as the Minimum
SDK for the minimum API level. This guarantees that practically every device can use
your application.
The next step in the installation process should be choosing the mobile activity, which
sets the application's default layout.
7
SOURCE CODE :
The Main Activity File
The main activity code is a Java file MainActivity.java. This is the actual application
file which
ultimately gets converted to a Dalvik executable and runs your application
package com.example.helloworldapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
The next step is to enable USB debugging so your phone can interact with your
computer in a developer mode.
Now, we can launch apps from Android Studio onto our device:
1. Select one of your projects and click "Run" from the toolbar.
2. In the "Choose Device" window that appears, select the "Choose a running device"
radio button, select the
device, and click OK.
II) Running app on Emulator(AVD)
To run the app from Android studio, open one of your project's activity files and click
Run icon from the toolbar. Android studio installs the app on your AVD and starts it
and if everything is fine with your set-up and application, it will display following
Emulator window −Once Gradle finishes building, Android Studio should install the
app on your connected device and start it.
10
11
3. Program for building a simple user interface using a XML for UI
layout
Aim:- Program for building a simple user interface using a XML for
UI layout
Objective: Student should be able to design their own UI for android
application using XML.
<EditText
android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
Res/values/Strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">second</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
12
13
14
4. Program for developing an Android Application using a linear layout.
Aim:- Program for developing an Android Application using a linear layout Objective:
Outcome: Student will demonstrate the android application using linear layout.
Software:
Theory:
The Linear Layout is one of the simplest layout classes. It allows you to create simple UIs
(or UI elements) that align a sequence of child Views in either a vertical or a horizontal
line. Linear Layout is a view group that aligns all children in a single direction, vertically
or horizontally.
Code:
Activity_main.xml:
<LinearLayout 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"
android:orientation="vertical" >
<Button
android:id="@+id/btnStartService" android:layout_width="150dp"
android:layout_height="wrap_content" android:text="@string/start_service" />
15
<Button android:id="@+id/btnStopService" android:layout_width="150dp"
android:layout_height="wrap_content" android:text="@string/stop_service" />
</LinearLayout>
Res/values/Strings.xml:
<string name="app_name">linearapp</string>
<string name="menu_settings">Settings</string>
<string name="start_service">Start</string>
<string name="pause_service">Pause</string>
<string name="stop_service">Stop</string>
</resources>
Output:
16
17
Result: - Students design android application using linear layout.
18
5. Program for developing an Android Application using a Relative
layout
Objective: Student should be able to design android application using relative layout.
Outcome: Student will demonstrate the android application using relative layout.
Software:
Theory:
The Relative Layout provides a great deal of flexibility for your layouts, allowing you
to define the position of each element within the layout in terms of its parent and other
views. Relative Layout is a view group that displays child views in relative positions.
Code:
Activity_main.xml:
<RelativeLayout 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" android:orientation="vertical">
19
<Button android:id="@+id/btnStopService" android:layout_width="100dp"
android:layout_height="wrap_content" android:layout_alignParentRight="true"
Res/values/Strings.xml:
<string name="app_name">third</string>
<string name="menu_settings">Settings</string>
<string name="start_service">Start</string>
<string name="pause_service">Pause</string>
<string name="stop_service">Stop</string>
</resources>
OUTPUT:
20
Result: - Students design an android application using relative layout.
21
6. Program for developing an Android Application using a Table
layout
Objective: Student should be able to design android application using Table layout.
Outcome: Student will demonstrate the android application using Table layout.
Software:
Theory:
Table Layout is a view that groups views into rows and columns.
Code:
Activity_main.xml:
<TableRow>
22
<TextView>
android:text="FirstName" android:layout_width="wrap_content"
android:layout_height="wrap_content"android:layout_column="1" />
<EditText
android:width="100dp" android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
</TableLayout>
Res/values/Strings.xml
<string name="app_name">tablelayout</string>
<string name="menu_settings">Settings</string>
</resources>
Output:
23
24
Result: - Students design an android application using table layout.
25
7. Program for developing an Android Application using Absolute
layout
Objective: Student should be able to design android application using absolute layout.
Outcome: Student will demonstrate the android application using absolute layout.
Software:
Theory:
Absolute Layout enables you to specify the exact location of its children.
Code:
Activity_main.xml:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button android:layout_width="100dp"
android:layout_height="wrap_content" android:text="ok" android:layout_x="50dp"
android:layout_y="361dp" />
<Button
android:layout_width="
100dp"
android:layout_height="wrap_content" android:text="Cancel" android:layout_x="225dp"
android:layout_y="361dp" />
26
</AbsoluteLayout>
Res/values/Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">absolutelayout</string>
<string name="menu_settings">Settings</string>
</resources>
Output:
27
8. Program for developing an Android Application using a Frame
layout.
Software:
Theory:
The Frame Layout is a placeholder on screen that you can use to display a single view.
Code:
a. MainActivity.Java code
package
com.example.framelayo
ut;import
android.os.Bundle;
import android.app.Activity;
28
public class MainActivity
}
}
b. activity_main.xml code
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:src="@drawable/ic_launcher"
android:scaleType="fitCenter"
android:layout_height="250dp"
android:layout_width="250dp"/>
<TextView
android:text="Frame
Demo"
android:textSize="30
sp"
android:textStyle="b
old"
android:layout_height="fill_
parent"
android:layout_width="fill_
parent"
android:gravity="center" />
</Frame
29
Layout>
Res/valu
es/String
s.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">framelayout</string>
<string name="menu_settings">Settings</string>
</resources>
30
Output:
31
Result: - Students design an android application using frame layout.
32
9. Developing an android application using Relative layout to
display Date and time.
Outcome: Student will demonstrate the android application using relative layout
to displayDate and time.
Software:
Theory:
Relative Layout is a view group that displays child views in relative positions.
Code:
1. MainActivity.Java
package com.example.relativeapp;
import java.text.SimpleDateFormat;
import
java.util.Date;
import
android.os.Bu
33
ndle; import
android.app.A
ctivity;
import
android.view.M
enu;
import android.widget.TextView;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
34
1. res/layout/activity_main.xml
<RelativeLayout
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"
android:paddingLeft="
16dp"
android:paddingRight=
"16dp"
>
<TextView
android:id = "@+id/dates" android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/times"
/>
<TextView android:id="@+id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentRight="true
" />
<Button
android:layout_width
35
="96dp"
android:layout_height="wrap_content"
android:layout_below="@+id/dates"
android:layout_toLeftOf="@+id/times"
android:text="@string/done" />
</RelativeLayout>
1. res/values/strings.xml
<string name="app_name">relativeapp</string>
<string name="action_settings">Settings</string>
<string name="reminder">Enter your name</string>
<string name = "done">Done</string>
</resources>
Output:
36
Result: - Students design an android application using relative layout to display Date &
Time.
37
38