[go: up one dir, main page]

0% found this document useful (0 votes)
8 views53 pages

MAD Lab Programs (2025)

The document outlines a series of mobile application lab programs, including creating basic applications such as a 'Hello World' app, handling screen orientation changes, implementing login functionality, and utilizing explicit and implicit intents. It provides detailed XML layouts and Java code snippets for each program, demonstrating various UI components and functionalities in Android development. The programs also cover advanced topics like data storage with SQLite, sending SMS and emails, and displaying maps based on location.

Uploaded by

thanish.mv009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views53 pages

MAD Lab Programs (2025)

The document outlines a series of mobile application lab programs, including creating basic applications such as a 'Hello World' app, handling screen orientation changes, implementing login functionality, and utilizing explicit and implicit intents. It provides detailed XML layouts and Java code snippets for each program, demonstrating various UI components and functionalities in Android development. The programs also cover advanced topics like data storage with SQLite, sending SMS and emails, and displaying maps based on location.

Uploaded by

thanish.mv009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

MOBILE APPLICATION LAB PROGRAMS

1. Creating “Hello world” Application.


2. Creating an application that displays message based on the screen orientation.
3. Create an application to develop Login window using UI controls.
4. Create an application to implement new activity using explicit intent and implicit
intent.
5. Create an application that displays custom designed Opening Screen.
6. Create an UI with all views.
7. Create menu in Application
8. Read/ write the Local data.
9. Create / Read / Write data with database (SQLite).
10. Create an application to send SMS and receive SMS
11. Create an application to send an email.
12. Display Map based on the Current/given location.
13. Create a sample application with login module (check user name and password)
On successful login change Textview “Login Successful”. On login fail alert using
Toast
“login fail”.
14. Learn to deploy Android applications
Program 1
Creating “Hello world” Application.

activity_main.xml
<?xml version="1.0" encoding="utf8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/resauto"
xmlns:tools="http://schemas.android.com/tools"
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="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textSize="30sp"/>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.bca.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Program 2
Creating an application that displays message based on the screen
orientation.

AndroidManifest.xml
<?xml version="1.0" encoding="utf8"?>
<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.ScreenOrientation"
tools:targetApi="31">
<activity
android:name=".MainActivity2"
android:exported="false"
android:screenOrientation="landscape"/>
<activity
android:name=".MainActivity"
android:exported="true"
android:screenOrientation="portrait">
<intentfilter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intentfilter>
</activity>
</application>
</manifest>

activity_main.xml
<?xml version="1.0" encoding="utf8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/resauto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="112dp"
android:onClick="onClick"
android:text="Launch next activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.612"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText1"
app:layout_constraintVertical_bias="0.613" />

<TextView
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="124dp"
android:ems="10"
android:textSize="22dp"
android:text="This activity is portrait orientation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.bca.screenorientation;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
}
}

activity_main2.xml
<?xml version="1.0" encoding="utf8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/resauto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="180dp"
android:text="this is landscape orientation"
android:textSize="22dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity2.java
package com.bca.screenorientation;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
Program 4
Create an application to implement new activity using explicit intent and
implicit intent.

activity_main.xml
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">

<Button
android:id="@+id/explicit_intent_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Go to Second Activity" />

<Button
android:id="@+id/implicit_intent_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Google" />

</LinearLayout>

new_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/message_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:text="Message will appear here" />
</LinearLayout>

Mainactivity.java
package com.example.loginpage;

import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button explicitIntentButton = findViewById(R.id.explicit_intent_button);


Button implicitIntentButton = findViewById(R.id.implicit_intent_button);

explicitIntentButton.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("message", "Hello from MainActivity");
startActivity(intent);
});

implicitIntentButton.setOnClickListener(view -> {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.meesho.com"));
startActivity(intent);
});
}
}

SecondActivity.java
package com.example.loginpage;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_activity);

TextView messageTextView = findViewById(R.id.message_textview);


String message = getIntent().getStringExtra("message");
messageTextView.setText(message);
}
}
Program 5
Create an application that displays custom designed Opening Screen.

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"
tools:context=".MainActivity">

<TextView
android:id="@+id/welcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:text="Welcome to the Main Screen!"
android:textColor="#616161"
android:textSize="24sp"
android:textStyle="bold" />

</LinearLayout>

MainActivity.java
package com.example.splashscreen;
import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/logo"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/img"
android:layout_centerInParent="true"
android:contentDescription="Logo" />

