0% found this document useful (0 votes)
4 views47 pages

Android Codesssss

The document provides a comprehensive list of 33 unique Android coding questions, including sample code for various applications such as a progress bar, SMS sending and receiving, email functionality, Bluetooth connectivity, ListView and GridView implementations, and a customer details storage application using SQLite. Each section includes XML layout files and corresponding Java code for the main activity and other necessary components. The document serves as a practical guide for developing fundamental Android applications.
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)
4 views47 pages

Android Codesssss

The document provides a comprehensive list of 33 unique Android coding questions, including sample code for various applications such as a progress bar, SMS sending and receiving, email functionality, Bluetooth connectivity, ListView and GridView implementations, and a customer details storage application using SQLite. Each section includes XML layout files and corresponding Java code for the main activity and other necessary components. The document serves as a practical guide for developing fundamental Android applications.
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/ 47

Final List of 33 Unique Android Coding Questions11

1. Develop a program to display a rectangular progress bar.

🔹 activity_main.xml
xml
CopyEdit
<?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:gravity="center"
android:padding="20dp">

<ProgressBar
android:id="@+id/progressBar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:progress="60"
android:max="100" />
</LinearLayout>

🔹 MainActivity.java
java
CopyEdit
package com.example.progressbarapp;

import android.os.Bundle;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


ProgressBar progressBar;

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

progressBar = findViewById(R.id.progressBar);
progressBar.setProgress(60); // Set 60% progress
}
}

2. Develop an application to send and receive SMS (Write Java and manifest permissions only).

🔹 activity_main.xml (2 Marks)
xml
CopyEdit
<?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="20dp">

<EditText
android:id="@+id/etNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Number"
android:inputType="phone" />

<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Message" />

<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS" />
</LinearLayout>

🔹 MainActivity.java (3 Marks)
java
CopyEdit
package com.example.sendsms;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity {


EditText etPhone, etMessage;
Button btnSend;

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

etPhone = findViewById(R.id.etPhone);
etMessage = findViewById(R.id.etMessage);
btnSend = findViewById(R.id.btnSend);

ActivityCompat.requestPermissions(this, new String[]


{Manifest.permission.SEND_SMS}, 1);

btnSend.setOnClickListener(v -> {
String phone = etPhone.getText().toString();
String msg = etMessage.getText().toString();
SmsManager.getDefault().sendTextMessage(phone, null, msg, null, null);
Toast.makeText(this, "SMS Sent", Toast.LENGTH_SHORT).show();
});
}
}

🔹 SMSReceiver.java
java
package com.example.smsapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus != null) {
for (int i = 0; i < pdus.length; i++) {
byte[] pdu = (byte[]) pdus[i];
SmsMessage message = SmsMessage.createFromPdu(pdu);
String sender = message.getOriginatingAddress();
String body = message.getMessageBody();
Toast.makeText(context, "From: " + sender + "\n" + body,
Toast.LENGTH_LONG).show();
}
}
}
}
}

🔹 Minimal AndroidManifest.xml
xml
CopyEdit
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />

<receiver
android:name=".SMSReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

3. Develop a program to send and receive an Email.

MainActivity.java (Sending Email)


java
CopyEdit
package com.example.sendemail;

import android.content.Intent;
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 {

EditText etEmail, etSubject, etMessage;


Button btnSendEmail;

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

etEmail = findViewById(R.id.etEmail);
etSubject = findViewById(R.id.etSubject);
etMessage = findViewById(R.id.etMessage);
btnSendEmail = findViewById(R.id.btnSendEmail);

btnSendEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = etEmail.getText().toString();
String subject = etSubject.getText().toString();
String message = etMessage.getText().toString();

Intent emailIntent = new Intent(Intent.ACTION_SEND);


emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{email});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, message);
emailIntent.setType("message/rfc822");

try {
startActivity(Intent.createChooser(emailIntent, "Choose an Email
client"));
Toast.makeText(MainActivity.this, "Email Sent",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "No email client found",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

activity_main.xml (UI Layout)


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

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

<EditText
android:id="@+id/etSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"
android:inputType="text" />

<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message"
android:inputType="textMultiLine" />

<Button
android:id="@+id/btnSendEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Email" />
</LinearLayout>

4. Develop a program for providing Bluetooth connectivity.

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

<TextView
android:id="@+id/out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth Status"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TURN ON"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="80dp"/>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DISCOVERABLE"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="30dp"/>

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TURN OFF"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="30dp"/>
</RelativeLayout>

2. MainActivity.java

java
CopyEdit
package com.example.bluetooth;

import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {


private static final int REQUEST_ENABLE_BT = 1;
private static final int REQUEST_DISCOVERABLE_BT = 2;
BluetoothAdapter bluetoothAdapter;
Button buttonTurnOn, buttonDiscoverable, buttonTurnOff;

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

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
buttonTurnOn = findViewById(R.id.button1);
buttonDiscoverable = findViewById(R.id.button2);
buttonTurnOff = findViewById(R.id.button3);

if (bluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth not supported on this device",
Toast.LENGTH_SHORT).show();
}

buttonTurnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
});

buttonDiscoverable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!bluetoothAdapter.isDiscovering()) {
Intent discoverableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(discoverableBtIntent,
REQUEST_DISCOVERABLE_BT);
}
}
});

buttonTurnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bluetoothAdapter.disable();
Toast.makeText(getApplicationContext(), "Bluetooth Turned Off",
Toast.LENGTH_LONG).show();
}
});
}
}

5. Develop a program to implement:

i) ListView of 5 items
ii) GridView of 4x4 items

iii)ImageView

1. Layout: activity_main.xml
xml
CopyEdit
<?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">

<!-- ListView -->


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

<!-- GridView -->


<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"/>

<!-- ImageView -->


<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sample_image" />

</LinearLayout>

2. Java Code: MainActivity.java


java
CopyEdit
package com.example.listviewgridviewimageview;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

ListView listView;
GridView gridView;
ImageView imageView;

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

listView = findViewById(R.id.listView);
gridView = findViewById(R.id.gridView);
imageView = findViewById(R.id.imageView);

// ListView setup
String[] listItems = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
ArrayAdapter<String> listAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(listAdapter);

// GridView setup
String[] gridItems = new String[16]; // 4x4 grid
for (int i = 0; i < gridItems.length; i++) {
gridItems[i] = "Item " + (i + 1);
}
ArrayAdapter<String> gridAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, gridItems);
gridView.setAdapter(gridAdapter);

// ImageView setup
// You can replace "sample_image" with your image resource
imageView.setImageResource(R.drawable.sample_image);
}
}

6. Develop an application to store customer's details (ID, Name, Mobile No., Address, Pin-code) and

retrieve data using SQLite.

✅ activity_main.xml (2M, now includes ID)


xml
CopyEdit
<?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="10dp">

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


android:layout_height="wrap_content" android:hint="Customer ID"/>
<EditText android:id="@+id/etName" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Name"/>
<EditText android:id="@+id/etMobile" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Mobile"/>
<EditText android:id="@+id/etAddress" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Address"/>
<EditText android:id="@+id/etPincode" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Pincode"/>

<Button android:id="@+id/btnInsert" android:layout_width="match_parent"


android:layout_height="wrap_content" android:text="Insert"/>
<Button android:id="@+id/btnShow" android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Show All"/>
<ListView android:id="@+id/listView" android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

✅ DatabaseHelper.java (1.5M — no changes needed unless you want to insert with custom ID)
java
CopyEdit
package com.example.customerdb;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.*;

public class DatabaseHelper extends SQLiteOpenHelper {

public DatabaseHelper(Context context) {


super(context, "custdb", null, 1);
}

public void onCreate(SQLiteDatabase db) {


db.execSQL("CREATE TABLE customer(id INTEGER PRIMARY KEY AUTOINCREMENT, name
TEXT, mobile TEXT, address TEXT, pincode TEXT)");
}

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

public void insert(String name, String mobile, String address, String pincode) {
getWritableDatabase().execSQL("INSERT INTO customer(name, mobile, address,
pincode) VALUES('" + name + "','" + mobile + "','" + address + "','" + pincode + "')");
}

public Cursor getData() {


return getReadableDatabase().rawQuery("SELECT * FROM customer", null);
}
}

✅ MainActivity.java (1.5M — now handles ID field)


java
CopyEdit
package com.example.customerdb;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends Activity {

EditText etId, etName, etMobile, etAddress, etPincode;


ListView listView;
DatabaseHelper db;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

etId = findViewById(R.id.etId);
etName = findViewById(R.id.etName);
etMobile = findViewById(R.id.etMobile);
etAddress = findViewById(R.id.etAddress);
etPincode = findViewById(R.id.etPincode);
listView = findViewById(R.id.listView);
db = new DatabaseHelper(this);

findViewById(R.id.btnInsert).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
db.insert(etName.getText().toString(), etMobile.getText().toString(),
etAddress.getText().toString(), etPincode.getText().toString());
}
});

findViewById(R.id.btnShow).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Cursor c = db.getData();
StringBuilder sb = new StringBuilder();
while (c.moveToNext()) {
sb.append("ID: ").append(c.getInt(0)).append("\n")
.append("Name: ").append(c.getString(1)).append("\n")
.append("Mobile: ").append(c.getString(2)).append("\n")
.append("Address: ").append(c.getString(3)).append("\n")
.append("Pincode: ").append(c.getString(4)).append("\n\n");
}
String[] arr = sb.toString().split("\n\n");
listView.setAdapter(new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1, arr));
}
});
}
}

7. Write a program to find the direction from user's current location to MSBTE, Bandra (Java + manifest
only).

package com.example.mapdirection;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.webkit.WebView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.google.android.gms.location.LocationServices;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

WebView webView = new WebView(this);


webView.getSettings().setJavaScriptEnabled(true);
setContentView(webView);

if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
return;
}

