AMPFinal
AMPFinal
Practical No: 01
Programming Resources
Aim: Insert the new contents in the following resources and demonstrate their uses in the
android application. (Color, Theme, String, drawable,Image, dimension)
Writeup:
Resources are the additional files and static content that your code uses, such as bitmaps, layout
definitions, user interface strings, animation instructions, and more.
Color Resource: XML file that define a state list of colors. They are saved in res/values/ folder.
You can use a color resource any place that accepts a hexadecimal color value.
(for example, android:drawable = "@color/green").
The value always begins with a pound (#) character and then followed by the Alpha-Red- Green-
Blue information in one of the following formats: #RGB, #ARGB, #RRGGBB, #AARRGGBB
Where ARGB is a color space composed of Alpha (transparency)
Theme Resource: XML file to define theme for different views. They are saved in
res/values/theme folder.
A theme is a collection of attributes that's applied to an entire app, activity, or view hierarchy— not
just an individual view.
String Resource: A string resource provides text strings for your application which are located in
res/values/ folder
It would be best to define the text you want to display in your application in the strings.xml file.
Each item consists of a key-value pair where the key indicates the ID of the text and the value
indicates the text itself.
Drawable and Image Resource: Define various graphics with bitmaps or XML.
Saved in res/drawable/ and accessed from the R.drawable class.
Dimension Resources: XML resource that carries a dimension value with a unit of measure.
The following units of measure are supported by Android:
Dp: Density-independent pixels
Sp: Scale-independent Pixels
Pt: Points(1/72 of an inch based on the physical size of the screen)
Px:Pixels: corresponds to actual pixels on the screen.
To add dimension XML ->
Right click on values folder and add new ->XML->Values XML File.->set name dimensions
Steps:
Step1: open android studio
Step2: open new project
Step3: select empty view activity
Step4: set API level at 30
Build Configuration language: Groovy DSL
Language java
Advanced Mobile Programming A-78
CODE:
Activity_main.xml
<TextView
android:layout_width="@dimen/wid"
android:layout_height="@dimen/ht"
android:background="@color/a4"
android:gravity="center"
android:text="@string/s1"
android:textColor="@color/white"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.602"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.785" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="@dimen/wid1"
android:layout_height="@dimen/ht1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.575"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.216"
app:srcCompat="@drawable/aashu" />
</androidx.constraintlayout.widget.ConstraintLayout>
Dimen.xml
Step: Right click on ”res”. New>XML>value xml file .
Enter name as “dimen”.
Codes:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="wid">400dp</dimen>
<dimen name="ht">100dp</dimen>
<dimen name="wid1">400dp</dimen>
<dimen name="ht1">250dp</dimen>
</resources>
Advanced Mobile Programming A-78
String.xml
<resources>
<string name="app_name">app prac 1</string>
<string name="s1">Android Resource</string>
</resources>
Themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.Appprac1" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your light theme here. -->
<item name="colorPrimary">@color/a3</item>
</style>
Colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="a1">#FF309950</color>
<color name="a2">#FF176060</color>
<color name="a3">#FF209901</color>
<color name="a4">#FF250901</color>
</resources>
Output:
Advanced Mobile Programming A-78
Practical 2
Programming Activities
AIM:
A. Implementing different methods of Activity Life Cycle.
B. Create an android application to pass data from one activity to another activity in
the same application using intents.
In Android, an activity is the entry point for interacting with the user. Every activity has what is known as a
lifecycle. The activity lifecycle consists of the different states that an activity can go through, from when the
activity first initializes to its destruction
Intents: 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
• Starting a service
• Delivering aa broadcast
There are two types of intents:
Explicit intents specify which component of which application will satisfy the intent, by
specifying a full component name. For example, starting other activity or some service.
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, opening webpage or
location map.
Practical 2A
Codes:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Activity Life Cycle"
android:textSize="30sp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.079" />
</androidx.constraintlayout.widget.ConstraintLayout>
Mainactivity.java
package com.example.pra2a;
import android.os.Bundle;
Advanced Mobile Programming A-78
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
Toast.makeText(this,"On create method called",Toast.LENGTH_LONG).show();;
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(this,"On Start method called",Toast.LENGTH_LONG).show();
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(this,"On Stop method called",Toast.LENGTH_LONG).show();
}
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(this,"On Restart method called",Toast.LENGTH_LONG).show();
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this,"On Resume method called",Toast.LENGTH_LONG).show();
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(this,"On Pause method called",Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(this,"On Destroy method called",Toast.LENGTH_LONG).show();
}
}
Advanced Mobile Programming A-78
Output:
Practical 2B
Codes:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="83dp"
android:layout_marginBottom="281dp"
android:gravity="center"
android:text="Activity 1"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
Advanced Mobile Programming A-78
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.133" />
<EditText
android:id="@+id/editText1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginEnd="84dp"
android:layout_marginBottom="169dp"
android:ems="10"
android:gravity="center"
android:hint="Enter Data to send"
android:inputType="text"
android:text="Name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.467" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="209dp"
android:text="Send Data"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
Activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
Advanced Mobile Programming A-78
android:layout_height="77dp"
android:layout_marginTop="171dp"
android:text="TextView"
android:textSize="20sp"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Mainactivity.java
package com.example.prac2b;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
Button b;
EditText t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
b=findViewById(R.id.button);
t=findViewById(R.id.editText1);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) ->
{
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
b.setOnClickListener(v->{
Intent in1 = new Intent(this, MainActivity2.class);
String data = t.getText().toString();
in1.putExtra("mes", data);
startActivity(in1);
});
}
Advanced Mobile Programming A-78
MainActivity2.java
package com.example.prac2b;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main2);
t=findViewById(R.id.textView3);
Output:
Advanced Mobile Programming A-78
Practical 3
Programs on different Layouts
AIM:
A. Create application to play video file using Constraint Layout.
B. Create simple application to play audio file using Relative Layout.
C. Create the standard calculator application using Linear and Grid layout.
An Android layout is a class that handles arranging the way its children appear on the screen. There
are different layouts available like Constraint Layout (Default Layout), Linear, Table, Relative,
Frame, List View etc.
Linear Layout:
Linear Layout is a view group that aligns all children in a single direction, vertically or horizontally.
Relative Layout:
Android RelativeLayout enables you to specify how child views are positioned relative to each other.
The position of each view can be specified as relative to sibling elements or relative to the parent.
Grid Layout:
Android GridLayout is used to display elements and views in the form of a rectangular grid.
ConstraintLayout:
This is default layout for most of the activities, you create large, complex layouts with a flat
view hierarchy—no nested view groups
Practical 3A
Codes:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
tools:context=".MainActivity">
Advanced Mobile Programming A-78
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginStart="67dp"
android:layout_marginTop="93dp"
android:layout_marginEnd="67dp"
android:layout_marginBottom="174dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="256dp"
android:text="Play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/videoView" />
</androidx.constraintlayout.widget.ConstraintLayout>
Main_Activity.java
package com.example.prac3a;
import android.net.Uri;
import android.os.Bundle;
Advanced Mobile Programming A-78
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
Button b;
Uri url=Uri.parse("https://cdn.pixabay.com/video/2021/08/10/84574-586228759_large.mp4");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
vv=findViewById(R.id.videoView);
b=findViewById(R.id.button);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
b.setOnClickListener(v->{
vv.setVideoURI(url);
MediaController m=new MediaController(this);
Advanced Mobile Programming A-78
m.setAnchorView(vv);
vv.setMediaController(m);
vv.start();
});
}
}
AndroidManifest.xml
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Prac3A"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
Advanced Mobile Programming A-78
Practical 3B
Codes:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play Audio"
android:layout_marginTop="100dp"
android:layout_centerHorizontal="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/play"
android:text="Play"
android:layout_marginTop="200dp"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pause"
Advanced Mobile Programming A-78
android:text="Pause"
android:layout_marginTop="300dp"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stop"
android:text="Stop"
android:layout_marginTop="400dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
MainActivity.java
package com.example.prac3b;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
Button bpl,bpa,bst;
int pos = 0;
boolean flag=true;
Advanced Mobile Programming A-78
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
bpl=findViewById(R.id.play);
bpa=findViewById(R.id.pause);
bst=findViewById(R.id.stop);
bst.setOnClickListener(v->{
mp.stop();
flag=true;
});
}
}
Output:
Advanced Mobile Programming A-78
Practical 3C
Codes:
Activity_main.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="20dp"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etDisplay"
android:textSize="32sp"
android:gravity="end"
android:background="@android:color/darker_gray"
android:layout_margin="10dp"
android:padding="10dp"
android:focusable="false"
android:textStyle="bold"/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:padding="10dp">
Advanced Mobile Programming A-78
<Button
android:id="@+id/btn7"
style="@style/Button"
android:text="7"/>
<Button
android:id="@+id/btn8"
style="@style/Button"
android:text="8"/>
<Button
android:id="@+id/btn9"
style="@style/Button"
android:text="9"/>
<Button
android:id="@+id/btnDiv"
style="@style/Button"
android:text="/"/>
<Button
android:id="@+id/btn4"
style="@style/Button"
android:text="4"/>
<Button
android:id="@+id/btn5"
style="@style/Button"
android:text="5"/>
<Button
android:id="@+id/btn6"
style="@style/Button"
android:text="6"/>
Advanced Mobile Programming A-78
<Button
android:id="@+id/btnMul"
style="@style/Button"
android:text="*"/>
<Button
android:id="@+id/btn1"
style="@style/Button"
android:text="1"/>
<Button
android:id="@+id/btn2"
style="@style/Button"
android:text="2"/>
<Button
android:id="@+id/btn3"
style="@style/Button"
android:text="3"/>
<Button
android:id="@+id/btnSub"
style="@style/Button"
android:text="-"/>
<Button
android:id="@+id/btnClear"
style="@style/Button"
android:text="C"/>
<Button
android:id="@+id/btn0"
style="@style/Button"
android:text="0"/>
<Button
android:id="@+id/btnDec"
Advanced Mobile Programming A-78
style="@style/Button"
android:text="."/>
<Button
android:id="@+id/btnAdd"
style="@style/Button"
android:text="+"/>
</GridLayout>
<Button
android:layout_width="match_parent"
android:id="@+id/btnEqual"
style="@style/Button"
android:text="="/>
</LinearLayout>
Style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Button">
<item name="android:layout_width">50dp</item>
<item name="android:layout_height">80dp</item>
<item name="android:layout_margin">5dp</item>
<item name="android:layout_columnWeight">1</item>
<item name="backgroundTint">#FF9438</item>
<item name="android:textSize">24sp</item>
</style>
</resources>
Advanced Mobile Programming A-78
Build.gradle
Build.gradle(:app)
plugins {
alias(libs.plugins.android.application)
}
android {
namespace 'com.example.pract3c'
compileSdk 35
defaultConfig {
applicationId "com.example.pract3c"
minSdk 30
targetSdk 35
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
Advanced Mobile Programming A-78
}
}
dependencies {
implementation libs.rhino
implementation libs.appcompat
implementation libs.material
MainActivity.java
package com.example.pract3c;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
@Override
Advanced Mobile Programming A-78
{
exp += value;
}
etdisp.setText(exp);
});
}
}
Output:
Practical 4
Advanced Mobile Programming A-78
AIM:
1. Create an android application using linear layout and insert 10 games in the list view and
display the selected game in the text view.
List View
Android ListView is a view which groups several items and display them in vertical scrollable list.
The list items are automatically inserted to the list using an Adapter that pulls content from a source
such as an array or database. An adapter actually bridges between UI components and the data
source that fill data into UI Component.
Android provides several subclasses of Adapter that are useful for retrieving different kinds of data
and building views for an AdapterView ( i.e. ListView or GridView). The common adapters are
ArrayAdapter, BaseAdapter, CursorAdapter, SimpleCursorAdapter etc.
ArrayAdapter
You can use this adapter when your data source is an array. By default, ArrayAdapter creates a
view for each array item by calling toString() on each item and placing the contents in a TextView.
Syntax:
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.ListView,StringArray);
Practical 4
Codes:
Activity_main.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="20dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/LV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#f00"
Advanced Mobile Programming A-78
android:dividerHeight="1dp"/>
<TextView
android:id="@+id/TV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="center"
android:layout_marginTop="100dp"
/>
</LinearLayout>
MainActivity.java
package com.example.pract4;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
ListView lv;
TextView tv;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
lv=findViewById(R.id.LV);
tv=findViewById(R.id.TV);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
String[] games = {"Carrom", "Ludo", "Snakes and Ladders", "Call Of Duty", "Roblox",
"Uno"};
ArrayAdapter<String> adapter=new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1,games);
lv.setAdapter(adapter);
Advanced Mobile Programming A-78
Output:
Advanced Mobile Programming A-78
Practical No: 05
Using Menus, Notification and Alert
Aim
a) Create an android application to display Alert Dialog on pressing the Back key.
b) Create an android application to generate two notifications, one notification will be
non-clickable and another is clickable (it will reload the current application)
c) Create Android application for option menu.
Notification:
A notification is a message you can display to the user outside of your application's normal UI.
Notifications appear in the phone's notification area. Creating a Notification Channel
Clickable Notification:
A clickable notification in Android allows users to tap on the notification to trigger an action,
such as opening an activity, a webpage, or executing a background task.
In Android, notifications are managed using the Notification API, and they typically use an
Intent to handle user interactions. Along with all above content it requires:
Pending Intent – A wrapper for Intent that allows it to be executed when the user interacts with
the notification.
Alert Dialog:
An AlertDialog is a modal dialog box in Android that presents important information or options
to the user. It is commonly used for displaying warnings, confirmations, or prompts that require
user interaction.
Components of an AlertDialog
An AlertDialog typically consists of the following parts:
Menus in Android
A menu in Android provides a way to present options, actions, or additional functionalities to the
user. Menus are essential for enhancing the user experience by organizing actions in a structured
way.
• Options Menu: The primary menu for an activity, appears in the app bar (toolbar) or as a
three-dot overflow menu. This menu is used for global actions like Settings, Search, etc.
2. Context Menu : This menu is associated with a specific UI element and appears when the
user long-presses on a view.
Practical 5A
Codes:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Press back to get Alert"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Advanced Mobile Programming A-78
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.pract5a;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
getOnBackPressedDispatcher().addCallback(
this, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Exit App");
builder.setMessage("Are you sure you want to exit?");
builder.setIcon(R.drawable.sharp_bomb_24);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.show();
}
}
);
}}
Advanced Mobile Programming A-78
Output:
Practical 5B
Codes:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="109dp"
android:text="Non Clickable Notification"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="313dp"
android:text="Clickable Notification"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Advanced Mobile Programming A-78
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.pract5b;
import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
Button b1,b2;
String ID="1001";
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
b1=findViewById(R.id.button);
b2=findViewById(R.id.button2);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
b1.setOnClickListener(v->{
NotificationManager nm = getSystemService(NotificationManager.class);
NotificationChannel ch = new NotificationChannel(ID,"My
Channel",NotificationManager.IMPORTANCE_HIGH);
nm.createNotificationChannel(ch);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, ID)
.setSmallIcon(android.R.drawable.star_big_on)
.setContentTitle("My Non-Clickable Notification")
.setContentText("Hello!!! This is Non-Clickable Notification..");
nm.notify(1, builder.build());
});
b2.setOnClickListener(v->{
NotificationManager nm = getSystemService(NotificationManager.class);
NotificationChannel ch = new NotificationChannel(ID, "My Channel",
NotificationManager.IMPORTANCE_HIGH);
nm.createNotificationChannel(ch);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(
this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT |
PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, ID)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setLargeIcon(BitmapFactory. decodeResource(
getResources(),R.drawable.img))
.setContentTitle("Clickable Notification")
.setContentText("Tap to open the APP!")
.setContentIntent(pi)
.setAutoCancel(true);
nm.notify(2,builder.build());
});
}
}
Advanced Mobile Programming A-78
Output:
Advanced Mobile Programming A-78
Practical 5C
Codes:
mymenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/set"
android:title="Settings"
android:icon="@android:drawable/arrow_down_float"
app:showAsAction="always">
<menu>
<item
android:id="@+id/ser"
android:title="Search"
android:icon="@android:drawable/ic_menu_search"
/>
<item
android:id="@+id/email"
android:title="Email"
android:icon="@android:drawable/ic_dialog_email"
android:enabled="false"/>
<item
android:id="@+id/share"
android:title="Share"
android:icon="@android:drawable/ic_menu_share" />
</menu>
</item>
</menu>
Themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.Pract5c" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your light theme here. -->
<!-- <item name="colorPrimary">@color/my_light_primary</item> -->
</style>
<style name="Theme.Pract5c" parent="Base.Theme.Pract5c" />
</resources>
MainActivity.java
package com.example.pract5c;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
Advanced Mobile Programming A-78
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id== R.id.ser){
Toast.makeText(this, "Search Clicked", Toast.LENGTH_SHORT).show();
return true;
} else if (id== R.id.email) {
return true;
}else if (id== R.id.share) {
Toast.makeText(this, "Share Clicked", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Output:
Advanced Mobile Programming A-78
Practical 6
TITLE: Program using Services and Broadcast
AIM:
a) Create the background service in android application to play the ringtone/music
b) Create an android application which automatically notify the user when Aeroplane
mode is turned on or off using broadcast receiver.
Service: A service is a component that runs in the background to perform long-running operations
without needing to interact with the user and it works even if application is destroyed. The prime aim
of a service is to ensure that the application remains active in the background so that the user can
operate multiple applications at the same time.
Types of Android Services
Broadcast: Broadcast in android is the system-wide events that can occur when the device starts,
when a message is received on the device or when incoming calls are received, or when a device goes
to airplane mode, etc. There are mainly two types of Broadcast Receivers:
Static Broadcast Receivers: These types of Receivers are declared in the manifest file and works
even if the app is closed.
Dynamic Broadcast Receivers: These types of receivers work only if the app is active or minimized.
Practical 6A
Codes:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
tools:context=".MainActivity">
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="131dp"
android:text="Start Service"
Advanced Mobile Programming A-78
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="114dp"
android:text="Stop Service"
app:layout_constraintStart_toStartOf="@+id/start"
app:layout_constraintTop_toBottomOf="@+id/start" />
</androidx.constraintlayout.widget.ConstraintLayout>
Myservice.java
package com.example.pract6a;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.provider.Settings;
import android.widget.Toast;
import androidx.annotation.Nullable;
public class MyService extends Service {
MediaPlayer mp;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mp = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
mp.start();
Toast.makeText(this, "Music Service Started",Toast.LENGTH_LONG);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
mp.stop();
super.onDestroy();
}
}
MainActivity.java
package com.example.pract6s;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
Advanced Mobile Programming A-78
b1=findViewById(R.id.start);
b2=findViewById(R.id.stop);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
b1.setOnClickListener(v->{
startService(new Intent( this, MyService.class));
});
b2.setOnClickListener(v->{
stopService(new Intent(this,MyService.class));
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Pract6s"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"
android:enabled="true"/>
</application>
</manifest>
Output:
Advanced Mobile Programming A-78
Practical 6B
Codes:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testing Broadcasting!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.pract6b;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
MyRec mr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
mr = new MyRec();
IntentFilter f=new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
this.registerReceiver(mr, f);
}
@Override
protected void onDestroy() {
super.onDestroy();
this.unregisterReceiver(mr);
}
}
MyRec.java
package com.example.pract6b;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.widget.Toast;
Advanced Mobile Programming A-78
Practical 7
TITLE: Program Using Different Events
Advanced Mobile Programming A-78
AIM:
Create an android application to demonstrate the following event listeners on the
Button: on click listener, long click listener
Image: Touch listener with these motion events =>Action up, action down and action move
Edit Text: key listener
The Appropriate message should be displayed in text view.
Events are a useful way to collect data about a user's interaction with interactive components of
Applications. Like button presses or screen touch etc.
OnClickListener(): This is called when the user either clicks or touches or focuses upon any
widget like button, text, image etc.
button.setOnClickListener(new View.OnClickListener()
{ @Override public void onClick(View v) { //Code
} });
OnLongClickListener(): This is called when the user either clicks or touches or focuses upon
any widget like button, text, image etc. for one or more seconds.
button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//Code
return true; // return true to indicate the event is consumed
}
});
imageView.setOnTouchListener(new View.OnTouchListener()
{ @Override
public boolean onTouch(View v, MotionEvent event)
{ //Code
return true;
}
});
Practical 7
Codes:
Activity_main.xml
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="63dp"
android:layout_marginTop="55dp"
android:layout_marginEnd="63dp"
android:layout_marginBottom="322dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:srcCompat="@drawable/img" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="72dp"
Advanced Mobile Programming A-78
android:layout_marginTop="317dp"
android:layout_marginBottom="314dp"
android:hint="Enter your name"
android:text="Name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/imageView"
app:layout_constraintVertical_bias="0.127" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:layout_marginBottom="10dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="46dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.pract7;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
ImageView iv;
EditText et;
Advanced Mobile Programming A-78
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
b=findViewById(R.id.button);
tv=findViewById(R.id.textView);
iv=findViewById(R.id.imageView);
et=findViewById(R.id.editText);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
b.setOnClickListener(v->{
tv.setText("Button Clicked");
});
b.setOnLongClickListener(v->{
tv.setText("You long Button clicked ");
return true;
});
et.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int KeyCode, KeyEvent event) {
if (KeyCode==KeyEvent.KEYCODE_ENTER)
tv.setText("Key Pressed");
return false;
}
});
iv.setOnTouchListener((v,event)->{
switch (event.getAction())
{
case (MotionEvent.ACTION_DOWN):
tv.setText("Action Down");break;
case (MotionEvent.ACTION_UP):
tv.setText("Action Up");break;
case (MotionEvent.ACTION_POINTER_DOWN):
tv.setText("Action Move");break;
}
return true;
});
}
}
Advanced Mobile Programming A-78
Output:
Advanced Mobile Programming A-78
Practical No: 08
Database Programming with SQLite
Aim:
Create an android application using sqlite database to manage the students data by using
insert, update and delete operations and display the data in text view.
SQLite is a lightweight, self-contained, and serverless database engine used for local data storage in
Android applications. It follows the ACID (Atomicity, Consistency, Isolation, Durability) properties
and uses SQL syntax for database operations.
Feature of SQLite:
Local Storage: Useful for apps that need to store structured data on the device.
Lightweight: No separate server required; it runs directly from application memory.
Fast & Reliable: Optimized for mobile applications with low memory and power consumption.
Standard SQL Support: Supports SQL queries for data manipulation.
SQLiteOpenHelper class
The android.database.sqlite.SQLiteOpenHelper class is used for database creation and version
management. For performing any database operation, you have to provide the implementation of
onCreate() and onUpgrade() methods of SQLiteOpenHelper class.
Methods:
1. public void onCreate(SQLiteDatabase db)
This method is called when the database is created for the first time.
D.public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
This method is called when the database needs to be upgraded.
E.public void execSQL (String sql, Object[] bindArgs)
Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.
Practical 8
Codes:
Activity_main.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="20dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="SQLite Demo"
android:layout_gravity="center"
android:textSize="30sp"
/>
<EditText
android:id="@+id/sid"
android:inputType="number"
android:hint="Auto generated ID"
android:textSize="15dp"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Advanced Mobile Programming A-78
android:layout_marginTop="30dp"
/>
<EditText
android:id="@+id/name"
android:inputType="text"
android:hint="Enter Name"
android:textSize="15dp"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
/>
<EditText
android:id="@+id/marks"
android:inputType="number"
android:hint="Enter Marks"
android:textSize="15dp"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
/>
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:columnCount="2">
<Button
android:id="@+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<Button
android:id="@+id/btnupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"/>
<Button
android:id="@+id/btndel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"/>
<Button
android:id="@+id/btnshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ShowAll"/>
</GridLayout>
<TextView
android:id="@+id/tvshow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
/>
</LinearLayout>
MyData.java
package com.example.pract8;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MyData extends SQLiteOpenHelper {
public static final String TNAME = "stud";
public static final String COL1 = "ID";
Advanced Mobile Programming A-78
}
MainActivity.java
package com.example.pract8;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
Advanced Mobile Programming A-78
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
MyData myDb;
EditText editName,editMarks ,editid;
Button btnAdd, btnDel,btnUpdate,btnView;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
myDb = new MyData(this);
editName = findViewById(R.id.name);
editid = findViewById(R.id.sid);
editMarks = findViewById(R.id.marks);
tv1=findViewById(R.id.tvshow);
btnAdd = findViewById(R.id.btnadd);
btnView = findViewById(R.id.btnshow);
btnUpdate = findViewById(R.id.btnupdate);
btnDel= findViewById(R.id.btndel);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
btnAdd.setOnClickListener(v->{
boolean ins = myDb.insertData(editName.getText().toString(),
editMarks.getText().toString() );
if(ins == true)
Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
});
btnUpdate.setOnClickListener(v->{
boolean upd = myDb.updateData(editid.getText().toString(),
editName.getText().toString(), editMarks.getText().toString());
if(upd == true)
Toast.makeText(MainActivity.this,"Data Updated",Toast.LENGTH_LONG).show();
});
btnView.setOnClickListener(v->{
String data=myDb.getAllData();
tv1.setText(data);
});
btnDel.setOnClickListener(v->{
String id = editid.getText().toString();
int r = myDb.deleteData(id);
if(r>0)
Toast.makeText(getApplicationContext(),"Deleted", Toast.LENGTH_LONG).show();
});
}
}
Advanced Mobile Programming A-78
Output: