Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views35 pages

Moj Lo BC

BC

Uploaded by

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

Moj Lo BC

BC

Uploaded by

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

NAME : SHIVAM NEGI UNIVERSITY ROLL.

NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB SUBJECT CODE : PBC 501
Problem Statement 01: Create an android application to step-by-step create a

database . JAVA CODE :

package com.example.databaseapp;

import
android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
import
android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends


AppCompatActivity { DatabaseHelper
dbHelper;
EditText nameInput, ageInput;
Button saveButton, showButton;
TextView displayData;

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

dbHelper = new DatabaseHelper(this);


nameInput =
findViewById(R.id.nameInput); ageInput =
findViewById(R.id.ageInput); saveButton
= findViewById(R.id.saveButton);
showButton =
findViewById(R.id.showButton); displayData
= findViewById(R.id.displayData);

saveButton.setOnClickListener(new
View.OnClickListener() { @Override
public void onClick(View v) {
String name = nameInput.getText().toString();
int age =
Integer.parseInt(ageInput.getText().toString());
boolean isInserted = dbHelper.insertData(name,
age); if (isInserted) {
1
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB Toast.makeText(MainActivity.this, "Data Saved", SUBJECT CODE : PBC 501
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Error Saving Data",
Toast.LENGTH_SHORT).show();
}
}
});

showButton.setOnClickListener(new View.OnClickListener() {

2
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB @Override SUBJECT CODE : PBC 501
public void onClick(View v) {
Cursor cursor = dbHelper.getAllData();
StringBuilder stringBuilder = new
StringBuilder();

if (cursor.getCount() == 0)
{ displayData.setText("No Data
Found"); return;
}

while (cursor.moveToNext()) {
stringBuilder.append("ID: ").append(cursor.getInt(0)).append("\n");
stringBuilder.append("Name: ").append(cursor.getString(1)).append("\n");
stringBuilder.append("Age: ").append(cursor.getInt(2)).append("\n\n");
}
displayData.setText(stringBuilder.toString());
}
});
}
}

DATABASEHELPER CODE :

package com.example.databaseapp;

import
android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import
android.database.sqlite.SǪLiteDatabase;
import
android.database.sqlite.SǪLiteOpenHelper;