<TextView
android:id="@+id/appName"
android:layout_below="@id/logo"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:text="My Splash App"
android:textColor="@color/black"
android:textSize="24sp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>

</RelativeLayout>

SplashActivity.java
package com.example.splashscreen;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;

@SuppressLint("CustomSplashScreen")
public class SplashActivity extends Activity {
private static final int SPLASH_SCREEN_TIME_OUT = 2000; // After completion
of 2000 ms, the next activity will get started.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// This method is used so that your splash activity can cover the entire screen.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,Windo
wManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.activity_splash);

new Handler().postDelayed(() -> {


Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
finish();
}, SPLASH_SCREEN_TIME_OUT);
}
}

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="@drawable/img"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SplashScreen"
tools:targetApi="31">
<activity
android:name=". SplashActivity "
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
<activity android:name=".MainActivity"/>
</application>

</manifest>
Program 6
Create an UI with all views.

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:orientation="vertical"
tools:context=".MainActivity">

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Logo -->

<ImageView
android:src="@drawable/img"
android:layout_gravity="center"
android:layout_marginTop="45dp"
android:layout_width="80dp"
android:layout_height="80dp" />
<!-- Name input -->
<TextView
android:text="Name:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:hint="Enter your name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Feedback -->

<TextView
android:text="Your Feedback:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:hint="Write here..."
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Subscribe checkbox -->

<CheckBox
android:text="Subscribe to updates"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- Satisfaction radio group -->

<TextView
android:text="Satisfaction:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<RadioGroup
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RadioButton
android:text="Low"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<RadioButton
android:text="Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<RadioButton
android:text="High"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<!-- Star rating -->
<TextView
android:text="Rate us:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<RatingBar
android:numStars="5"
android:stepSize="1.0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- Topic spinner -->

<TextView
android:text="Feedback Topic:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Spinner
android:id="@+id/spinner_topic"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Switch -->

<TextView
android:text="Notifications:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Switch
android:text="Enable Notifications"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- SeekBar -->

<TextView
android:text="Volume Preference:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- CalendarView -->

<TextView
android:text="Preferred Contact Date:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CalendarView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Simulated Progress -->

<TextView
android:text="Processing..."
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:progress="70"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Submit -->

<Button
android:text="Submit Feedback"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Program 7
Create menu in Application

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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:text="Welcome to the Menu App"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

