0% found this document useful (0 votes)
5 views

practical-file-for-android-programming

This document is a practical file for the Android Programming course (UGCA 1944) for final year BCA students at I.K. Gujral Punjab Technical University. It includes detailed instructions on setting up Java and Android development environments, as well as various practical exercises covering Java programming concepts and Android application development. The file is structured with an index and multiple practical sections that guide students through essential programming tasks and concepts.

Uploaded by

hicacay952
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

practical-file-for-android-programming

This document is a practical file for the Android Programming course (UGCA 1944) for final year BCA students at I.K. Gujral Punjab Technical University. It includes detailed instructions on setting up Java and Android development environments, as well as various practical exercises covering Java programming concepts and Android application development. The file is structured with an index and multiple practical sections that guide students through essential programming tasks and concepts.

Uploaded by

hicacay952
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

lOMoARcPSD|55218029

Practical File for Android Programming (UGCA 1944) - BCA


Final Year
Bachelor of Computer Applications (I. K. Gujral Punjab Technical University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by nope open ([email protected])
lOMoARcPSD|55218029

PRACTICAL FILE OF
ANDROID PROGRAMMING
SUBECT CODE :- UGCA 1944

SUBMITTED TO: SUBMITTED BY:

Mrs. Narinder Kaur Ramandeep Kaur

(Assistant Professor) CLASS: B.C.A Final Year

ROLL NO: 2120597

SHRI GURU RAMDAS INSTITUTE OF


MANAGEMENT AND TECHNOLOGY
(HALWARA)
Page 1

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

INDEX
S No. Practical Name Page No. Remarks

1. Installation of Java, Android framework. 3-5

2. Android SDK Manager and its all components. 6-9

3. Programs based on the overriding, constructor, 10-11


classes in JAVA.

4. Program based on static keyword in JAVA. 12-12

5. Directory structure of an Android project. 13-14

6. Application based on Text box and button. 15-17

7. Application based on Radio button. 18-21

8. Application based on Intents and Intent Filters. 22-23

9. Application based on Activities and Services. 24-25

10. Application based on Action Bar. 26-26

11. Application based on Option Menu. 27-28

12. Application based on Rating Bar. 29-30

13. Application based on Media Player. 31-36

14. Application based on Content Providers. 37-38

15. Application based on accessing camera. 39-41

Page 2

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Practical – 1
Installation of JAVA, android Framework.
Android Studio, Java JDK & SDK is part of Android Development for building Android Apps. In
this post, we’re going to learn “How to install & Setup Android Studio Java JDK & SDK“.
We’ll also see, how to download the Android Studio and Java JDK, as well as the SDK (Software
Development Kit) setup.

Install JAVA JDK

The Java Development Kit (JDK), officially called “Java SE”, is needed for writing Java programs.
The JDK is freely available from Sun Micro-systems, now it is part of Oracle. check
it– http://www.oracle.com/technetwork/java/javase/overview/index.html.

Un-Install Older Version(s) of JDK/JRE

 Install only the newest Java JDK.


 If you have installed the older versions of JDK/JRE, uninstall all of them.
 Go to Control Panel >> “Programs” >> Select Programs and Features” >> Uninstall
ALL programs begin with “Java”, like “Java SE Development Kit, etc.

Step 1: Download JDK

 Goto Java SE download


site http://www.oracle.com/technetwork/java/javase/downloads/index.html.
 Click the “Oracle JDK Download” button.
 Check “Accept License Agreement”.
 Choose the JDK as per your system requirement.

Step 2: Install JDK

 Double clicks on download to setup and Run.


 By default, JDK is installed in the directory “C:\Program Files\Java\jdk-13.0.“, Accept the
defaults.

Step 3: Verify the JDK Installation

Open a CMD via the following steps:

Page 3

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Click the Search button and Type “cmd” ⇒ Choose “Command Prompt”, or Right-
click “Start” button ⇒ run ⇒ enter “cmd”.

the following commands to verify that JDK/JRE is properly installed and display their version.

Installing the Android SDK

Note: You can copy the SDK from your friends or any other computer with the same OS because
the download size is 3GB of zip data and expand to 5 GB of disk data.

1. Start Android Studio.


2. Select >Tools > SDK Manager.
3. As you can see default Settings dialog box, click these tabs to install Android SDK platform
packages and developer tools.

SDK Platforms: Select the latest Android SDK package.

SDK Tools: Select these Android SDK tools:

 Android SDK Build-Tools.


 NDK.
 Android SDK Platform-Tools.
 Android SDK Tools.

Page 4

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

4. Click Apply. Android Studio starts installing your selected packages. After completing the
installation, the Status of the installed packages and tools changes from Not
Installed to Installed.

5. Click OK.

Page 5

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Practical -2

Android SDK Manager and its all components.

Android SDK is a collection of libraries and Software Development tools that are essential for
Developing Android Applications. Whenever Google releases a new version or update of
Android Software, a corresponding SDK also releases with it. In the updated or new version of
SDK, some more features are included which are not present in the previous version. Android
SDK consists of some tools which are very essential for the development of Android
Application. These tools provide a smooth flow of the development process from developing
and debugging. Android SDK is compatible with all operating systems such as Windows, Linux,
macOS, etc.

Components of Android SDK

Page 6

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Android SDK Components play a major role in the Development of Android applications. Below
are the important components:

1. Android SDK Tools:- Android SDK tool is an important component of Android SDK. It
consists of a complete set of development and debugging tools. Below are the SDK developer
tools:
 Android SDK Build tool.
 Android Emulator.
 Android SDK Platform-tools.
 Android SDK Tools.
These are shown below :

2. Android SDK Build-Tools:- Android SDK build tools are used for building actual binaries
of Android App. The main functions of Android SDK Build tools are built, debug, run and test
Android applications. The latest version of the Android SDK Build tool is 30.0.3. While
downloading or updating Android in our System, one must ensure that its latest version is
download in SDK Components.

3. Android Emulator:-An Android Emulator is a device that simulates an Android device on


your system. Suppose we want to run our android application that we code. One option is that
we will run this on our Android Mobile by Enabling USB Debugging on our mobile. Another
option is using Android Emulator. In Android Emulator the virtual android device is shown on
our system on which we run the Android application that we code.

Page 7

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Thus, it simply means that without needing any physical device Android SDK component
“Android Emulator” provides a virtual device on the System where we run our Application. The
emulator’s come with the configuration for Various android phones, tablets, Wear OS, and
Android TV devices.
4. Android SDK Platform-tools:- Android SDK Platform-tools is helpful when we are working
on Project and they will show the error messages at the same time. It is specifically used for
testing. It includes:
 Android Debug Bridge (ADB), is a command-line tool that helps to communicate with the
device. It allows us to perform an action such as Installing App and Debugging App etc.
 Fastboot allows you to flash a device with a new system image.
 Systrace tools help to collect and inspect timing information. It is very crucial for App
Debugging.
5. Android SDK Tools:- Android SDK tool is a component of SDK tool. It consists of a set of
tools which and other Utilities which are crucial for the development of Android Application. It
contains the complete set of Debugging and Development tools for android.
6. SDK Platforms:- For Each Android Software, one SDK platform is available as shown
below:

In this Android 11.0(R) is installed.


These are numbered according to the android version. The new version of the SDK platform has
more features and more compatible but the old version is less compatible with fewer features.
Like in Android 11.0(R) have more compatible and have more feature but the below versions
like Android 10.0(Q), Android4.4(KitKat) have less feature and is less compatible.

7. SDK Update Sites:-In SDK Update Sites, some sites are embedded in it which will check for
Android SDK Updates Tools. In this, one must ensure we don’t unclick the button below because
these are checked by default which will check for updates if we will unclick it then it doesn’t
check updates for those.

Page 8

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Page 9

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Practical -3
Programs based on the overriding, constructor, classes in java.
 Overriding in JAVA:-
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move(); // runs the method in Animal class
b.move(); // runs the method in Dog class
}
}