public class DatabaseHelper extends SǪLiteOpenHelper {


private static final String DATABASE_NAME =
"UserDatabase.db"; private static final int
DATABASE_VERSION = 1;

private static final String TABLE_NAME =


"Users"; private static final String COLUMN_ID
= "ID";
private static final String COLUMN_NAME =
"Name"; private static final String
COLUMN_AGE = "Age";

public DatabaseHelper(Context context) {


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

3
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB
@Override SUBJECT CODE : PBC 501
public void onCreate(SǪLiteDatabase db) {
String createTableǪuery = "CREATE TABLE " + TABLE_NAME
+ "(" + COLUMN_ID + " INTEGER PRIMARY KEY
AUTOINCREMENT, " + COLUMN_NAME + " TEXT, " +

4
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB COLUMN_AGE + " INTEGER)"; SUBJECT CODE : PBC 501
db.execSǪL(createTableǪuery);
}

@Override
public void onUpgrade(SǪLiteDatabase db, int oldVersion, int newVersion) {
db.execSǪL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

public boolean insertData(String name, int


age) { SǪLiteDatabase db =
this.getWritableDatabase(); ContentValues
values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_AGE, age);
long result = db.insert(TABLE_NAME, null,
values); return result != -1;
}

public Cursor getAllData() {


SǪLiteDatabase db = this.getReadableDatabase();
return db.rawǪuery("SELECT * FROM " + TABLE_NAME, null);
}
}

XML CODE :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/and
roid" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<EditText
android:id="@+id/nameInput"
android:layout_width="match_pa
rent"
android:layout_height="wrap_con
tent" android:hint="Enter Name"
/>

<EditText
android:id="@+id/ageInp
ut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Age"
android:inputType="number" />

5
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB
<Button SUBJECT CODE : PBC 501
android:id="@+id/saveButton"
android:layout_width="match_parent"

6
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB android:layout_height="wrap_con SUBJECT CODE : PBC 501
tent" android:text="Save Data"
/>

<Button
android:id="@+id/showButton"
android:layout_width="match_pa
rent"
android:layout_height="wrap_con
tent" android:text="Show Data"
/>

<TextView
android:id="@+id/displayData"
android:layout_width="match_pare
nt"
android:layout_height="wrap_conte
nt" android:padding="16dp"
android:textSize="16sp" />
</LinearLayout>

7
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB
Problem Statement 02: Create android application to perform SUBJECT
crud CODE : in
operation PBC 501
the
database.

JAVA CODE :
package com.example.crudapp;

import
android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
import
android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends


AppCompatActivity { DatabaseHelper
dbHelper;
EditText idInput, nameInput, ageInput;
Button addButton, updateButton, deleteButton,
viewButton; TextView displayData;

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

dbHelper = new DatabaseHelper(this);


idInput = findViewById(R.id.idInput);
nameInput =
findViewById(R.id.nameInput); ageInput =
findViewById(R.id.ageInput); addButton =
findViewById(R.id.addButton);
updateButton =
findViewById(R.id.updateButton); deleteButton
= findViewById(R.id.deleteButton); viewButton
= findViewById(R.id.viewButton); displayData
= findViewById(R.id.displayData);

addButton.setOnClickListener(v -> {
String name = nameInput.getText().toString();
int age =
Integer.parseInt(ageInput.getText().toString());
boolean isInserted =
dbHelper.insertData(name, age);
8
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB showToast(isInserted, "Record Added"); SUBJECT CODE : PBC 501
});

updateButton.setOnClickListener(v -> {
int id =
Integer.parseInt(idInput.getText().toString());
String name =
nameInput.getText().toString();
int age =
Integer.parseInt(ageInput.getText().toString()); boolean
isUpdated = dbHelper.updateData(id, name, age);
showToast(isUpdated, "Record Updated");
});

9
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB SUBJECT CODE : PBC 501
deleteButton.setOnClickListener(v -> {
int id =
Integer.parseInt(idInput.getText().toString());
boolean isDeleted =
dbHelper.deleteData(id);
showToast(isDeleted, "Record Deleted");
});

viewButton.setOnClickListener(v -
> { Cursor cursor =
dbHelper.getAllData();
StringBuilder stringBuilder = new StringBuilder();

if (cursor.getCount() == 0)
{ displayData.setText("No Data
Found"); return;
}

while (cursor.moveToNext()) {
stringBuilder.append("ID: ").append(cursor.getInt(0)).append("\n");
stringBuilder.append("Name: ").append(cursor.getString(1)).append("\n");
stringBuilder.append("Age: ").append(cursor.getInt(2)).append("\n\n");
}

displayData.setText(stringBuilder.toString());
});
}

private void showToast(boolean isSuccess, String


message) { if (isSuccess) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Error Occurred", Toast.LENGTH_SHORT).show();
}
}
}

DATABASE HELPER CODE:


package com.example.crudapp;

import
android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import
android.database.sqlite.SǪLiteDatabase;
import
android.database.sqlite.SǪLiteOpenHelper;

10
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB
public class DatabaseHelper extends SUBJECT CODE : PBC 501
SǪLiteOpenHelper { private static final String
DATABASE_NAME = "StudentDB"; private static final
int DATABASE_VERSION = 1;

private static final String TABLE_NAME = "Students";

11
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB
private static final String COLUMN_ID
= "ID"; SUBJECT CODE : PBC 501
private static final String COLUMN_NAME =
"Name"; private static final String
COLUMN_AGE = "Age";

public DatabaseHelper(Context context) {


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

@Override
public void onCreate(SǪLiteDatabase db) {
String createTableǪuery = "CREATE TABLE " + TABLE_NAME
+ " (" + COLUMN_ID + " INTEGER PRIMARY KEY
AUTOINCREMENT, " + COLUMN_NAME + " TEXT, " +
COLUMN_AGE + " INTEGER)";
db.execSǪL(createTableǪuery);
}

@Override
public void onUpgrade(SǪLiteDatabase db, int oldVersion, int newVersion) {
db.execSǪL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

// Create
public boolean insertData(String name, int
age) { SǪLiteDatabase db =
this.getWritableDatabase(); ContentValues
values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_AGE, age);
long result = db.insert(TABLE_NAME, null,
values); return result != -1;
}

// Read
public Cursor getAllData() {
SǪLiteDatabase db = this.getReadableDatabase();
return db.rawǪuery("SELECT * FROM " + TABLE_NAME, null);
}

// Update
public boolean updateData(int id, String name,
int age) { SǪLiteDatabase db =
this.getWritableDatabase(); ContentValues
values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_AGE, age);
int rowsAffected = db.update(TABLE_NAME, values, COLUMN_ID + "=?", new
String[]{String.valueOf(id)});
return rowsAffected > 0;
12
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB
} SUBJECT CODE : PBC 501

13
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB SUBJECT CODE : PBC 501
// Delete
public boolean deleteData(int id) {
SǪLiteDatabase db = this.getWritableDatabase();
int rowsDeleted = db.delete(TABLE_NAME, COLUMN_ID + "=?", new String[]
{String.valueOf(id)}); return rowsDeleted > 0;
}
}