MainActivity.java
package com.example.menu;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Call createMenu() instead of inflating from XML
@Override
public boolean onCreateOptionsMenu(Menu menu) {
createMenu(menu);
return true;
}
// Custom method to create menu items programmatically
private void createMenu(Menu menu) {
menu.add(0, 1, Menu.NONE, "Settings");
menu.add(0, 2, Menu.NONE, "About");
}
// Handle clicks on menu items
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Toast.makeText(this, "Settings clicked", Toast.LENGTH_SHORT).show();
return true;
case 2:
Toast.makeText(this, "About clicked", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Menu" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
</resources>
Program 8
Read/ write the Local data

activity_file_storage.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"
tools:context=".fileStorage">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:padding="5dp"
android:text="@string/file"
android:textStyle="bold"
android:textSize="28sp" />

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_margin="5dp"
android:hint="@string/enter_a_data"
android:inputType="text"
android:minHeight="48dp"
android:minLines="5"/>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/write_text_into_file"
android:onClick="WriteBtn"
android:layout_alignTop="@+id/button2"
android:layout_alignEnd="@+id/editText1" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read_text_from_file"
android:onClick="ReadBtn"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/editText1" />
</RelativeLayout>

Mainactivity.java
package com.example.sharedpreference;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import androidx.appcompat.app.AppCompatActivity;
public class fileStorage extends AppCompatActivity {
EditText textmsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_storage);
textmsg= findViewById(R.id.editText1);
}
// write text to file
public void WriteBtn(View v) {
// add-write text into file
try {
FileOutputStream fileout=openFileOutput("mytextfile.txt", MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(textmsg.getText().toString());
outputWriter.close();
//display file saved message
Toast.makeText(getBaseContext(), "File saved successfully!",
Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
// Read text from file
public void ReadBtn(View v) {
//reading text from file
try {
FileInputStream fileIn=openFileInput("mytextfile.txt");
InputStreamReader isr = new InputStreamReader(fileIn);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null)
{
sb.append(line);
}
fileIn.close();
String fileData = sb.toString();
textmsg.setText(fileData);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Program 9
Create / Read / Write data with database (SQL Lite)

activity_db.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<EditText
android:id="@+id/nameInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Enter name to update"
android:importantForAutofill="no"
android:inputType="textPersonName"
android:textColorHint="#546E7A"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/ageInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:hint="Enter new age"
android:importantForAutofill="no"
android:inputType="number"
android:minHeight="48dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/nameInput" />

<Button
android:id="@+id/updateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update Age"
app:layout_constraintTop_toBottomOf="@id/ageInput"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="12dp"/>
<TextView
android:id="@+id/outputText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginTop="20dp"
app:layout_constraintTop_toBottomOf="@id/updateButton"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Database.java
package com.example.database2;

import android.widget.TextView;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;

/** */
public class database extends AppCompatActivity {

private StudentDatabaseHelper db;


private TextView outputText;
private EditText nameInput, ageInput;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_db);

db = new StudentDatabaseHelper(this);

// Insert a sample record if table is empty


db.insertStudent("Kumar", 25);

outputText = findViewById(R.id.outputText);
nameInput = findViewById(R.id.nameInput);
ageInput = findViewById(R.id.ageInput);
Button updateButton = findViewById(R.id.updateButton);
displayData();

updateButton.setOnClickListener(view -> {
String name = nameInput.getText().toString().trim();
String ageStr = ageInput.getText().toString().trim();

if (name.isEmpty() || ageStr.isEmpty()) {
Toast.makeText(database.this, "Please enter name and age",
Toast.LENGTH_SHORT).show();
return;
}

int newAge = Integer.parseInt(ageStr);


db.updateStudent(name, newAge);
Toast.makeText(database.this, "Updated age for " + name,
Toast.LENGTH_SHORT).show();

displayData();
});
}

private void displayData() {


String result = db.getAllStudentsAsString();
outputText.setText(result);
}

@Override
protected void onDestroy() {
super.onDestroy();
if (db != null) db.close();
}
}

StudentDatabaseHelper.java
package com.example.database2;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.ContentValues;
import android.database.Cursor;

public class StudentDatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "students.db";


private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "students";

public StudentDatabaseHelper(Context context) {


super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT NOT NULL, " +
"age INTEGER NOT NULL)";
db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

public void insertStudent(String name, int age) {


try (SQLiteDatabase db = getWritableDatabase()) {
ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
db.insert(TABLE_NAME, null, values);
}
}

public void updateStudent(String name, int newAge) {


try (SQLiteDatabase db = getWritableDatabase()) {
ContentValues values = new ContentValues();
values.put("age", newAge);
db.update(TABLE_NAME, values, "name = ?", new String[]{name});
}
}

public String getAllStudentsAsString() {


StringBuilder result = new StringBuilder();
try (SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null)) {

while (cursor.moveToNext()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
int age = cursor.getInt(2);
result.append("ID: ").append(id)
.append(", Name: ").append(name)
.append(", Age: ").append(age)
.append("\n");
}
}
return result.toString();
}

}
Program 10
Create an application to send SMS and receive SMS

activity_sms.xml
<?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">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:orientation="horizontal">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Number:"
android:width="165sp"
android:textSize="18sp" />

<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="Enter number"
android:inputType="phone"
tools:ignore="TouchTargetSizeCheck" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:orientation="horizontal">

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message to be Sent:"
android:width="165sp"
android:textSize="18sp" />

<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:gravity="top|start"
android:hint="Enter message"
android:inputType="textMultiLine"
tools:ignore="TouchTargetSizeCheck" />
</LinearLayout>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="32dp"
android:text="SEND" />
</LinearLayout>

Mainactivity.java
import android.os.Bundle;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText txtMobile;
private EditText txtMessage;
private Button btnSms;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtMobile = (EditText)findViewById(R.id.mblTxt);
txtMessage = (EditText)findViewById(R.id.msgTxt);
btnSms = (Button)findViewById(R.id.btnSend);
btnSms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
// Creating an Intent with ACTION_SENDTO
Intent intent = new Intent(Intent.ACTION_SENDTO);
// Setting the data URI to "smsto:" followed by the mobile number
intent.setData(Uri.parse("smsto:" + txtMobile.getText().toString()));
// Adding the message text as an extra
intent.putExtra("sms_body", txtMessage.getText().toString());
// Starting the SMS app activity
startActivity(intent);
}
catch (Exception e){
Toast.makeText(MainActivity.this, "SMS Failed to Send, please try again",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Program 11
Create an application to send an Email.

activity_email.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"
tools:context=".EmailActivity">

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY OWN APPLICATION "
android:textColor="@color/black"
android:textSize="30sp"
android:layout_marginTop="50dp"
android:layout_marginStart="50dp" />

<Button
android:id="@+id/sendEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="25dp"
android:layout_marginTop="50dp"
android:text="Compose email"/>
</LinearLayout>

EmailActivity.java
import android.os.Bundle;
import android.net.Uri;
import android.content.Intent;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
public class EmailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_email);
Button startBtn = findViewById(R.id.sendEmail);
startBtn.setOnClickListener(view -> sendEmail());
}
protected void sendEmail() {
Log.i("Send email", "");
String[] TO = {""};
String[] CC = {""};
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Log.i("Finished sending email...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(EmailActivity.this, "There is no email client installed.",
Toast.LENGTH_SHORT).show();
}
}
}
Program 12
Display Map based on the Current/given location.

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"
tools:context=".MainActivity">

<Button
android:id = "@+id/button"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:text = "Get Location"/>
</LinearLayout>

MainActivity.java
package com.example.mymap;

import android.os.Bundle;
import android.Manifest;
import android.os.Handler;
import android.os.Looper;
import android.widget.Button;
import android.widget.Toast;
import android.location.Address;
import android.location.Geocoder;
import java.util.List;
import java.util.Locale;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


Button btnShowLocation;
private static final int REQUEST_CODE_PERMISSION = 2;
String mPermission = Manifest.permission.ACCESS_FINE_LOCATION;

// GPSTracker class
GPSTracker gps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
if (ActivityCompat.checkSelfPermission(this, mPermission)
!= PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(this, new String[]{mPermission},


REQUEST_CODE_PERMISSION);

// If any permission above not allowed by user, this condition will


//execute every time, else your else part will work
}
}
catch (Exception e)
{
e.printStackTrace();
}

btnShowLocation = findViewById(R.id.button);

// show location button click event


btnShowLocation.setOnClickListener(arg0 -> {
gps = new GPSTracker(MainActivity.this);

if (gps.canGetLocation()) {
Toast.makeText(getApplicationContext(), "Fetching location...",
Toast.LENGTH_SHORT).show();

new Handler(Looper.getMainLooper()).postDelayed(() -> {


double latitude = gps.getLatitude();
double longitude = gps.getLongitude();

Geocoder geocoder = new Geocoder(MainActivity.this,


Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude,
longitude, 1);
if (addresses != null && !addresses.isEmpty()) {
Address address = addresses.get(0);
String city = address.getLocality(); // e.g., "Bangalore"
String state = address.getAdminArea(); // e.g., "Karnataka"
String country = address.getCountryName(); // e.g., "India"

String fullAddress = city + ", " + state + ", " + country;

Toast.makeText(getApplicationContext(), "You are in: " +


fullAddress,
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Address not found",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Geocoder failed",
Toast.LENGTH_LONG).show();
}

}, 3000); // Wait for GPS update


} else {
gps.showSettingsAlert();
}
});

}
}

GPSTracker.java
package com.example.mymap;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import androidx.core.app.ActivityCompat;

public class GPSTracker extends Service implements LocationListener {


private final Context mContext;

// flag for GPS status


boolean isGPSEnabled = false;

// flag for network status


boolean isNetworkEnabled = false;

// flag for GPS status


boolean canGetLocation = false;

Location location; // location


double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters


private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10
meters

// The minimum time between updates in milliseconds


private static final long MIN_TIME_BW_UPDATES = 1000 * 60; // 1 minute

// Declaring a Location Manager


protected LocationManager locationManager;

public GPSTracker(Context context) {


this.mContext = context;
getLocation();
}

public void getLocation() {


try {
locationManager = (LocationManager)
mContext.getSystemService(LOCATION_SERVICE);

// getting GPS status


isGPSEnabled =
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

// getting network status


isNetworkEnabled =
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled)
{
// no network provider is enabled
}
else
{
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
if (ActivityCompat.checkSelfPermission(mContext,
android.Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(mContext,
android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// Request the permission
// You can add a method to request permission here
return;
}
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

Log.d("Network", "Network");
if (locationManager != null) {
location =
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}

// if GPS Enabled get lat/long using GPS Services


if (isGPSEnabled) {
if (location == null) {
assert locationManager != null;
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null)
{
location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDE
R);

if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}

} catch (Exception e) {
e.printStackTrace();
}

/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
*/
public void stopUsingGPS() {
if (locationManager != null) {
if (ActivityCompat.checkSelfPermission(mContext,
android.Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(mContext,
android.Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
// Request the permission
// You can add a method to request permission here
return;
}
locationManager.removeUpdates(GPSTracker.this);
}
}

/**
* Function to get latitude
*/
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}

// return latitude
return latitude;
}

/**
* Function to get longitude
*/
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}

// return longitude
return longitude;
}

/**
* Function to check GPS/wifi enabled
* @return boolean
*/
public boolean canGetLocation() {
return this.canGetLocation;
}

/**
* Function to show settings alert dialog
* On pressing Settings button will launch Settings Options
*/
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

// Setting Dialog Title


alertDialog.setTitle("GPS is settings");

// Setting Dialog Message


alertDialog.setMessage("GPS is not enabled. Do you want to go to settings
menu?");

// On pressing Settings button


alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
Intent intent = new
Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});

// on pressing cancel button


alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

// Showing Alert Message


alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {
if (location != null) {
this.location = location;
this.latitude = location.getLatitude();
this.longitude = location.getLongitude();
Log.d("GPS", "Location updated: " + latitude + ", " + longitude);
}
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
Program 13
Create a sample application with login module (check user name and
password) On successful login change Textview “Login Successful”. On login
fail alert using Toast “login fail”

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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Login Form"
android:layout_gravity="center"/>

<TextView
android:id="@+id/tvUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="User Name" />

<EditText
android:id="@+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text"
android:padding="8dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="30dp"/>

<TextView
android:id="@+id/tvPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Password" />

<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:padding="8dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="30dp"/>
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="18sp"
android:layout_marginTop="16dp"/>

<TextView
android:id="@+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Password" />
</LinearLayout>

Mainactivity.java
package com.bca.loginprgrm;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText etUsername,etPassword; Button btnLogin;
TextView tvMessage;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogin = (Button) findViewById(R.id.btnLogin);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
tvMessage = (TextView) findViewById(R.id.tvMessage);
btnLogin.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View view)
{
if(etUsername.getText().toString().isEmpty())
{
etUsername.setError("Enter User name");
} else if (etPassword.getText().toString().isEmpty()) {
etPassword.setError("Enter Password");
}
else if(etUsername.getText().toString().equals("bca") &&
etPassword.getText().toString().equals("bca"))
{
tvMessage.setText("Valid Login");
}
else
{
tvMessage.setText("Invalid login");
}
}
});
}
}
Program 14
Learn to deploy Android applications

Steps to Deploy an Android Application


1. Prepare App (use Program 1 Hello world for this program)
• Optimize performance and test thoroughly.
• Ensure compatibility with various devices.

2. Generate Signed APK (Android Package Kit):


• In Android Studio, navigate to Build > Generate Signed Bundle/APK.
• Follow the prompts to create a new keystore or use an existing one. A
keystore is a
binary file that contains a set of private keys.
• Configure the build type (release) and signing configuration.
• Generate the signed APK file.

3. Test Your Signed APK:


• Before distributing your app, test the signed APK to ensure that the signing
process
didn't introduce any issues.
• Install the APK on various devices and perform thorough testing.
• Release on Google Play Console:
• Sign in to the Google Play Console (https://play.google.com/apps/publish).
• Create a new app entry if this is your first release or select an existing app.
• Complete all the required information for the app listing, including the title,
description,
screenshots, and categorization.
• Upload your signed APK file.
• Set pricing and distribution options.
• Optimize your store listing for search and conversion.
• Once everything is set, click the "Publish" button to release your app to the
Google Play
Store.
4. Other Distribution Channels (Optional):
• Besides Google Play, you can distribute your app through other channels
such as
Amazon Appstore, Samsung Galaxy Store, or thirdparty app marketplaces.
• Each distribution channel may have its own requirements and submission
process, so
be sure to follow their guidelines.
5. Monitor and Update:
• Keep an eye on user feedback and app performance metrics through the
Google Play
Console.
• Regularly update your app to fix bugs, add new features, and improve user
experience
based on feedback.

You might also like