Output:
Animals can move
Dogs can walk and run

 Constructor in JAVA :
class Main {

Page 10

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

private String name;


// constructor
Main() {
System.out.println("Constructor Called:");
name = "Programiz";
}
public static void main(String[] args) {
// constructor is invoked while
// creating an object of the Main class
Main obj = new Main();
System.out.println("The name is " + obj.name);
}
}

Output:
Constructor Called:
The name is Programiz

Classes in JAVA : Java is an object-oriented programming language.Everything in Java is


associated with classes and objects, along with its attributes and methods. For example: in real life,
a car is an object. The car has attributes, such as weight and color, and methods, such as drive
and brake.A Class is like an object constructor, or a "blueprint" for creating objects.

Create a Class: To create a class, use the keyword class:


Main.javaGet your own Java Server
Create a class named "Main" with a variable x:
public class Main
{
int x = 5;
}

Page 11

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Practical-4
Program based on static keyword in JAVA.
The static keyword in Java is used for memory management mainly. We can apply static
keyword with variables, methods, blocks and nested classes. The static keyword belongs to the
class than an instance of the class.
The static can be:
 Variable (also known as a class variable)
 Method (also known as a class method)
 Block
 Nested class

//Java Program to demonstrate the use of static variable


class Student{
int rollno;//instance variable
String name;
static String college ="ITS";//static variable
//constructor
Student(int r, String n){
rollno = r;
name = n;
}
//method to display the values
void display (){System.out.println(rollno+" "+name+" "+college);}
}
//Test class to show the values of objects
public class TestStaticVariable1{
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
//we can change the college of all objects by the single line of code
//Student.college="BBDIT";
s1.display();
s2.display();
}
}
Output:
111 Karan ITS

