0% found this document useful (0 votes)
4 views

MAD Program

The document contains multiple Android application examples, including sending emails, displaying checkboxes, listing device sensors, capturing images, calculating factorials, and implementing an auto-complete text view. Each example includes XML layout files and corresponding Java code to demonstrate functionality. The applications cover a range of basic Android programming concepts and user interface elements.

Uploaded by

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

MAD Program

The document contains multiple Android application examples, including sending emails, displaying checkboxes, listing device sensors, capturing images, calculating factorials, and implementing an auto-complete text view. Each example includes XML layout files and corresponding Java code to demonstrate functionality. The applications cover a range of basic Android programming concepts and user interface elements.

Uploaded by

atharvchavan0911
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/ 61

1.

WAP to send e-mail

XML FILE:

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

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

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

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

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Email" />
</LinearLayout>
JAVA FILE:
package com.example.e_mail;

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

public class MainActivity extends AppCompatActivity {


Button button;
EditText sendto, subject, body;

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

sendto = findViewById(R.id.editText1);
subject = findViewById(R.id.editText2);
body = findViewById(R.id.editText3);
button = findViewById(R.id.button);

button.setOnClickListener(view -> {
String emailsend = sendto.getText().toString();
String emailsubject = subject.getText().toString();
String emailbody = body.getText().toString();

Intent intent = new Intent(Intent.ACTION_SEND);


intent.putExtra(Intent.EXTRA_EMAIL, new String[]{emailsend});
intent.putExtra(Intent.EXTRA_SUBJECT, emailsubject);
intent.putExtra(Intent.EXTRA_TEXT, emailbody);
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Choose an Email client:"));
});
}
}
MANIFEST FILE:
<uses-permission android:name="android.permission.INTERNET"/>

2. Write a program to show five checkboxes and total selected


checkboxes using linearlayout.

XML FILE:

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


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

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

<CheckBox
android:id="@+id/c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Swimming" />

<CheckBox
android:id="@+id/c2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Running" />

<CheckBox
android:id="@+id/c3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cycling" />

<CheckBox
android:id="@+id/c4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reading" />

<CheckBox
android:id="@+id/c5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Football" />

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

</LinearLayout>

JAVAFILE:

package com.example.checkbox;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


Button b1;
CheckBox c1, c2, c3, c4, c5;

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

b1 = findViewById(R.id.b1);
c1 = findViewById(R.id.c1);
c2 = findViewById(R.id.c2);
c3 = findViewById(R.id.c3);
c4 = findViewById(R.id.c4);
c5 = findViewById(R.id.c5);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = 0; // Initialize count

if (c1.isChecked()) count++;
if (c2.isChecked()) count++;
if (c3.isChecked()) count++;
if (c4.isChecked()) count++;
if (c5.isChecked()) count++;

Toast.makeText(MainActivity.this, "Total selected: " + count,


Toast.LENGTH_SHORT).show();
}
});
}
}

3. Write a program to display the list of sensors supported by device.

XML FILE:
<?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">

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

</LinearLayout>

JAVAFILE:

package com.example.sensor;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

public class MainActivity extends AppCompatActivity {


private SensorManager sensorManager;
private TextView txtList;

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

sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);


txtList = findViewById(R.id.sensorslist);

List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);


StringBuilder strBuilder = new StringBuilder();

for (Sensor sensor : sensorList) {


strBuilder.append(sensor.getName()).append("\n");
}

txtList.setText(strBuilder.toString());
}
}

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

OR
24.write a program to demonstrate declaring and using permissions
with any relevant example.

XML FILE:

<?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:padding="40dp">

<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CAMERA"
android:textSize="20sp"
android:gravity="center" />

<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text"
android:layout_marginTop="20dp"
android:contentDescription="Captured Image" />

<Button
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/image"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="TAKE PHOTO" />
</RelativeLayout>

JAVAFILE:

package com.example.camera;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private Button b1;
private ImageView imageView;
private static final int CAMERA_REQUEST = 1;

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

b1 = findViewById(R.id.photo);
imageView = findViewById(R.id.image);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK && data != null) {
Bitmap image = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(image);
}
}
}