LocationServices.getFusedLocationProviderClient(this).getLast
Location().addOnSuccessListener(location -> { if (location !=
null) {
String url = "https://www.google.com/maps/dir/?
api=1&origin=" +
location.getLatitude() + "," +
location.getLongitude() +
"&destination=MSBTE+Bandra";
webView.loadUrl(url);
}});
}
}
✅ AndroidManifest.xml (

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

8. Develop a program to convert Text to Speech.

✅ activity_main.xml
xml
CopyEdit
<?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:padding="16dp"
android:orientation="vertical">

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

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

✅ MainActivity.java
java
CopyEdit
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.Locale;

public class MainActivity extends Activity {

TextToSpeech tts;
EditText editText;
Button button;

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

editText = findViewById(R.id.editText);
button = findViewById(R.id.button);

tts = new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() {


@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.US);
}
}
});

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = editText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
}
});
}

@Override
protected void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}

9. Write a program to demonstrate Date and Time picker.

✅ activity_main.xml (2 Marks)
xml
CopyEdit
<?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:gravity="center"
android:padding="16dp">

<Button
android:id="@+id/btnDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Date" />

<Button
android:id="@+id/btnTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Time"
android:layout_marginTop="10dp" />

</LinearLayout>

✅ MainActivity.java (3 Marks)
java
CopyEdit
package com.example.datetimepicker;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import java.util.Calendar;

public class MainActivity extends Activity {


Button btnDate, btnTime;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnDate = findViewById(R.id.btnDate);
btnTime = findViewById(R.id.btnTime);

btnDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Calendar c = Calendar.getInstance();
DatePickerDialog dp = new DatePickerDialog(MainActivity.this,
null, c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
dp.show();
}
});

btnTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Calendar c = Calendar.getInstance();
TimePickerDialog tp = new TimePickerDialog(MainActivity.this,
null, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), false);
tp.show();
}
});
}
}

10.Write a program to demonstrate declaring and using permissions (with relevant example).
Java
package com.example.permissiondemo;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {

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

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


btn.setOnClickListener(v -> {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
Toast.makeText(this, "Permission Already Granted", Toast.LENGTH_SHORT).show();
}
});
}

@Override
public void onRequestPermissionsResult(int reqCode, String[] permissions, int[] results) {
if (reqCode == 1 && results.length > 0) {
String msg = (results[0] == PackageManager.PERMISSION_GRANTED) ?
"Permission Granted" : "Permission Denied";
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}
}

Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">

<Button
android:id="@+id/btn"
android:text="Request Permission"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

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

11.Write a program to convert temperature from Celsius to Fahrenheit and vice versa using Toggle
button.

Java
package com.example.tempconverter;

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

public class MainActivity extends AppCompatActivity {

EditText inputTemp;
ToggleButton toggle;
TextView result;

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

inputTemp = findViewById(R.id.inputTemp);
toggle = findViewById(R.id.toggle);
result = findViewById(R.id.result);

toggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String value = inputTemp.getText().toString();
if (!value.isEmpty()) {
double temp = Double.parseDouble(value);
if (toggle.isChecked()) {
// Convert Celsius to Fahrenheit
double f = (temp * 9 / 5) + 32;
result.setText("Fahrenheit: " + f);
} else {
// Convert Fahrenheit to Celsius
double c = (temp - 32) * 5 / 9;
result.setText("Celsius: " + c);
}
} else {
result.setText("Enter Temperature");
}
}
});
}
}

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

<EditText
android:id="@+id/inputTemp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter temperature"
android:inputType="numberDecimal" />

<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="C to F"
android:textOff="F to C" />

<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Result here" />
</LinearLayout>

12.Write a program to capture an image using camera and display it.

activity_main.xml
xml
CopyEdit
<?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"
android:paddingLeft="10dp"
android:paddingRight="10dp">

<Button
android:id="@+id/btnTakePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take a Photo"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />

<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/capturedImage"
android:layout_above="@+id/btnTakePicture"/>
</RelativeLayout>

MainActivity.java
java
CopyEdit
package com.tutlane.cameraexample;

import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button btnCapture;
private ImageView imgCapture;
private static final int Image_Capture_Code = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCapture = (Button)findViewById(R.id.btnTakePicture);
imgCapture = (ImageView) findViewById(R.id.capturedImage);

btnCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cInt, Image_Capture_Code);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Image_Capture_Code) {
if (resultCode == RESULT_OK) {
Bitmap bp = (Bitmap) data.getExtras().get("data");
imgCapture.setImageBitmap(bp);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
}
}
}
}

13.Write a program to implement Android Activity Life Cycle using toast messages.
Java
package com.example.lifecycle;

import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
}

@Override
protected void onStart() {
super.onStart();
Toast.makeText(this, "onStart", Toast.LENGTH_SHORT).show();
}

@Override
protected void onResume() {
super.onResume();
Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
}

@Override
protected void onPause() {
super.onPause();
Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
}

@Override
protected void onStop() {
super.onStop();
Toast.makeText(this, "onStop", Toast.LENGTH_SHORT).show();
}

@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(this, "onRestart", Toast.LENGTH_SHORT).show();
}

@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
}
}

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:gravity="center"
android:orientation="vertical">

<TextView
android:text="Activity Life Cycle Example"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

14.Develop an application to display Google Map with user's current location.

Java
package com.example.maplocation;

import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import com.google.android.gms.location.*;
import android.Manifest;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {


GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mf = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mf.getMapAsync(this);
}

public void onMapReady(GoogleMap googleMap) {


map = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
else
map.setMyLocationEnabled(true);
}
}
Xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maplocation">

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


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

<application
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:label="Map Location">

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY_HERE" />

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

15. Design UI using TableLayout to display 0-9 number buttons, submit and clear button, and show

clicked numbers on Java

package com.example.tableinput;

import android.os.Bundle;

import android.view.View;

import android.widget.*;

import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {

StringBuilder input;

TextView display;

@Override

protected void onCreate(Bundle b) {

super.onCreate(b);

setContentView(R.layout.activity_main);

input = new StringBuilder();

display = findViewById(R.id.display);

int[] ids = {R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4,

R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9};

View.OnClickListener numberClickListener = new View.OnClickListener() {

@Override

public void onClick(View v) {

Button btn = (Button) v;

input.append(btn.getText().toString());

};

for (int id : ids) {

Button btn = findViewById(id);

btn.setOnClickListener(numberClickListener);
}

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

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

submit.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

display.setText("Clicked: " + input.toString());

});

clear.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

input.setLength(0);

display.setText("Clicked: ");

});

Xml

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

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

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:stretchColumns="*"
android:padding="20dp">

<TextView

android:id="@+id/display"

android:text="Clicked: "

android:textSize="18sp"

android:padding="10dp"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

<TableRow>

<Button android:id="@+id/btn1" android:text="1" />

<Button android:id="@+id/btn2" android:text="2" />

<Button android:id="@+id/btn3" android:text="3" />

</TableRow>

<TableRow>

<Button android:id="@+id/btn4" android:text="4" />

<Button android:id="@+id/btn5" android:text="5" />

<Button android:id="@+id/btn6" android:text="6" />

</TableRow>

<TableRow>

<Button android:id="@+id/btn7" android:text="7" />

<Button android:id="@+id/btn8" android:text="8" />

<Button android:id="@+id/btn9" android:text="9" />

</TableRow>
<TableRow>

<Button android:id="@+id/btn0" android:text="0" />

<Button android:id="@+id/submit" android:text="Submit" />

<Button android:id="@+id/clear" android:text="Clear" />

</TableRow>

</TableLayout>

16.Develop an Android application using RadioButton.


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

<RadioGroup android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:id="@+id/opt1" android:text="Option A"/>
<RadioButton android:id="@+id/opt2" android:text="Option B"/>
</RadioGroup>

<Button android:id="@+id/submit" android:text="Submit"


android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

Java
package com.example.radiodemo;

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

public class MainActivity extends AppCompatActivity {


protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);

RadioGroup group = findViewById(R.id.group);


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

submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int id = group.getCheckedRadioButtonId();
if (id != -1) {
RadioButton selected = findViewById(id);
Toast.makeText(MainActivity.this, selected.getText(), Toast.LENGTH_SHORT).show();
}
}
});
}
}

17.Develop an application to perform addition, subtraction, multiplication, and division of two numbers.

18.Develop an application to convert 'thanks' text to speech.

java
package com.example.ttsdemo;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {


TextToSpeech tts;

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

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

tts = new TextToSpeech(this, status -> {


if (status == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.US);
}
});

speakBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tts.speak("thanks", TextToSpeech.QUEUE_FLUSH, null, null);
}
});
}

@Override
protected void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}

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:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/speakBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLICK TO SPEAK 'thanks'" />
</LinearLayout>

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

19. Develop an application to update a record in SQLite where emp.id is 'E101'. Change name and

show updated record.

✅ activity_main.
xml
CopyEdit
<?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="10dp">

<EditText
android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter New Name" />

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

<TextView
android:id="@+id/textResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result will appear here" />
</LinearLayout>

✅ MainActivity.java
java
CopyEdit
package com.example.empdb;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends Activity {


EditText editName;
Button btnUpdate;
TextView textResult;
DBHelper dbHelper;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editName = findViewById(R.id.editName);
btnUpdate = findViewById(R.id.btnUpdate);
textResult = findViewById(R.id.textResult);
dbHelper = new DBHelper(this);

btnUpdate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name = editName.getText().toString();
dbHelper.updateName(name);
String result = dbHelper.getData();
textResult.setText(result);
}
});
}
}

✅ DBHelper.java
java
CopyEdit
package com.example.empdb;

import android.content.*;
import android.database.Cursor;
import android.database.sqlite.*;

public class DBHelper extends SQLiteOpenHelper {

public DBHelper(Context context) {


super(context, "EmpDB", null, 1);
}

public void onCreate(SQLiteDatabase db) {


db.execSQL("CREATE TABLE emp(id TEXT PRIMARY KEY, name TEXT)");
db.execSQL("INSERT INTO emp VALUES('E101', 'OldName')");
}

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

public void updateName(String newName) {


SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("UPDATE emp SET name = '" + newName + "' WHERE id = 'E101'");
}

public String getData() {


SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM emp WHERE id = 'E101'", null);
if (c.moveToFirst()) {
return "ID: " + c.getString(0) + "\nName: " + c.getString(1);
}
return "No Record Found";
}
}
20.Develop a program to TURN ON and OFF Bluetooth.

Java
package com.example.bluetoothapp;

import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


BluetoothAdapter bluetooth;

protected void onCreate(Bundle b) {


super.onCreate(b);

bluetooth = BluetoothAdapter.getDefaultAdapter();

Button on = new Button(this);


on.setText("Turn ON");
on.setOnClickListener(v -> {
if (!bluetooth.isEnabled())
bluetooth.enable();
});

Button off = new Button(this);


off.setText("Turn OFF");
off.setOnClickListener(v -> {
if (bluetooth.isEnabled())
bluetooth.disable();
});

LinearLayout layout = new LinearLayout(this);


layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(on);
layout.addView(off);

setContentView(layout);
}
}
Manifest
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

21.Write a program to create first screen of a search engine using AutoCompleteTextView.

Java
package com.example.autocompletetest;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


String[] suggestions = {"Apple", "Banana", "Cherry", "Date", "Fig", "Grapes"};

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

AutoCompleteTextView autoText = findViewById(R.id.autoText);


ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, suggestions);
autoText.setAdapter(adapter);
}
}

Xml
<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="20dp">

<AutoCompleteTextView
android:id="@+id/autoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type here"
android:completionThreshold="1" />
</LinearLayout>

22.Develop Android application to enter a number and display its factorial on button click.

package com.example.factorialapp;

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

public class MainActivity extends AppCompatActivity {


EditText number;
Button calc;
TextView result;

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

number = findViewById(R.id.number);
calc = findViewById(R.id.calc);
result = findViewById(R.id.result);

calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
int num = Integer.parseInt(number.getText().toString());
long fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
result.setText("Factorial: " + fact);
});
}
}

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

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

<Button
android:id="@+id/calc"
android:text="Find Factorial"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/result"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"/>
</LinearLayout>

23.Write a program to show user's current location.

Java
package com.example.maplocation;

import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import com.google.android.gms.location.*;
import android.Manifest;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {


GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mf = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mf.getMapAsync(this);
}

public void onMapReady(GoogleMap googleMap) {


map = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
else
map.setMyLocationEnabled(true);
}
}
Xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maplocation">

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


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

24.Write a program to list all sensors supported by device.

25.Write a program to send email.

26.Write a program to show five CheckBoxes and toast selected ones using LinearLayout.

Java
package com.example.checkboxdemo;

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

public class MainActivity extends AppCompatActivity {

CheckBox cb1, cb2, cb3, cb4, cb5;


Button btn;

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

cb1 = findViewById(R.id.cb1);
cb2 = findViewById(R.id.cb2);
cb3 = findViewById(R.id.cb3);
cb4 = findViewById(R.id.cb4);
cb5 = findViewById(R.id.cb5);
btn = findViewById(R.id.btnShow);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder result = new StringBuilder("Selected: ");
if (cb1.isChecked()) result.append(cb1.getText()).append(" ");
if (cb2.isChecked()) result.append(cb2.getText()).append(" ");
if (cb3.isChecked()) result.append(cb3.getText()).append(" ");
if (cb4.isChecked()) result.append(cb4.getText()).append(" ");
if (cb5.isChecked()) result.append(cb5.getText());

Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_SHORT).show();


}
});
}
}

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

<CheckBox android:id="@+id/cb1" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:text="Option 1" />

<CheckBox android:id="@+id/cb2" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:text="Option 2" />

<CheckBox android:id="@+id/cb3" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:text="Option 3" />

<CheckBox android:id="@+id/cb4" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:text="Option 4" />

<CheckBox android:id="@+id/cb5" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:text="Option 5" />

<Button android:id="@+id/btnShow" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:text="Show Selected" />
</LinearLayout>

27.Develop Android app for student mark sheet using TableLayout (5 subjects, total, percentage).

xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:stretchColumns="1">

<TableRow>
<TextView android:text="Subject" />
<TextView android:text="Marks" />
</TableRow>

<TableRow>
<TextView android:text="Math" />
<EditText android:id="@+id/m1" android:inputType="number" />
</TableRow>

<TableRow>
<TextView android:text="Science" />
<EditText android:id="@+id/m2" android:inputType="number" />
</TableRow>

<TableRow>
<TextView android:text="English" />
<EditText android:id="@+id/m3" android:inputType="number" />
</TableRow>
<TableRow>
<TextView android:text="History" />
<EditText android:id="@+id/m4" android:inputType="number" />
</TableRow>

<TableRow>
<TextView android:text="Geography" />
<EditText android:id="@+id/m5" android:inputType="number" />
</TableRow>

<TableRow>
<Button
android:id="@+id/calc"
android:text="Calculate"
android:layout_span="2" />
</TableRow>

<TableRow>
<TextView android:text="Total" />
<TextView android:id="@+id/total" />
</TableRow>

<TableRow>
<TextView android:text="Percentage" />
<TextView android:id="@+id/percent" />
</TableRow>
</TableLayout>

java
package com.example.marksheet;

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

public class MainActivity extends AppCompatActivity {

EditText m1, m2, m3, m4, m5;


TextView total, percent;
Button calc;

protected void onCreate(Bundle b) {


super.onCreate(b);
setContentView(R.layout.activity_main);

m1 = findViewById(R.id.m1);
m2 = findViewById(R.id.m2);
m3 = findViewById(R.id.m3);
m4 = findViewById(R.id.m4);
m5 = findViewById(R.id.m5);
total = findViewById(R.id.total);
percent = findViewById(R.id.percent);
calc = findViewById(R.id.calc);

calc.setOnClickListener(v -> {
int a = Integer.parseInt(m1.getText().toString());
int b1 = Integer.parseInt(m2.getText().toString());
int c = Integer.parseInt(m3.getText().toString());
int d = Integer.parseInt(m4.getText().toString());
int e = Integer.parseInt(m5.getText().toString());

int t = a + b1 + c + d + e;
float p = t / 5f;

total.setText(String.valueOf(t));
percent.setText(p + "%");
});
}
}

28.Develop an application to store and retrieve student details using roll number in SQLite.

✅ activity_main.xml (2 Marks)
xml
CopyEdit
<?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="10dp">

<EditText
android:id="@+id/editRoll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Roll Number" />

<EditText
android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Student Name" />

<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save" />

<Button
android:id="@+id/btnRetrieve"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Retrieve" />

<TextView
android:id="@+id/textResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>

✅ MainActivity.java (3 Marks including DBHelper)


java
CopyEdit
package com.example.studentdb;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends Activity {

EditText editRoll, editName;


Button btnSave, btnRetrieve;
TextView textResult;
StudentDBHelper db;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editRoll = findViewById(R.id.editRoll);
editName = findViewById(R.id.editName);
btnSave = findViewById(R.id.btnSave);
btnRetrieve = findViewById(R.id.btnRetrieve);
textResult = findViewById(R.id.textResult);
db = new StudentDBHelper(this);

btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
db.insertStudent(editRoll.getText().toString(),
editName.getText().toString());
Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
}
});

btnRetrieve.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Cursor c = db.getStudent(editRoll.getText().toString());
if (c.moveToFirst()) {
textResult.setText("Name: " + c.getString(1));
} else {
textResult.setText("No record found");
}
}
});
}
}

✅ StudentDBHelper.java (Part of 3 Marks)


java
CopyEdit
package com.example.studentdb;

import android.content.*;
import android.database.Cursor;
import android.database.sqlite.*;

public class StudentDBHelper extends SQLiteOpenHelper {

public StudentDBHelper(Context context) {


super(context, "StudentDB", null, 1);
}

public void onCreate(SQLiteDatabase db) {


db.execSQL("CREATE TABLE student(roll TEXT PRIMARY KEY, name TEXT)");
}

public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {}

public void insertStudent(String roll, String name) {


SQLiteDatabase db = getWritableDatabase();
ContentValues v = new ContentValues();
v.put("roll", roll);
v.put("name", name);
db.insert("student", null, v);
}

public Cursor getStudent(String roll) {


SQLiteDatabase db = getReadableDatabase();
return db.rawQuery("SELECT * FROM student WHERE roll = ?", new String[]{roll});
}
}
29.Write a program to demonstrate GridView and ImageView.

java
package com.example.simplegrid;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);

GridView grid = findViewById(R.id.grid);


String[] items = {"A", "B", "C", "D", "E", "F"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
items);
grid.setAdapter(adapter);
}
}

Xml
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:padding="20dp" />

Imageview java
package com.example.simpleimage;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
}
}
Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/ic_launcher_foreground" />
</LinearLayout>

30.Design a UI for employee registration form using components.

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="20dp">

<EditText
android:hint="Employee Name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:hint="Employee ID"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:hint="Email"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:hint="Phone Number"
android:inputType="phone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:hint="Department"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:text="Register"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

31.Develop Android app for student feedback with DB connectivity.

✅ activity_main.xml (Minimal – 2 Marks)


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

<EditText
android:id="@+id/editname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />

<EditText
android:id="@+id/editclass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Class" />

<EditText
android:id="@+id/editfeedback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Feedback" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>

✅ MainActivity.java (Simple Old Style – 3 Marks)


java
CopyEdit
package com.example.feedback;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.database.sqlite.SQLiteDatabase;

public class MainActivity extends Activity {

EditText name, stdClass, feedback;


Button btn;
SQLiteDatabase db;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

name = findViewById(R.id.editname);
stdClass = findViewById(R.id.editclass);
feedback = findViewById(R.id.editfeedback);
btn = findViewById(R.id.button);

db = openOrCreateDatabase("StudentDB", MODE_PRIVATE, null);


db.execSQL("CREATE TABLE IF NOT EXISTS Feedback(name TEXT, class TEXT, feedback
TEXT)");

btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String n = name.getText().toString();
String c = stdClass.getText().toString();
String f = feedback.getText().toString();

db.execSQL("INSERT INTO Feedback VALUES('" + n + "', '" + c + "', '" + f


+ "')");
Toast.makeText(MainActivity.this, "Feedback Saved",
Toast.LENGTH_SHORT).show();
}
});
}
}

32.Design an app to show list of paired Bluetooth devices.

✅ activity_main.xml (2 Marks)
xml
CopyEdit
<?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">

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

✅ AndroidManifest.xml (1 Mark)
xml
CopyEdit
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

✅ MainActivity.java (3 Marks)
java
CopyEdit
package com.example.bluetoothlist;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Set;

public class MainActivity extends Activity {

ListView listView;
BluetoothAdapter bluetoothAdapter;

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

listView = findViewById(R.id.listView);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {


Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
ArrayList<String> deviceList = new ArrayList<>();

for (BluetoothDevice device : pairedDevices) {


deviceList.add(device.getName() + "\n" + device.getAddress());
}

listView.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, deviceList));
}
}
}
33.Develop Android app to send SMS (Write XML, Java, Manifest).

🔹 activity_main.xml (2 Marks)
xml
CopyEdit
<?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="20dp">

<EditText
android:id="@+id/etNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Number"
android:inputType="phone" />

<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Message" />

<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS" />
</LinearLayout>

🔹 MainActivity.java (3 Marks)
java
CopyEdit
package com.example.sendsms;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity {


EditText etPhone, etMessage;
Button btnSend;

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

etPhone = findViewById(R.id.etPhone);
etMessage = findViewById(R.id.etMessage);
btnSend = findViewById(R.id.btnSend);

ActivityCompat.requestPermissions(this, new String[]


{Manifest.permission.SEND_SMS}, 1);

btnSend.setOnClickListener(v -> {
String phone = etPhone.getText().toString();
String msg = etMessage.getText().toString();
SmsManager.getDefault().sendTextMessage(phone, null, msg, null, null);
Toast.makeText(this, "SMS Sent", Toast.LENGTH_SHORT).show();
});
}
}

🔹 Minimal AndroidManifest.xml
xml
CopyEdit
<uses-permission android:name="android.permission.SEND_SMS" />

1. Develop an application to store customer's details (ID, Name, Mobile No., Address, Pin-code) and

retrieve data using SQLite.

✅ activity_main.xml (2M, now includes ID)


xml
CopyEdit
<?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="10dp">

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


android:layout_height="wrap_content" android:hint="Customer ID"/>
<EditText android:id="@+id/etName" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Name"/>
<EditText android:id="@+id/etMobile" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Mobile"/>
<EditText android:id="@+id/etAddress" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Address"/>
<EditText android:id="@+id/etPincode" android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Pincode"/>

<Button android:id="@+id/btnInsert" android:layout_width="match_parent"


android:layout_height="wrap_content" android:text="Insert"/>
<Button android:id="@+id/btnShow" android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Show All"/>
<ListView android:id="@+id/listView" android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

✅ DatabaseHelper.java (1.5M — no changes needed unless you want to insert with custom ID)
java
CopyEdit
package com.example.customerdb;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.*;

public class DatabaseHelper extends SQLiteOpenHelper {

public DatabaseHelper(Context context) {


super(context, "custdb", null, 1);
}

public void onCreate(SQLiteDatabase db) {


db.execSQL("CREATE TABLE customer(id INTEGER PRIMARY KEY AUTOINCREMENT, name
TEXT, mobile TEXT, address TEXT, pincode TEXT)");
}

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

public void insert(String name, String mobile, String address, String pincode) {
getWritableDatabase().execSQL("INSERT INTO customer(name, mobile, address,
pincode) VALUES('" + name + "','" + mobile + "','" + address + "','" + pincode + "')");
}

public Cursor getData() {


return getReadableDatabase().rawQuery("SELECT * FROM customer", null);
}
}

✅ MainActivity.java (1.5M — now handles ID field)


java
CopyEdit
package com.example.customerdb;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends Activity {

EditText etId, etName, etMobile, etAddress, etPincode;


ListView listView;
DatabaseHelper db;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

etId = findViewById(R.id.etId);
etName = findViewById(R.id.etName);
etMobile = findViewById(R.id.etMobile);
etAddress = findViewById(R.id.etAddress);
etPincode = findViewById(R.id.etPincode);
listView = findViewById(R.id.listView);
db = new DatabaseHelper(this);

findViewById(R.id.btnInsert).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
db.insert(etName.getText().toString(), etMobile.getText().toString(),
etAddress.getText().toString(), etPincode.getText().toString());
}
});

findViewById(R.id.btnShow).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Cursor c = db.getData();
StringBuilder sb = new StringBuilder();
while (c.moveToNext()) {
sb.append("ID: ").append(c.getInt(0)).append("\n")
.append("Name: ").append(c.getString(1)).append("\n")
.append("Mobile: ").append(c.getString(2)).append("\n")
.append("Address: ").append(c.getString(3)).append("\n")
.append("Pincode: ").append(c.getString(4)).append("\n\n");
}
String[] arr = sb.toString().split("\n\n");
listView.setAdapter(new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1, arr));
}
});
}
}

1. Develop an application to update a record in SQLite where emp.id is 'E101'. Change name and

show updated record.

✅ activity_main.
xml
CopyEdit
<?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="10dp">

<EditText
android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter New Name" />

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

<TextView
android:id="@+id/textResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result will appear here" />
</LinearLayout>

✅ MainActivity.java
java
CopyEdit
package com.example.empdb;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends Activity {

EditText editName;
Button btnUpdate;
TextView textResult;
DBHelper dbHelper;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editName = findViewById(R.id.editName);
btnUpdate = findViewById(R.id.btnUpdate);
textResult = findViewById(R.id.textResult);
dbHelper = new DBHelper(this);

btnUpdate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name = editName.getText().toString();
dbHelper.updateName(name);
String result = dbHelper.getData();
textResult.setText(result);
}
});
}
}

✅ DBHelper.java
java
CopyEdit
package com.example.empdb;

import android.content.*;
import android.database.Cursor;
import android.database.sqlite.*;

public class DBHelper extends SQLiteOpenHelper {

public DBHelper(Context context) {


super(context, "EmpDB", null, 1);
}

public void onCreate(SQLiteDatabase db) {


db.execSQL("CREATE TABLE emp(id TEXT PRIMARY KEY, name TEXT)");
db.execSQL("INSERT INTO emp VALUES('E101', 'OldName')");
}

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

public void updateName(String newName) {


SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("UPDATE emp SET name = '" + newName + "' WHERE id = 'E101'");
}

public String getData() {


SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM emp WHERE id = 'E101'", null);
if (c.moveToFirst()) {
return "ID: " + c.getString(0) + "\nName: " + c.getString(1);
}
return "No Record Found";
}
}
1. Develop an application to store and retrieve student details using roll number in SQLite.

✅ activity_main.xml (2 Marks)
xml
CopyEdit
<?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="10dp">

<EditText
android:id="@+id/editRoll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Roll Number" />

<EditText
android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Student Name" />

<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save" />

<Button
android:id="@+id/btnRetrieve"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Retrieve" />

<TextView
android:id="@+id/textResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>

✅ MainActivity.java (3 Marks including DBHelper)


java
CopyEdit
package com.example.studentdb;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends Activity {

EditText editRoll, editName;


Button btnSave, btnRetrieve;
TextView textResult;
StudentDBHelper db;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editRoll = findViewById(R.id.editRoll);
editName = findViewById(R.id.editName);
btnSave = findViewById(R.id.btnSave);
btnRetrieve = findViewById(R.id.btnRetrieve);
textResult = findViewById(R.id.textResult);
db = new StudentDBHelper(this);

btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
db.insertStudent(editRoll.getText().toString(),
editName.getText().toString());
Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show();
}
});

btnRetrieve.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Cursor c = db.getStudent(editRoll.getText().toString());
if (c.moveToFirst()) {
textResult.setText("Name: " + c.getString(1));
} else {
textResult.setText("No record found");
}
}
});
}
}

✅ StudentDBHelper.java (Part of 3 Marks)


java
CopyEdit
package com.example.studentdb;

import android.content.*;
import android.database.Cursor;
import android.database.sqlite.*;

public class StudentDBHelper extends SQLiteOpenHelper {

public StudentDBHelper(Context context) {


super(context, "StudentDB", null, 1);
}

public void onCreate(SQLiteDatabase db) {


db.execSQL("CREATE TABLE student(roll TEXT PRIMARY KEY, name TEXT)");
}

public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {}

public void insertStudent(String roll, String name) {


SQLiteDatabase db = getWritableDatabase();
ContentValues v = new ContentValues();
v.put("roll", roll);
v.put("name", name);
db.insert("student", null, v);
}

public Cursor getStudent(String roll) {


SQLiteDatabase db = getReadableDatabase();
return db.rawQuery("SELECT * FROM student WHERE roll = ?", new String[]{roll});
}
}

1. Develop Android app for student feedback with DB connectivity.


✅ activity_main.xml (Minimal – 2 Marks)
xml
CopyEdit
<?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">

<EditText
android:id="@+id/editname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />

<EditText
android:id="@+id/editclass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Class" />

<EditText
android:id="@+id/editfeedback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Feedback" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>

✅ MainActivity.java (Simple Old Style – 3 Marks)


java
CopyEdit
package com.example.feedback;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.database.sqlite.SQLiteDatabase;

public class MainActivity extends Activity {

EditText name, stdClass, feedback;


Button btn;
SQLiteDatabase db;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

name = findViewById(R.id.editname);
stdClass = findViewById(R.id.editclass);
feedback = findViewById(R.id.editfeedback);
btn = findViewById(R.id.button);
db = openOrCreateDatabase("StudentDB", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Feedback(name TEXT, class TEXT, feedback
TEXT)");

btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String n = name.getText().toString();
String c = stdClass.getText().toString();
String f = feedback.getText().toString();

db.execSQL("INSERT INTO Feedback VALUES('" + n + "', '" + c + "', '" + f


+ "')");
Toast.makeText(MainActivity.this, "Feedback Saved",
Toast.LENGTH_SHORT).show();
}
});
}
}

You might also like