Page 12

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

222 Aryan ITS

Practical-5
Directory Structure of an Android project.

Android Directory Structure :-Generally we have three kinds of android projects such as:

Android Projects:- An Android project is the container for our application's source code,
resource files, and files such as the Ant build and Android Manifest file. An application
project is the main type of project and the contents are eventually built into an .apk file
that you install on a device.

Test Projects:- These projects contain code to test our application projects and are built into
applications that run on a device.

Library Projects:- These projects contain shareable Android source code and resources that we
can reference in Android projects. This is useful when we have common code that we want to
reuse. Library projects cannot be installed onto a device, however, they are pulled into the .apk file
at build time.

The main objective of an android project is to build an .apk file from it. And for this when we start
with an android project then some directories creates by default and some needs few customization
to enhance android applications. So let’s understand the directory structure in android project to
develop an android application.

/src – This is the most common folder in any android project. It contains our java source code for
android application. Under this folder we can see our java source files in which we write logic for
android applications.

Page 13

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

/gen – In most IDE(such as Eclipse) this folder is created by IDE for configuration purpose. Under
this folder we can find two java files such as BuildConfig.java and R.java. Both files are auto
generated by IDE which helps android project to work smoothly.

/Android(version name) – Under this folder we can see some android.jar file which relates to
android sdk platform. We should not modify this file for smooth functioning of our project.

/assets – By default this folder is empty, but we can store raw asset files such as music file,data
file etc in this folder. The files retains its original name and structure when compiled into
an .apk file.

/bin – This is a very important folder in our directory structure as it


contains AndroidManifest.xml file and serve as a output directory for our project which further
build an .apk file.

/libs – This folder contains private libraries.

/res – This folder contains lots of required sub folders for the project. Let’s understand these
folders one by one.

a) /drawable – Under this folder we store images(PNG,JPEG,GIF) for our project. Generally this
folder comes with five types of sub folders which ensures rendering of images on mobile devices
according to its screen resolution. There are five sub folders with different names such as:
drawable-hdpi,drawable-ldpi,drawable-mdpi,drawable-xhdpi,drawable-xxhdpi (where dpi refers
to dot per inch). Normally Google prefers .PNG format for images.

