0% found this document useful (0 votes)
0 views37 pages

Android End FILE

The document provides code examples for creating Android applications that demonstrate the use of fragments, array adapters, and CRUD operations with a SQLite database. It includes Java code for main activities and fragments, as well as XML layout files for user interfaces. Each section outlines the necessary components and functionalities for the respective applications.

Uploaded by

thapakartik021
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)
0 views37 pages

Android End FILE

The document provides code examples for creating Android applications that demonstrate the use of fragments, array adapters, and CRUD operations with a SQLite database. It includes Java code for main activities and fragments, as well as XML layout files for user interfaces. Each section outlines the necessary components and functionalities for the respective applications.

Uploaded by

thapakartik021
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/ 37

NAME -: SAMEER SINGH UNIVERSITY ROLL NO.

-: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

Q14 - Create an android application to depict fragments.

Code -

MainActvitiy.java -

package com.example.fragmentsapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


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

Button buttonFragmentOne = findViewById(R.id.buttonFragmentOne);


Button buttonFragmentTwo = findViewById(R.id.buttonFragmentTwo);

buttonFragmentOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(new FragmentOne());
}
});

buttonFragmentTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
replaceFragment(new FragmentTwo());
}
});
}

private void replaceFragment(Fragment fragment) {


FragmentManager fragmentManager = getSupportFragmentManager();

1
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, fragment);
fragmentTransaction.commit();
}
}

FragmentOne.java –

package com.example.fragmentsapp;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentOne extends Fragment {


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one, container, false);
}
}

FragmentTwo –

package com.example.fragmentsapp;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentTwo extends Fragment {


@Nullable

2
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
}

XML Code -

<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">

<Button
android:id="@+id/buttonFragmentOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Fragment One" />

<Button
android:id="@+id/buttonFragmentTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Fragment Two" />

<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>

Fragment_one.xml

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

3
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment One"
android:textSize="20sp" />
</LinearLayout>

Fragment_two.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Fragment Two"
android:textSize="20sp" />
</LinearLayout>

Output :

4
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

Q15 - Create a android application to show use of array adapter ?

Code –

package com.example.arrayadapterdemo;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

5
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Sample data for the ListView

String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Grape", "Kiwi",


"Mango", "Orange", "Pineapple", "Watermelon"};

// Reference to the ListView in the layout

ListView listView = findViewById(R.id.listView);

// Creating an ArrayAdapter

ArrayAdapter<String> adapter = new ArrayAdapter<>(

this,

android.R.layout.simple_list_item_1,

fruits

);

// Setting the adapter to the ListView

listView.setAdapter(adapter);

XML CODE :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content

6
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

android:layout_height="wrap_content
android:orientation="vertical"
android:padding="16dp">

<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@android:color/darker_gray"
android:dividerHeight="1dp" />
</LinearLayout>

Output –

7
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

16. Create an android application to step by step create a


database..

CODE –

package com.example.databaseapp;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

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

private static final String TABLE_NAME = "students";

private static final String COL_1 = "ID";

private static final String COL_2 = "NAME";

private static final String COL_3 = "AGE";

public DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, 1);

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE " TABLE_NAME " (ID INTEGER PRIMARY KEY


AUTOINCREMENT, NAME TEXT, AGE INTEGER)");

8
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)


{

db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

onCreate(db);

public boolean insertData(String name, int age) {

SQLiteDatabase db = this.getWritableDatabase();

ContentValues contentValues = new ContentValues();

contentValues.put(COL_2 name);

contentValues.put(COL_3age);

long result = db.insert(TABLE_NAME, null, contentValues);

return result != -1; // Return true if insert is successful

public Cursor getAllData() {

SQLiteDatabase db = this.getWritableDatabase();

return db.rawQuery("SELECT * FROM " TABLE_NAME, null);

public boolean updateData(String id, String name, int age) {

SQLiteDatabase db = this.getWritableDatabase();

ContentValues contentValues = new ContentValues();

contentValues.put(COL_2, name);

contentValues.put(COL_3, age);

9
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

int results = db.update(TABLE_NAME, contentValues, "ID = ?", new


String[]{id});

return result > 0;

public Integer deleteData(String id) {

SQLiteDatabase db = this.getWritableDatabase();

return db.delete(TABLE_NAME, "ID = ?", new String[]{id});

Javacode 2 :

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.ListView;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

DatabaseHelper databaseHelper;

EditText editName, editAge, editId;

10
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

Button btnAdd, btnView, btnUpdate, btnDelete;

ListView listView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

databaseHelper = new DatabaseHelper(this);

editName = findViewById(R.id.editName);

editAge = findViewById(R.id.editAge);

editId = findViewById(R.id.editId);

btnAdd = findViewById(R.id.btnAdd);

btnView = findViewById(R.id.btnView);

btnUpdate = findViewById(R.id.btnUpdate);

btnDelete = findViewById(R.id.btnDelete);

listView = findViewById(R.id.listView);

btnAdd.setOnClickListener(v -> {

String name = editName.getText().toString();

int age = Integer.parseInt(editAge.getText().toString());

boolean isInserted = databaseHelper.insertData(name, age);

showToast(isInserted ? "Data Inserted" : "Insertion Failed");

});

11
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

btnView.setOnClickListener(v -> viewAllData());

btnUpdate.setOnClickListener(v -> {

String id = editId.getText();

String name = editName.getText().toString();

int age = Integer.parseInt(editAge.getText().toString());

boolean isUpdated = databaseHelper.updateData(id, name, age);

showToast(isUpdated ? "Data Updated" : "Update Failed");

});

btnDelete.setOnClickListener(v -> {

String id = editId.getText().toString();

Integer deletedRows = databaseHelper.deleteData(id);

showToast(deletedRows > 0 ? "Data Deleted" : "Deletion Failed");

});

private void viewAllData() {

Cursor cursor = databaseHelper.getAllData();

if (cursor.getCount() == 0) {

showToast("No Data Found");

return;

ArrayList<String> data = new ArrayList<>();

while (cursor.moveToNext()) {

12
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

data.add("ID: " + cursor.getString(0) + ", Name: " +


cursor.getString(1) + ", Age: " + cursor.getString(2));

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,


android.R.layout.simple_list_item_1, data);

listView.setAdapter(adapter);

private void showToast(String message) {

Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

Xml code :

<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">

<EditText

android:id="@+id/editName"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter Name" />

<EditText

13
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

android:id="@+id/editAge"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter Age"

android:inputType="number" />

<EditText

android:id="@+id/editId"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter ID (for update/delete)" />

<Button

android:id="@+id/btAdd"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Add Data" />

<Button

android:id="@+id/btViews"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="View All Data" />

<Button

android:id="@+id/btUpdate"

14
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Update Data" />

<Button

android:id="@+id/btDelete"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Delete Data" />

<ListView

android:id="@+id/listView"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

</LinearLayout>

Output –

15
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

16
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

17. Create an android application to perform CRUD operation in


database.

CODE :

Javafile 1 –

package com.example.crudapp;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

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

private static final String TABLE_NAME = "students";

private static final String COL_1 = "ID";

private static final String COL_2 = "NAME";

private static final String COL_3 = "AGE";

public DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, 1);

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY


KEY AUTOINCREMENT, NAME TEXT, AGE INTEGER)");

17
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)


{

db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

onCreate(db);

// CREATE

public boolean insertData(String name, int age) {

SQLiteDatabase db = this.getWritableDatabase();

ContentValues contentValues = new ContentValues();

contentValues.put(COL_2, name);

contentValues.put(COL_3, age);

long result = db.insert(TABLE_NAME, null, contentValues);

return result != -1;

// READ

public Cursor getAllData() {

SQLiteDatabase db = this.getWritableDatabase();

return db.rawQuery("SELECT * FROM " + TABLE_NAME, null);

// UPDATE

public boolean updateData(String id, String name, int age) {

SQLiteDatabase db = this.getWritableDatabase();

18
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

ContentValues contentValues = new ContentValues();

contentValues.put(COL_2, name);

contentValues.put(COL_3, age);

int result = db.update(TABLE_NAME, contentValues, "ID = ?", new


String[]{id});

return result > 0;

// DELETE

public Integer deleteData(String id) {

SQLiteDatabase db = this.getWritableDatabase();

return db.delete(TABLE_NAME, "ID = ?", new String[]{id});

JavaFile 2 –

package com.example.crudapp;

import android.database.Cursor;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

19
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

DatabaseHelper databaseHelper;

EditText editName, editAge, editId;

Button btnAdd, btnView, btnUpdate, btnDelete;

ListView listView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

databaseHelper = new DatabaseHelper(this);

editName = findViewById(R.id.editName);

editAge = findViewById(R.id.editAge);

editId = findViewById(R.id.editId);

btnDelete = findViewById(R.id.btnDelete);

listView = findViewById(R.id.listView);

// CREATE

btnAdd.setOnClickListener(v -> {

String name = editName.getText().toString();

int age = Integer.parseInt(editAge.getText().toString());

boolean isInserted = databaseHelper.insertData(name, age);

20
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

showToast(isInserted ? "Data Inserted" : "Insertion Failed");

});

// READ

btnView.setOnClickListener(v -> viewAllData());

// UPDATE

btnUpdate.setOnClickListener(v -> {

String id = editId.getText().toString();

String name = editName.getText().toString();

int age = Integer.parseInt(editAge.getText().toString());

boolean isUpdated = databaseHelper.updateData(id, name, age);

showToast(isUpdated ? "Data Updated" : "Update Failed");

});

// DELETE

btnDelete.setOnClickListener(v -> {

String id = editId.getText().toString();

Integer deletedRows = databaseHelper.deleteData(id);

showToast(deletedRows > 0 ? "Data Deleted" : "Deletion Failed");

});

private void viewAllData() {

Cursor cursor = databaseHelper.getAllData();

if (cursor.getCount() == 0) {

21
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

showToast("No Data Found");

return;

ArrayList<String> data = new ArrayList<>();

while (cursor.moveToNext()) {

data.add("ID: " + cursor.getString(0) + ", Name: " +


cursor.getString(1) + ", Age: " + cursor.getString(2));

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,


android.R.layout.simple_list_item_1, data);

listView.setAdapter(adapter);

private void showToast(String message) {

Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

XML CODE :

<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">

<EditText
android:id="@+id/editName"
android:layout_width="match_parent"

22
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

android:layout_height="wrap_content"
android:hint="Enter Name" />

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

<EditText
android:id="@+id/editd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter ID (for update/delete)" />

<Button
android:id="@+id/btnAddd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Data" />

<Button
android:id="@+id/btnView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View All Data" />

<Button
android:id="@+id/btnUpdate"
android:layout_width="wrap_content
android:layout_height="wrap_content"
android:text="Update Data" />

<Button
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Data" />

23
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

OUTPUT :

24
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

18. Create an android application to load Google map .

CODE :

Manifest xml code :

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

package="com.example.googlemapapp">
25
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

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

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

<application

android:allowBackup="true"

android:label="GoogleMapApp"

android:theme="@style/Theme.AppCompat.Light.DarkActionBar">

<!-- Google Maps API Key -->

<meta-data

android:name="com.google.android.geo.API_KEY"

android:value="lajdflkjasdflasjdflkasldfwlwl23lsd" />

<activity

android:name=".MapsActivity"

android:exported="true">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

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

</intent-filter>

</activity>

</application>

</manifest>

26
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

Javacode :

package com.example.googlemapapp;

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;

import com.example.googlemapapp.databinding.ActivityMapsBinding;

public class MapsActivity extends FragmentActivity implements


OnMapReadyCallback {

private GoogleMap mMap;

private ActivityMapsBinding binding;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

binding = ActivityMapsBinding.inflate(getLayoutInflater());

setContentView(binding.getRoot());

27
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

// Obtain the SupportMapFragment and get notified when the map is


ready to be used.

SupportMapFragment mapFragment = (SupportMapFragment)


getSupportFragmentManager()

.findFragmentById(R.id.map);

mapFragment.getMapAsync(this);

@Override

public void onMapReady(GoogleMap googleMap) {

mMap = googleMap;

// Add a marker at a specific location and move the camera

LatLng sydney = new LatLng(-34, 151); // Example: Sydney, Australia

mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in
Sydney"));

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,
10));

Xml code :

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

android:layout_width="match_parent"

android:layout_height="match_parent">

28
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

<fragment

android:id="@+id/map"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:name="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

OUTPUT :

19. Create an android application to show EMEI number and access


the phone state .

Code :

Manifest xml code :

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

29
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

package="com.example.imeinumber">

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

<application

android:allowBackup="true"

android:label="IMEI App"

android:theme="@style/Theme.AppCompat.Light.DarkActionBar">

<activity android:name=".MainActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

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

</intent-filter>

</activity>

</application>

</manifest>

Java code :

package com.example.imeinumber;

import android.Manifest;

import android.content.pm.PackageManager;

import android.os.Build;

import android.os.Bundle;

import android.telephony.TelephonyManager;

import android.widget.TextView;

30
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

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_REQUEST_CODE = 100;

private TextView imeiTextView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imeiTextView = findViewById(R.id.imeiTextView);

// Check and request permissions

if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE)

!= PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(this,

new String[]{Manifest.permission.READ_PHONE_STATE},
PERMISSION_REQUEST_CODE);

} else {

displayIMEI();

31
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

private void displayIMEI() {

try {

TelephonyManager telephonyManager = (TelephonyManager)


getSystemService(TELEPHONY_SERVICE);

if (telephonyManager != null) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

String imei = telephonyManager.getImei();

imeiTextView.setText("IMEI Number: " + imei);

} else {

String imei = telephonyManager.getDeviceId();

imeiTextView.setText("IMEI Number: " + imei);

} catch (SecurityException e) {

Toast.makeText(this, "Permission Denied!",


Toast.LENGTH_SHORT).show();

@Override

public void onRequestPermissionsResult(int requestCode, @NonNull


String[] permissions,

@NonNull int[] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions,
grantResults);

32
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

if (requestCode == PERMISSION_REQUEST_CODE) {

if (grantResults.length > 0) {

displayIMEI();

} else {

Toast.makeText(this, "Permission Denied!",


Toast.LENGTH_SHORT).show();

Xml Code :

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="16dp">

<TextView

android:id="@+id/imeiTextView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="IMEI Number will appear here"

android:textSize="18sp"

android:layout_centerInParent="true" />

33
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

</RelativeLayout>

OUTPUT :

Q21 - Create an application to implement login.

CODE :

Javacode :

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 {

private EditText usernameEditText, passwordEditText;


private Button loginButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
34
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

// Initialize views
usernameEditText = findViewById(R.id.username);
passwordEditText = findViewById(R.id.password);
loginButton = findViewById(R.id.loginButton);

// Set up the login button's onClickListener


loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();

// Basic validation
if (username.equals("user") && password.equals("password123"))
{
Toast.makeText(MainActivity.this, "Login successful",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Invalid credentials",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

xmlcode :

<?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">

35
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

<EditText

android:id="@+id/username"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Username"

android:inputType="text" />

<EditText

android:id="@+id/password"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Password"

android:inputType="textPassword" />

<Button

android:id="@+id/loginButton"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Login" />

</LinearLayout>

OUTPUT :

36
NAME -: SAMEER SINGH UNIVERSITY ROLL NO. -: 2221901
SUBJECT -: ANDROID PROGRAMMING LAB SUBJECT CODE -: PBC 501

37

You might also like