XML CODE :
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/and
roid" android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<EditText
android:id="@+id/idInp
ut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter ID"
android:inputType="number" />

<EditText
android:id="@+id/nameInput"
android:layout_width="match_pa
rent"
android:layout_height="wrap_con
tent" android:hint="Enter Name"
/>

<EditText
android:id="@+id/ageInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Age"
android:inputType="number" />

<Button
android:id="@+id/addButton
"
android:layout_width="match_pa
rent"
android:layout_height="wrap_con
tent" android:text="Add Record"
14
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB /> SUBJECT CODE : PBC 501

<Button
android:id="@+id/updateButt
on"

15
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB android:layout_width="match_pa SUBJECT CODE : PBC 501
rent"
android:layout_height="wrap_con
tent" android:text="Update
Record" />

<Button
android:id="@+id/deleteButton"
android:layout_width="match_pa
rent"
android:layout_height="wrap_con
tent" android:text="Delete
Record" />

<Button
android:id="@+id/viewButton
"
android:layout_width="match_pa
rent"
android:layout_height="wrap_con
tent" android:text="View All
Records" />

<TextView
android:id="@+id/displayData"
android:layout_width="match_pa
rent"
android:layout_height="wrap_con
tent"
android:text="Data will be displayed
here" android:padding="16dp"
android:textSize="16sp" />

</LinearLayout>
</ScrollView>

16
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB
Problem Statement 03: Create android application to load SUBJECT CODE : PBC 501

Google maps. JAVA CODE :

package com.example.googlemapsapp;

import

androidx.fragment.app.FragmentActivity;

import android.os.Bundle;

import
com.google.android.gms.maps.CameraUpdateFactory
; import com.google.android.gms.maps.GoogleMap;
import
com.google.android.gms.maps.OnMapReadyCallback;
import
com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends FragmentActivity implements

OnMapReadyCallback { private GoogleMap mMap;

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

// Obtain the SupportMapFragment and get notified when the map is ready to be
used. SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.ma
p); if (mapFragment != null) {
mapFragment.getMapAsync(this);
}
}

@Override
public void onMapReady(GoogleMap
googleMap) { mMap = googleMap;

// Add a marker in a location (e.g., Sydney) and move


the camera LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in
Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
17
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB SUBJECT CODE : PBC 501
MANIFEST .XML FILE :

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


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

18
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB
xmlns:tools="http://schemas.android.com/tools"> SUBJECT CODE : PBC 501

<application
android:allowBackup="tr
ue"
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.GoogleMapsApp"
tools:targetApi="31">

<meta-data
android:name="com.google.android.geo.API
_KEY"
android:value="AIzaSyDYSaLIFOhVhL1rNhJV--r0OEǪ3q6lsqR4" />

<activity
android:name=".MainActi
vity"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</intent-filter>
</activity>
</application>

</

manifest>

XML

CODE :

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragme
nt" android:layout_width="match_parent"
android:layout_height="match_parent" />
19
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB
</RelativeLayout> SUBJECT CODE : PBC 501

20
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB SUBJECT CODE : PBC 501
Problem Statement 04: Create an application to show EMEI number access the

phone state. JAVA CODE :

package com.example.showimei;

import android.Manifest;
import
android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import
android.telephony.TelephonyManager;
import android.widget.Button;
import
android.widget.TextView;
import
android.widget.Toast;
import androidx.annotation.NonNull;
import
androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity {

private static final int PERMISSION_REǪUEST_CODE = 1;


private TextView imeiText;

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

imeiText = findViewById(R.id.imeiText);
Button getIMEIButton = findViewById(R.id.getIMEIButton);

getIMEIButton.setOnClickListener(v -> {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
==
PackageManager.PERMISSION_GRANTED) {
getIMEI();
} else {
requestPhoneStatePermission(
);
}
});
}

21
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB
private void getIMEI() SUBJECT CODE : PBC 501
{ try {
TelephonyManager telephonyManager = (TelephonyManager)
getSystemService(TELEPHONY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Ǫ) {
imeiText.setText("IMEI access is restricted on Android 10+");
} else if (telephonyManager != null) {

22
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB String imei = SUBJECT CODE : PBC 501
telephonyManager.getDeviceId();
imeiText.setText("IMEI: " + imei);
} else {
imeiText.setText("Unable to fetch IMEI");
}
} catch (SecurityException e) {
Toast.makeText(this, "Permission not granted!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}

private void requestPhoneStatePermission() {


ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_PHONE_STATE},
PERMISSION_REǪUEST_CODE);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults); if (requestCode == PERMISSION_REǪUEST_CODE) {
if (grantResults.length > 0 CC grantResults[0] ==
PackageManager.PERMISSION_GRANTED) { getIMEI();
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
}
}
}

MANIFEST.XML CODE :

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<uses-permission
android:name="android.permission.READ_PRIVILEGED_PHONE_STATE"
tools:ignore="ProtectedPermissions" />

<application
android:allowBackup="tr
ue"
android:dataExtractionRules="@xml/
data_extraction_rules"
23
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB android:fullBackupContent="@xml/backup_rules" SUBJECT CODE : PBC 501
android:icon="@mipmap/ic_launcher"

24
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB android:label="@string/app_name" SUBJECT CODE : PBC 501
android:roundIcon="@mipmap/ic_launcher_ro
und" android:supportsRtl="true"
android:theme="@style/Theme.ShowIMEI"
tools:targetApi="31">
<activity
android:name=".MainActi
vity"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</intent-filter>
</activity>
</application>

</

manifest>

XML

CODE :

<?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="verti
cal"
android:padding="16dp">

<TextView
android:id="@+id/imeiTe
xt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="IMEI will appear here"
android:textSize="18sp"
android:textStyle="bold"
android:padding="8dp"
android:gravity="center" />

<Button
android:id="@+id/getIMEIButton
"
android:layout_width="wrap_con
tent"
25
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB android:layout_height="wrap_cont SUBJECT CODE : PBC 501
ent" android:text="Get IMEI"
android:layout_gravity="center" /
>
</LinearLayout>

26
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB
Problem Statement 05: Create an android application to show SUBJECT
OTP CODE : PBC 501
based

authentication. JAVA CODE :

package com.example.otpautnetication;

import android.os.Bundle;
import android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import
com.google.firebase.FirebaseException;
import
com.google.firebase.auth.FirebaseAuth;
import
com.google.firebase.auth.PhoneAuthCredential;
import
com.google.firebase.auth.PhoneAuthProvider;

public class MainActivity extends


AppCompatActivity { private FirebaseAuth
mAuth;
private String verificationId;

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

mAuth = FirebaseAuth.getInstance();

EditText phoneInput =
findViewById(R.id.phoneInput); EditText otpInput =
findViewById(R.id.otpInput);
Button sendOtpButton =
findViewById(R.id.sendOtpButton); Button
verifyOtpButton = findViewById(R.id.verifyOtpButton);

sendOtpButton.setOnClickListener(v -> {
String phoneNumber = phoneInput.getText().toString();
if (phoneNumber.isEmpty() || phoneNumber.length() < 10) {
Toast.makeText(MainActivity.this, "Enter a valid phone number",
27
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB
Toast.LENGTH_SHORT).show(); SUBJECT CODE : PBC 501
return;
}

sendVerificationCode(phoneNumber);
otpInput.setVisibility(View.VISIBLE);
verifyOtpButton.setVisibility(View.VISIBLE);
});

28
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB verifyOtpButton.setOnClickListener(v SUBJECT CODE : PBC 501
-> { String otp =
otpInput.getText().toString(); if
(otp.isEmpty() || otp.length() < 6) {
Toast.makeText(MainActivity.this, "Enter valid OTP",
Toast.LENGTH_SHORT).show(); return;
}
verifyCode(otp);
});
}

private void sendVerificationCode(String


phoneNumber)
{ PhoneAuthProvider.getInstance().verifyPhoneN
umber(
phoneNumber, 60, java.util.concurrent.TimeUnit.SECONDS,
this, new
PhoneAuthProvider.OnVerificationStateChangedCallbacks()
{
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
// Auto-retrieve OTP
signInWithCredential(creden
tial);
}

@Override
public void onVerificationFailed(@NonNull FirebaseException e) {

public void onVerificationFailed(Exception e) {


Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}

@Override
public void onCodeSent(String verificationId,
token) PhoneAuthProvider.ForceResendingToken
{
MainActivity.this.verificationId = verificationId;
Toast.makeText(MainActivity.this, "OTP Sent",
Toast.LENGTH_SHORT).show();
}
});
}

private void verifyCode(String code) {


PhoneAuthCredential credential =
PhoneAuthProvider.getCredential(verificationId, code);
signInWithCredential(credential);
}
29
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB SUBJECT CODE : PBC 501
private void signInWithCredential(PhoneAuthCredential credential)
{ mAuth.signInWithCredential(credential)
.addOnCompleteListener(task
-> { if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Logged in successfully",
Toast.LENGTH_SHORT).show();
} else {

30
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB Toast.makeText(MainActivity.this, SUBJECT CODE : PBC 501
task.getException().getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
}

XML CODE :

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/and
roid" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<EditText
android:id="@+id/phoneInput"
android:layout_width="match_pa
rent"
android:layout_height="wrap_content"
android:hint="Enter Phone Number"
android:inputType="phone" />

<Button
android:id="@+id/sendOtpButton"
android:layout_width="match_pare
nt"
android:layout_height="wrap_conte
nt" android:text="Send OTP" />

<EditText
android:id="@+id/otpInp
ut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter OTP"
android:inputType="number"
android:visibility="gone" />

<Button
android:id="@+id/verifyOtpButton
"
android:layout_width="match_pare
nt"
android:layout_height="wrap_conte
nt" android:text="Verify OTP"
android:visibility="gone" />
</LinearLayout>

31
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB SUBJECT CODE : PBC 501

32
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB SUBJECT CODE : PBC 501
Problem Statement 05: Create an application to

implement login. JAVA CODE ;

package com.example.loginapp;

import android.os.Bundle;
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 {

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

EditText etEmail = findViewById(R.id.etEmail);


EditText etPassword =
findViewById(R.id.etPassword); Button btnLogin
= findViewById(R.id.btnLogin);

btnLogin.setOnClickListener(new
View.OnClickListener() { @Override
public void onClick(View v) {
String email = etEmail.getText().toString();
String password = etPassword.getText().toString();

if (email.equals("[email protected]") CC password.equals("password123")) {
Toast.makeText(MainActivity.this, "Login Successful!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Invalid Email or Password",
Toast.LENGTH_SHORT).show();
}
}
});
}
33
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB
} SUBJECT CODE : PBC 501

XML CODE :

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/and
roid" android:layout_width="match_parent"

34
NAME : SHIVAM NEGI UNIVERSITY ROLL.NO :
COURSE : BCA C1 2221967 CLASS ROLL.NO :
SUBJECT : ANDROID PROGRAMMING 58
LAB
android:layout_height="match_parent" SUBJECT CODE : PBC 501
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">

<EditText
android:id="@+id/etEmai
l"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress" />

<EditText
android:id="@+id/etPassword"
android:layout_width="match_pa
rent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:layout_marginTop="8dp" />

<Button
android:id="@+id/btnLo
gin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="16dp" />
</LinearLayout>

35

You might also like