b) /layout – Under this folder we store UI layouts file for our android project, which is written
in xml format. we can store as many activity file under this folder. In other words this folder relates
to our application UI layouts.

c) /menu – For XML files that define application menus.

d) /values – In this folder we place predefined application values in the form of XML files that
define the variable names and their value that will be later referenced in our java source code. In
simple words, it holds all our constant values for our application in one place. In this way we can
customize our applications at one place as per our requirements. Within this folder we can find
three xml files such as:

i) dimens.xml: This XML file defines the dimension values such as font sizes and standard heights
for our UI.

ii) strings.xml: In this file we define text strings which is used in the application. for example: if
we wish to change the title of an application, then we can simply change in this file instead of
doing any hard coding in application.

Page 14

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

iii) styles.xml: This XML file defines styles which is used in the application. Generally we apply
these styles to our UI elements. With the help of style we can separate the look of our application
from the layout and functionality.

/project.properties: This file contains project settings, such as the build target. We can edit
project properties in eclipse(IDE).

Practical -6
Application based on text box and button.
Creating a Button
 For creating a button we can use Button tag
 Just go inside the layout and type <Button

You will get a dropdown now press enter. Now you can set the width and height for you button.
You see the xml code for a button below.

<Button
android:id="@+id/button"

Page 15

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a button" />

android:id=”@+id/button”: This is the id for your button after @+id/ i.e. here the id for your
button is “button” (without quotes). This id will be used to handle the button in java
coding. Whenever we put @+id in android:id tag of any view a respective hex value is stored
in a variable named with the id you have given here in R.java file. Now these things are done
automatically so you do not need to bother much about this. Basically what you need to know is
this is the id that will be used for handling this button.
Now we will create an Edit Text. For creating Edit Text we have the tag <EditText

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

So the code for the final layout that we have created yet is.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a button" />

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Page 16

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

</LinearLayout>

Now if you will switch to the design view you will see something like this:-

So as you can see we have our Button and EditText.

Page 17

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Practical -7
Application based on Radio Button.

RadioButton is a two states button which is either checked or unchecked. If a single radio button
is unchecked, we can click it to make checked radio button. Once a radio button is checked, it
cannot be marked as unchecked by user. RadioButton is generally used with RadioGroup.
RadioGroup contains several radio buttons, marking one radio button as checked makes all other
radio buttons as unchecked.

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="example.javatpoint.com.radiobutton.MainActivity">

<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal"

Page 18

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

android:textSize="22dp"
android:text="Single Radio Buttons" />

<!-- Default RadioButtons -->


<RadioButton
android:id="@+id/radioButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Radio Button 1"
android:layout_marginTop="20dp"
android:textSize="20dp" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Radio Button 2"
android:layout_marginTop="10dp"
android:textSize="20dp" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:background="#B8B894" />
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

Page 19

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

android:layout_marginTop="30dp"
android:gravity="center_horizontal"
android:textSize="22dp"
android:text="Radio button inside RadioGroup" />

<!-- Customized RadioButtons -->


<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGroup">
<RadioButton
android:id="@+id/radioMale"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Male"
android:layout_marginTop="10dp"
android:checked="false"
android:textSize="20dp" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Female"
android:layout_marginTop="20dp"
android:checked="false"
android:textSize="20dp" />
</RadioGroup>
<Button

Page 20

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected"
android:id="@+id/button"
android:onClick="onclickbuttonMethod"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Output:

Page 21

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Practical-8
Application based on Intents and Intents Filters.
An Intent is a messaging object you can use to request an action from another app component.
Although intents facilitate communication between components in several ways, there are three
fundamental use cases:

 Starting an activity
An Activity represents a single screen in an app. You can start a new instance of an Activity by
passing an Intent to startActivity(). The Intent describes the activity to start and carries any
necessary data.
If you want to receive a result from the activity when it finishes, call startActivityForResult(). Your
activity receives the result as a separate Intent object in your activity's onActivityResult() callback.
For more information, see the Activities guide.
 Starting a service