MANIFEST FILE:
<uses-permission android:name="android.permission.CAMERA" />
5. Develop android application to enter one number and display
factorial of a number once click on button.

XML FILE:

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

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

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/number"
android:text="Calculate Factorial" />

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn1"
android:layout_marginTop="16dp" />
</RelativeLayout>

JAVAFILE:

package com.example.factorial;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


private EditText number;
private Button btn1;
private TextView tv
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

number = findViewById(R.id.number);
btn1 = findViewById(R.id.btn1);
tv = findViewById(R.id.tv);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strnum = number.getText().toString(); // Get input from EditText
if (!strnum.isEmpty()) { // Check if input is not empty
int num = Integer.parseInt(strnum); // Parse input to integer
long factorial = calculateFactorial(num); // Calculate factorial
tv.setText("Factorial: " + factorial); // Display result
} else {
tv.setText("Please enter a number"); // Prompt user for input
}
}
});
}

private long calculateFactorial(int num) {


long result = 1;
for (int i = 1; i <= num; i++) {
result *= i;
}
return result;
}
}

6. Write a program to create first display screen of any search engine


using auto complete text view.

XMLFILE:

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

<AutoCompleteTextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Text to search" />
</RelativeLayout>

JAVA FILE:

package com.example.autotext;

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", "orange", "grape", "kiwi"};

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

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


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

txt.setAdapter(adapter);
}
}

7. Design a student registration form.

XML FILE:

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


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

<TextView
android:id="@+id/formTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Registration Form"
android:textSize="24sp"
android:layout_gravity="center"
android:layout_marginBottom="20dp"/>

<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Full Name"
android:inputType="textPersonName"
android:layout_marginBottom="16dp"/>

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

<TextView
android:id="@+id/labelGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender:"
android:layout_marginBottom="8dp"/>

<RadioGroup
android:id="@+id/radioGroupGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="16dp">

<RadioButton
android:id="@+id/rbMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:layout_marginEnd="16dp"/>

<RadioButton
android:id="@+id/rbFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:layout_marginEnd="16dp"/>

<RadioButton
android:id="@+id/rbOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other"/>
</RadioGroup>

<TextView
android:id="@+id/labelCourse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Course:"
android:layout_marginBottom="8dp"/>

<Spinner
android:id="@+id/spinnerCourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp" />

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

</LinearLayout>

8. Develop a program to implement - List View of 6 item.

XMLFILE:

<?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
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

JAVAFILE:

package com.example.listview;

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

public class MainActivity extends AppCompatActivity {


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

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


String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
}
}

9. List UI component explain any one with help of example.


• TextView

• EditText

• Button
• ImageView

• ListView

• CheckBox

• RadioButton

• ProgressBar

XMLFILE:

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

<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
</LinearLayout>

JAVAFILE:

package com.example.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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


myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Button Clicked!", Toast.LENGTH_SHORT).show();
}
});
}
}

10. Develop a program for providing bluetooth connectivity.


MANIFESTFILE:

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

XMLFILE:

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="40dp"
android:orientation="horizontal"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bluetooth"
android:id="@+id/text"
android:textSize="20sp"
android:gravity="center"/>

<Button
android:id="@+id/on"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:layout_marginTop="62dp"
android:text="ON" />

<Button
android:id="@+id/discoverable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/on"
android:layout_marginTop="74dp"
android:text="DISCOVERABLE" />

<Button
android:id="@+id/off"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/discoverable"
android:layout_marginTop="104dp"
android:text="OFF" />
</RelativeLayout>

JAVAFILE:

package com.example.bluetooth;

import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


private BluetoothAdapter bluetoothAdapter;

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

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

Button on = findViewById(R.id.on);
Button discoverable = findViewById(R.id.discoverable);
Button off = findViewById(R.id.off);

on.setOnClickListener(v -> {
if (!bluetoothAdapter.isEnabled()) {
startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE),1);
}
});

discoverable.setOnClickListener(v -> {
if (!bluetoothAdapter.isDiscovering()) {
startActivityForResult(new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE), 2);
}
});

off.setOnClickListener(v -> bluetoothAdapter.disable());


}
}

11. Develop the android application for student marksheets using


table layout atleast five subject marks with total and percentage.
(Write both Java and xml code)

XMLFILE:

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


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

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Marksheet"
android:textSize="24sp"
android:layout_gravity="center"
android:layout_marginBottom="20dp" />

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<!-- Subject Rows -->


<TableRow>
<TextView android:text="Subject 1" />
<EditText
android:id="@+id/subject1_marks"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"
android:hint="Marks" />
</TableRow>
<TableRow>
<TextView android:text="Subject 2" />
<EditText
android:id="@+id/subject2_marks"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"
android:hint="Marks" />
</TableRow>
<TableRow>
<TextView android:text="Subject 3" />
<EditText
android:id="@+id/subject3_marks"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"
android:hint="Marks" />
</TableRow>
<TableRow>
<TextView android:text="Subject 4" />
<EditText
android:id="@+id/subject4_marks"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"
android:hint="Marks" />
</TableRow>
<TableRow>
<TextView android:text="Subject 5" />
<EditText
android:id="@+id/subject5_marks"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"
android:hint="Marks" />
</TableRow>

<!-- Total and Percentage -->


<TableRow>
<TextView android:text="Total Marks" />
<TextView
android:id="@+id/total_marks"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0" />
</TableRow>
<TableRow>
<TextView android:text="Percentage" />
<TextView
android:id="@+id/percentage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0%" />
</TableRow>
</TableLayout>

<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_gravity="center" />
</LinearLayout>

JAVAFILE:

package com.example.studentmarksheet;

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

public class MainActivity extends AppCompatActivity {


private EditText[] subjects = new EditText[5];
private TextView totalMarks, percentage;
private Button calculateButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
subjects[0] = findViewById(R.id.subject1_marks);
subjects[1] = findViewById(R.id.subject2_marks);
subjects[2] = findViewById(R.id.subject3_marks);
subjects[3] = findViewById(R.id.subject4_marks);
subjects[4] = findViewById(R.id.subject5_marks);

totalMarks = findViewById(R.id.total_marks);
percentage = findViewById(R.id.percentage);
calculateButton = findViewById(R.id.submit_button);

calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int total = 0;
try {
for (EditText subject : subjects) {
total += Integer.parseInt(subject.getText().toString());
}
double percentageValue = (total / 500.0) * 100;
totalMarks.setText(String.valueOf(total));
percentage.setText(String.format("%.2f%%", percentageValue));
} catch (NumberFormatException e) {
Toast.makeText(MainActivity.this, "Please enter valid marks",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

12. Develop an application to store student details like roll no, name,
marks and retrieve student information using roll no. in SQLite
database. (Write java andxml file).

XML FILE:

<?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/editRollNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Roll No"
android:inputType="number" />

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

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

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

<EditText
android:id="@+id/searchRollNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search Roll No"
android:inputType="number" />

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

<TextView
android:id="@+id/studentInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Student Info"
android:textSize="18sp"
android:gravity="center"
android:layout_marginTop="20dp" />

</LinearLayout>

JAVA FILE:

package com.example.sql;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


EditText editRollNo, editName, editMarks, searchRollNo;
TextView studentInfo;
SQLiteDatabase database;

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

editRollNo = findViewById(R.id.editRollNo);
editName = findViewById(R.id.editName);
editMarks = findViewById(R.id.editMarks);
searchRollNo = findViewById(R.id.searchRollNo);
studentInfo = findViewById(R.id.studentInfo);
Button saveButton = findViewById(R.id.saveButton);
Button searchButton = findViewById(R.id.searchButton);

database = openOrCreateDatabase("student.db", MODE_PRIVATE, null);


database.execSQL("CREATE TABLE IF NOT EXISTS students (roll_no TEXT PRIMARY KEY,
name TEXT, marks TEXT);");

saveButton.setOnClickListener(v -> {
String rollNo = editRollNo.getText().toString();
String name = editName.getText().toString();
String marks = editMarks.getText().toString();
if (!rollNo.isEmpty() && !name.isEmpty() && !marks.isEmpty()) {
ContentValues values = new ContentValues();
values.put("roll_no", rollNo);
values.put("name", name);
values.put("marks", marks);
long result = database.insert("students", null, values);
Toast.makeText(this, result == -1 ? "Failed to save" : "Saved",
Toast.LENGTH_SHORT).show();
clearFields();
} else {
Toast.makeText(this, "Fill all fields", Toast.LENGTH_SHORT).show();
}
});

searchButton.setOnClickListener(v -> {
String rollNo = searchRollNo.getText().toString();
if (!rollNo.isEmpty()) {
Cursor cursor = database.query("students", null, "roll_no = ?", new String[]{rollNo},
null, null, null);
if (cursor != null && cursor.moveToFirst()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String marks = cursor.getString(cursor.getColumnIndex("marks"));
studentInfo.setText("Roll No: " + rollNo + "\nName: " + name + "\nMarks: " +
marks);
cursor.close();
} else {
studentInfo.setText("Not found");
}
} else {
Toast.makeText(this, "Enter roll number", Toast.LENGTH_SHORT).show();
}
});
}

private void clearFields() {


editRollNo.setText("");
editName.setText("");
editMarks.setText("");
searchRollNo.setText("");
}

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

13. Develop a program to implement


1.list view of 5 item
2.grid view of 4*4 items
3.image View

XMLFILE:

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


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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List View"
android:textSize="20sp"
android:layout_marginBottom="8dp" />

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Grid View"
android:textSize="20sp"
android:layout_marginBottom="8dp" />

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Image View"
android:textSize="20sp"
android:layout_marginBottom="8dp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/ic_launcher_background"/>

</LinearLayout>

JAVAFILE:

package com.example.list;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;

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

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);
String[] gridItems = new String[16];
for (int i = 0; i < 16; i++) {
gridItems[i] = "Grid " + (i + 1);
}
ArrayAdapter<String> gridAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, gridItems);
gridView.setAdapter(gridAdapter);
}
}

14.Develop a simple calculator using relative layout

XML FILE:

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


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

<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0"
android:textSize="50sp"
android:padding="20dp"
android:gravity="end" />

<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:columnCount="4"
android:rowCount="5">

<!-- Buttons will be added programmatically -->


</GridLayout>
</LinearLayout>

JAVAFILE:

package com.example.calculator;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


private TextView display;
private String input = "";
private double num1 = 0;
private String operator = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = findViewById(R.id.t1);
GridLayout grid = findViewById(R.id.gridLayout);

// Define buttons for the calculator


String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "C", "=", "+"
};

// Create buttons and add them to the GridLayout


for (String btn : buttons) {
Button button = new Button(this);
button.setText(btn);
button.setOnClickListener(this::handleClick);
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.width = 0; // Set width to 0 for weight
params.height = GridLayout.LayoutParams.WRAP_CONTENT;
params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f); // Use weight
params.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f); // Use weight
button.setLayoutParams(params);
grid.addView(button);
}
}

private void handleClick(View v) {


String value = ((Button) v).getText().toString();
if (value.matches("[0-9]")) {
input += value; // Append number to input
} else if (value.equals("C")) {
input = ""; // Clear input
display.setText("0");
return;
} else if (value.matches("[+*/-]") && !input.isEmpty()) {
num1 = Double.parseDouble(input);
operator = value; // Set operator
input = ""; // Clear input for next number
} else if (value.equals("=") && !input.isEmpty()) {
input = computeResult(); // Compute result
}
display.setText(input.isEmpty() ? "0" : input); // Update display
}

private String computeResult() {


double num2 = Double.parseDouble(input);
switch (operator) {
case "+": return String.valueOf(num1 + num2);
case "-": return String.valueOf(num1 - num2);
case "*": return String.valueOf(num1 * num2);
case "/": return num2 != 0 ? String.valueOf(num1 / num2) : "Error";
default: return "";
}
}
}

15.Design a employee registration from using ui component

XML FILE:

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


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

<TextView
android:id="@+id/formTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Employee Registration Form"
android:textSize="24sp"
android:layout_gravity="center"
android:layout_marginBottom="20dp"/>

<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Full Name"
android:inputType="textPersonName"
android:layout_marginBottom="16dp"/>

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

<TextView
android:id="@+id/labelGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender:"
android:layout_marginBottom="8dp"/>

<RadioGroup
android:id="@+id/radioGroupGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="16dp">

<RadioButton
android:id="@+id/rbMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:layout_marginEnd="16dp"/>

<RadioButton
android:id="@+id/rbFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:layout_marginEnd="16dp"/>

<RadioButton
android:id="@+id/rbOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other"/>
</RadioGroup>

<TextView
android:id="@+id/labelDepartment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Department:"
android:layout_marginBottom="8dp"/>

<Spinner
android:id="@+id/spinnerDepartment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp" />

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

</LinearLayout>
16. develope an android application for taking student feedback with
data base connectivity

XML FILE:

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


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

<TextView
android:id="@+id/formTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Feedback Form"
android:textSize="24sp"
android:layout_gravity="center"
android:layout_marginBottom="20dp"/>

<EditText
android:id="@+id/etStudentName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Your Name"
android:layout_marginBottom="16dp"/>

<EditText
android:id="@+id/etFeedback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Your Feedback"
android:inputType="textMultiLine"
android:layout_marginBottom="16dp"
android:minLines="3"
android:maxLines="5"/>

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

</LinearLayout>

JAVAFILE:

package com.example.feedback;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
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 etStudentName, etFeedback;


private SQLiteDatabase database;

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

etStudentName = findViewById(R.id.etStudentName);
etFeedback = findViewById(R.id.etFeedback);
Button btnSubmit = findViewById(R.id.btnSubmit);

database = openOrCreateDatabase("feedback.db", MODE_PRIVATE, null);


database.execSQL("CREATE TABLE IF NOT EXISTS feedback (ID INTEGER PRIMARY KEY
AUTOINCREMENT, NAME TEXT, FEEDBACK TEXT)");

btnSubmit.setOnClickListener(v -> submitFeedback());


}
private void submitFeedback() {
String name = etStudentName.getText().toString();
String feedback = etFeedback.getText().toString();

if (name.isEmpty() || feedback.isEmpty()) {
Toast.makeText(this, "Please fill in all fields", Toast.LENGTH_SHORT).show();
return;
}

ContentValues contentValues = new ContentValues();


contentValues.put("NAME", name);
contentValues.put("FEEDBACK", feedback);
long result = database.insert("feedback", null, contentValues);

if (result != -1) {
Toast.makeText(this, "Feedback Submitted", Toast.LENGTH_SHORT).show();
etStudentName.setText("");
etFeedback.setText("");
} else {
Toast.makeText(this, "Error Submitting Feedback", Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if (database != null) {
database.close();
}
}
}
17.Design an android application to show the list of paired
devices by Bluetooth

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:layout_marginTop="200dp"
android:padding="16dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paired Bluetooth Devices"
android:textSize="24sp"
android:layout_gravity="center"
android:layout_marginBottom="20dp"/>

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

</LinearLayout>

JAVAFILE:

package com.example.listblutooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Set;

public class MainActivity extends AppCompatActivity {

private ListView listViewDevices;


private BluetoothAdapter bluetoothAdapter;

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

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

showPairedDevices();
}
private void showPairedDevices() {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1);

if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
adapter.add(device.getName() + "\n" + device.getAddress());
}
} else {
adapter.add("No paired devices found.");
}

listViewDevices.setAdapter(adapter);
}
}

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

18. develope a program to Add "Hello Word " marker at(10,10)


coordinates. write only java file

JAVA FILE:
package com.example.hello;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new HelloWorldView(this));
}

private class HelloWorldView extends View {


private Paint paint;

public HelloWorldView(Context context) {


super(context);
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(50); // Set text size
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText("Hello World", 10, 10, paint); // Draw "Hello World" at (10,
10)
}
}
}
19.Write an xml file to create login page using table layout.

XML FILE:

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

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TableRow>
<TextView
android:text="Username:"/>
<EditText
android:id="@+id/username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter Username"/>
</TableRow>

<TableRow>
<TextView
android:text="Password:"/>

<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter Password"
android:inputType="textPassword"/>
</TableRow>

<TableRow>
<Button
android:id="@+id/login_button"
android:text="Login"/>
</TableRow>

</TableLayout>
</LinearLayout>
20.Develop an android application using radio button.

XML FILE:

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

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RadioButton
android:id="@+id/apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple" />

<RadioButton
android:id="@+id/banana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Banana" />

<RadioButton
android:id="@+id/orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Orange" />
</RadioGroup>

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

<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textSize="18sp" />
</LinearLayout>

JAVAFILE:

package com.example.radiobutton;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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


Button submitButton = findViewById(R.id.submitButton);
TextView resultTextView = findViewById(R.id.resultTextView);

submitButton.setOnClickListener(v -> {
int selectedId = radioGroup.getCheckedRadioButtonId();
RadioButton selectedRadioButton = findViewById(selectedId);
resultTextView.setText(selectedRadioButton != null ?
selectedRadioButton.getText() : "None");
});

}
}

21.Develope an application to convert “thanks” text to speech


as given in the following GUI.

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Text to Speech (TTS) Demo"
android:textSize="24sp"
android:layout_gravity="center"
android:paddingBottom="16dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="thanks"
android:textSize="32sp"
android:layout_gravity="center"
android:paddingBottom="16dp"/>

<Button
android:id="@+id/speakButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLICK TO CONVERT TEXT TO SPEECH"
android:layout_gravity="center"/>
</LinearLayout>

JAVA FILE:

package com.example.speech;

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 {

private TextToSpeech textToSpeech;

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

textToSpeech = new TextToSpeech(this, status -> {


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

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


speakButton.setOnClickListener(v -> textToSpeech.speak("thanks",
TextToSpeech.QUEUE_FLUSH, null, null));
}

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

22.Develope a program to Turn ON and OFF Bluetooth


write .java file and permission tags.

XML FILE

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

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Bluetooth ON"
android:textOff="Bluetooth OFF" />
</LinearLayout>
JAVA FILE:

package com.example.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

private BluetoothAdapter bluetoothAdapter;


private ToggleButton toggleButton;
private static final int REQUEST_BLUETOOTH_PERMISSION = 1;

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

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

if (bluetoothAdapter == null) {
showToast("Bluetooth not supported");
finish();
return;
}

toggleButton.setChecked(bluetoothAdapter.isEnabled());
toggleButton.setOnCheckedChangeListener(this::onToggleChanged);
}

private void onToggleChanged(CompoundButton buttonView, boolean isChecked) {


if (isChecked) {
if (checkSelfPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
== PackageManager.PERMISSION_GRANTED) {
bluetoothAdapter.enable();
showToast("Bluetooth ON");
} else {
requestPermissions(new String[]
{android.Manifest.permission.BLUETOOTH_CONNECT},
REQUEST_BLUETOOTH_PERMISSION);
toggleButton.setChecked(false); // Reset toggle if permission is not granted
}
} else {
bluetoothAdapter.disable();
showToast("Bluetooth OFF");
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_BLUETOOTH_PERMISSION && grantResults.length
> 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
bluetoothAdapter.enable();
showToast("Bluetooth ON");
} else {
toggleButton.setChecked(false);
showToast("Permission denied");
}
}

private void showToast(String message) {


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

Manifest file:

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


<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

23.WAP to convert temperature from celcius to ferenhitr and


vice versa using Toggle button (Desing UI as per your choise
write xml and java file)

XML FILE

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Temperature Converter"
android:textSize="24sp"
android:paddingBottom="16dp" />

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

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="C to F"
android:textOff="F to C"
android:layout_marginTop="16dp" />

<Button
android:id="@+id/convertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert"
android:layout_marginTop="16dp" />

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

JAVA FILE:

package com.example.tempretureconversion;

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

public class MainActivity extends AppCompatActivity {

private EditText inputTemperature;


private ToggleButton toggleButton;
private Button convertButton;
private TextView resultTextView;

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

inputTemperature = findViewById(R.id.inputTemperature);
toggleButton = findViewById(R.id.toggleButton);
convertButton = findViewById(R.id.convertButton);
resultTextView = findViewById(R.id.resultTextView);

convertButton.setOnClickListener(this::convertTemperature);
}

private void convertTemperature(View view) {


String input = inputTemperature.getText().toString();
if (input.isEmpty()) {
resultTextView.setText("Please enter a temperature.");
return;
}

double temperature = Double.parseDouble(input);


if (toggleButton.isChecked()) {
// Convert Celsius to Fahrenheit
double fahrenheit = (temperature * 9/5) + 32;
resultTextView.setText(String.format("%.2f °F", fahrenheit));
} else {
// Convert Fahrenheit to Celsius
double celsius = (temperature - 32) * 5/9;
resultTextView.setText(String.format("%.2f °C", celsius));
}
}
}

25. develope a program to perform


addition,substraction,division,multiplication of two number
and display the result (use appropriate UI controls)

XML FILE:
<?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/num1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal"/>

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

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

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

<Button
android:id="@+id/btnMultiply"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Multiplication"/>
<Button
android:id="@+id/btnDivide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Division"/>

<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result: "
android:textSize="18sp"
android:textStyle="bold"
android:padding="10dp"/>
</LinearLayout>

JAVA FILE:

package com.example.add;

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

public class MainActivity extends AppCompatActivity {

EditText num1, num2;


Button btnAdd, btnSubtract, btnMultiply, btnDivide;
TextView tvResult;

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

num1 = findViewById(R.id.num1);
num2 = findViewById(R.id.num2);
btnAdd = findViewById(R.id.btnAdd);
btnSubtract = findViewById(R.id.btnSubtract);
btnMultiply = findViewById(R.id.btnMultiply);
btnDivide = findViewById(R.id.btnDivide);
tvResult = findViewById(R.id.tvResult);

btnAdd.setOnClickListener(v -> performOperation("+"));


btnSubtract.setOnClickListener(v -> performOperation("-"));
btnMultiply.setOnClickListener(v -> performOperation("*"));
btnDivide.setOnClickListener(v -> performOperation("/"));
}

private void performOperation(String operator) {


String s1 = num1.getText().toString();
String s2 = num2.getText().toString();

if (s1.isEmpty() || s2.isEmpty()) {
tvResult.setText("Please enter both numbers");
return;
}

double number1 = Double.parseDouble(s1);


double number2 = Double.parseDouble(s2);
double result = 0;

switch (operator) {
case "+":
result = number1 + number2;
break;
case "-":
result = number1 - number2;
break;
case "*":
result = number1 * number2;
break;
case "/":
if (number2 == 0) {
tvResult.setText("Error: Cannot divide by zero");
return;
}
result = number1 / number2;
break;
}

tvResult.setText("Result: " + result);


}
}

26. develop an application to update a record of an employee


whose empid is 'E101' in sqlite database change employee
name from "pqr" to "XYZ". Also display the updated
record(write .java and .xml files)

XML FILE:

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

<Button
android:id="@+id/btnUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toggle Employee Name" />

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

JAVA FILE:

package com.example.employee;

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

public class MainActivity extends AppCompatActivity {

SQLiteDatabase db;
TextView txtResult;
String empid = "E101";

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

txtResult = findViewById(R.id.txtResult);
db = openOrCreateDatabase("EmployeeDB", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Employee(empid TEXT PRIMARY
KEY, name TEXT)");
db.execSQL("INSERT OR IGNORE INTO Employee VALUES('E101', 'pqr')");

findViewById(R.id.btnUpdate).setOnClickListener(v ->
toggleEmployeeName());
displayUpdatedRecord();
}
private void toggleEmployeeName() {
Cursor c = db.rawQuery("SELECT name FROM Employee WHERE empid=?",
new String[]{empid});
if (c.moveToFirst()) {
String newName = c.getString(0).equals("pqr") ? "XYZ" : "pqr";
db.execSQL("UPDATE Employee SET name=? WHERE empid=?", new Object[]
{newName, empid});
}
c.close();
displayUpdatedRecord();
}

private void displayUpdatedRecord() {


Cursor c = db.rawQuery("SELECT * FROM Employee WHERE empid=?", new
String[]{empid});
if (c.moveToFirst()) txtResult.setText("ID: " + c.getString(0) + "\nName: " +
c.getString(1));
c.close();
}
}

You might also like