A Service is a component that performs operations in the background without a user interface.
With Android 5.0 (API level 21) and later, you can start a service with JobScheduler. For more
information about JobScheduler, see its API-reference documentation.
For versions earlier than Android 5.0 (API level 21), you can start a service by using methods of
the Service class. You can start a service to perform a one-time operation (such as downloading a
file) by passing an Intent to startService(). The Intent describes the service to start and carries any
necessary data.
If the service is designed with a client-server interface, you can bind to the service from another
component by passing an Intent to bindService(). For more information, see the Services guide.
 Delivering a broadcast

Page 22

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

A broadcast is a message that any app can receive. The system delivers various broadcasts for
system events, such as when the system boots up or the device starts charging. You can deliver a
broadcast to other apps by passing an Intent to sendBroadcast() or sendOrderedBroadcast().

The rest of this page explains how intents work and how to use them. For related information,
see Interacting with Other Apps and Sharing Content.

Intent types:- There are two types of intents:

 Explicit intents specify which application will satisfy the intent, by supplying either the target
app's package name or a fully-qualified component class name. You'll typically use an explicit
intent to start a component in your own app, because you know the class name of the activity
or service you want to start. For example, you might start a new activity within your app in
response to a user action, or start a service to download a file in the background.
 Implicit intents do not name a specific component, but instead declare a general action to
perform, which allows a component from another app to handle it. For example, if you want
to show the user a location on a map, you can use an implicit intent to request that another
capable app show a specified location on a map.

Example filters : To demonstrate some of the intent filter behaviors, here is an example
from the manifest file of a social-sharing app:

<activity android:name="MainActivity" android:exported="true">


<!-- This activity is the main entry, should appear in app launcher -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name="ShareActivity" android:exported="false">


<!-- This activity handles "SEND" actions with text data -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
<!-- This activity also handles "SEND" and "SEND_MULTIPLE" with media data -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/vnd.google.panorama360+jpg"/>
<data android:mimeType="image/*"/>

Page 23

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

<data android:mimeType="video/*"/>
</intent-filter>
</activity>

The first activity, MainActivity, is the app's main entry point—the activity that opens when the
user initially launches the app with the launcher icon:

 The ACTION_MAIN action indicates this is the main entry point and does not expect any intent
data.
 The CATEGORY_LAUNCHER category indicates that this activity's icon should be placed in
the system's app launcher. If the <activity> element does not specify an icon with icon, then the
system uses the icon from the <application> element.

Practical -9
Application based on Activities and Services.
Activity:- An activity is a single, focused thing that the user can do. It is the foreground task that
is right in front of the user. Almost all activities interact with the user, so the Activity class creates
a window for you to place your UI by loading a UI layout.
How to Interact with User:- The way an Activity interacts with a user is by responding to events.
The UI elements in your layout are often bonded to the Activity through functions or methods. For
example, your UI has a TextView to show some static text and you want to change it when the
user presses a button. For this to work your Button must bind to a function in your activity class
and it will invoke that function every time it is pressed. The function will set the new text for the
TextView by interacting with the reference of the instanced TextView.
When to Use Activity:- You will likely be using Activities if your app depends on user inputs.
The activity displays the UI and handles any user inputs, which means your app likely have a few
activities. A simple rule to go by is that if UI is involved then you want to use an Activity. If UI is
not necessary then a Service might be better for your need.

Service:- A Service in Android is an application component that can perform long-running


operations in the background. This means that a Service does not provide a UI. A service can run
independently meaning that after your app starts the service, it can run even when your app is no
longer in focus.A Service provides great flexibility by having three different types. They are
Foreground, Background, and Bound.
Foreground:- A foreground service performs operations that is noticeable to the user. For
example, a foreground service would be playing audio. A foreground service continues to run even

Page 24

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

when the user is not interacting with your app. It is important to note that foreground services must
display a status bar icon.
Background:- A background service performs an operation that is not directly noticeable by the
user. For example, a service that compacts its storage or updates a database.
Bound:- A service is bound when an application component binds to it through the bindService()
function. Bound service offers a client-server interface that allows an application component to
interact with the service, send requests, receive requests, and even do so with interprocess
communication (IPC).
Unlike other types of services, a bound service runs as long as another application service is bound
to it. For example, multiple components can bind to the service at once, but when all the
components unbind, the service is destroyed.
Persistent Service:- In addition, you can specify if a service is persistent or not. If a service is
persistent (Sticky), then if the Android system at any point kills the service’s process, it will mark
the service for restart as soon as resources are available. If the service is not persistent (Non-sticky),
then once the service’s process is killed while running, it will not be marked for restart. In order
to define whether a service is sticky or not, you must override the “onStartCommand” function to
return the flag for Sticky or Non-sticky.

When to Use Service:- You will likely use a service when you want to do something that does not
involve UI and needs it to run whether your app is running or not. For example, if you want to play
audio a service would be a choice because the audio will still play when the user opens another
app.

Using Activity and Service Together

Activity and Service communication with broadcast

Page 25

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

There are two simple ways for you to use an Activity and Service together. One way is to create a
bound service and bind a component in your activity to the service acting as a communication
channel. Another way is to use the Android Broadcast System, which sends out a broadcast intent
system-wide. For this to work, you will need to have your own set of actions in your app, which
only your app will understand. In your activity class, you will register a broadcast receiver that
will listen for any of the actions pertaining to your app. Since an intent can carry data, you can
communicate data back and forth between an Activity and Service through this method.

Practical -10
Application based on Action Bar.
In Android applications, ActionBar is the element present at the top of the activity screen. It is a
salient feature of a mobile application that has a consistent presence over all its activities. It
provides a visual structure to the app and contains some of the frequently used elements for the
users. Android ActionBar was launched by Google in 2013 with the release of Android 3.0(API
11). Before that, the name of this top most visual element was AppBar. AppBar contains only the
name of the application or current activity. It was not very much useful for the users and developers
also have negligible option to customize it.
Google announced a support library along with the introduction of ActionBar. This library is a
part of AppCompat and its purpose is to provide backward compatibility for older versions of
Android and to support tabbed interfaces. Components included in the ActionBar are:
 App Icon: Display the branding logo/icon of the application.
 View Controls: Section that displays the name of the application or current activity.
Developers can also include spinner or tabbed navigation for switching between views.
 Action Button: Contains some important actions/elements of the app that may be required
to the users frequently.
 Action Overflow: Include other actions that will be displayed as a menu.
Designing a Custom ActionBar:- The following example demonstrates the steps involved in
creating a custom ActionBar for the MainActivity of an application. All important aspects of visual
elements like icon, title, subtitle, action buttons, and overflow menu will be covered.

Page 26

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Default ActionBar:- As mentioned earlier, every android app contains an ActionBar by default.
This pre-included ActionBar display title for the current activity that is managed by
the AncdroidManifest.xml file. The string value of the application’s title is provided
by @string/app_name resource present under the application nodes.
<application
…..
android:label=”@string/app_name”
…..
</application>
Output:

Practical – 11
Application based on Option Menu.
Android Option Menus are the primary menus of android. They can be used for settings, search,
delete item etc.Here, we are going to see two examples of option menus. First, the simple option
menus and second, options menus with images.Here, we are inflating the menu by calling
the inflate() method of MenuInflater class. To perform event handling on menu items, you need
to override onOptionsItemSelected() method of Activity class.

Android Option Menu Example:- Let's see how to create menu in android. Let's see the simple
option menu example that contains three menu items.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
tools:context="example.javatpoint.com.optionmenu.MainActivity">

Page 27

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

Output:

Page 28

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Practical-12
Application based on Rating Bar.
Android RatingBar can be used to get the rating from the user. The Rating returns a floating-
point number. It may be 2.0, 3.5, 4.0 etc.Android RatingBar displays the rating in stars. Android
RatingBar is the subclass of AbsSeekBar class.The getRating() method of android RatingBar
class returns the rating number.
Android RatingBar Example:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"

Page 29

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

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"
tools:context="example.javatpoint.com.ratingbar.MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:id="@+id/button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.615" />

<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="72dp"
android:layout_marginTop="60dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Output:

Page 30

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Practical -13
Application based on Media Player.
Android provides many ways to control playback of audio/video files and streams. One of this
way is through a class called MediaPlayer.

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

Page 31

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView android:text="Music Palyer" android:layout_width="wrap_content"


android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="@+id/textView"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="35dp" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"

Page 32

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:src="@drawable/abc"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/forward"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pause"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/back"
android:id="@+id/button3"
android:layout_alignTop="@+id/button2"

Page 33

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

android:layout_toRightOf="@+id/button2"
android:layout_toEndOf="@+id/button2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rewind"
android:id="@+id/button4"
android:layout_alignTop="@+id/button3"
android:layout_toRightOf="@+id/button3"
android:layout_toEndOf="@+id/button3" />

<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
android:layout_alignLeft="@+id/textview"
android:layout_alignStart="@+id/textview"
android:layout_alignRight="@+id/textview"
android:layout_alignEnd="@+id/textview"
android:layout_above="@+id/button" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/textView2"

Page 34

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

android:layout_above="@+id/seekBar"
android:layout_toLeftOf="@+id/textView"
android:layout_toStartOf="@+id/textView" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/textView3"
android:layout_above="@+id/seekBar"
android:layout_alignRight="@+id/button4"
android:layout_alignEnd="@+id/button4" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView4"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_centerHorizontal="true" />

</RelativeLayout>
Following is the content of the res/values/string.xml.
<resources>
<string name="app_name">My Application</string>

Page 35

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

<string name="back"><![CDATA[<]]></string>
<string name="rewind"><![CDATA[<<]]></string>
<string name="forward"><![CDATA[>>]]></string>
<string name="pause">||</string>
</resources>
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity
android:name="com.example.sairamkrishna.myapplication.MainActivity"
android:label="@string/app_name" >

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>

</application>

Page 36

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

</manifest>

Output:

Practical – 14
Application Based on Content Providers.

Page 37

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

A content provider component supplies data from one application to others on request. Such
requests are handled by the methods of the ContentResolver class. A content provider can use
different ways to store its data and the data can be stored in a database, in files, or even over a
network.

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.MyApplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<provider android:name="StudentsProvider"
android:authorities="com.example.MyApplication.StudentsProvider"/>

</application>

</manifest>

Page 38

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Practical – 15

Page 39

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Application Based on accessing Camera.


Step 1:- Open Android Studio.
Go to File >> New >> New Project.
Specify your project name and the location where you want to save your project. Click on Next.
Select the API levels.

The above picture shows that my app's name is My Application, by default. However, you can
change the name to whatever you want.

Step 2:-Drag a button from the Toolbar, change the button name to Camera, and change button ID
to btncamera.
Again, drag the ImageView from Toolbar.
Give the ImageView ID. I have given it as an imageView.
You can put any picture you want to see in the ImageView. For that, copy your image from your
PC and paste it to the "Drawables" folder which you can find in the left corner of the screen. Then,
double click on ImageView and give the source of that image i.e src= @drawable/imagename.

Page 40

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Step 3:-Now, the part remaining is the coding section.


1. Initialize the button and ImageView objects.
2. Ask for permission for using the camera to hardware using intent (Java code).

Step 4 :-Now, we need to execute our camera app. We will do this by using a virtual device
called emulator which is already present in the Android Studio.

Page 41

Downloaded by nope open ([email protected])


lOMoARcPSD|55218029

Now, your camera app is ready and displayed by the emulator.

Now, when you click on camera in the app, your camera will start.

Page 42

Downloaded by nope open ([email protected])

You might also like