Practical No 9 (MAD)
Practical No 9 (MAD)
Q1. Write a program to create as toggle button to display ON/OFF Bluetooth on the
display screen
Xml code
<?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">
<ToggleButton
android:id="@+id/toggleBluetooth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:textOff="Bluetooth OFF"
android:textOn="Bluetooth ON" />
<TextView
android:id="@+id/bluetoothStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/toggleBluetooth"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Bluetooth is OFF"
android:textSize="18sp"
android:textColor="#000000" />
</RelativeLayout>
Java file
package com.example.hemloworld;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not supported on this device.",
Toast.LENGTH_SHORT).show();
finish(); // Close the app
}
toggleButton = findViewById(R.id.toggleBluetooth);
bluetoothStatus = findViewById(R.id.bluetoothStatus);
@SuppressLint("SetTextI18n")
private void updateBluetoothUI() {
if (bluetoothAdapter.isEnabled()) {
toggleButton.setChecked(true);
bluetoothStatus.setText("Bluetooth is ON");
} else {
toggleButton.setChecked(false);
bluetoothStatus.setText("Bluetooth is OFF");
}
}
private void enableBluetooth() {
if (!bluetoothAdapter.isEnabled()) {
Intent enableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, 1);
} else {
Toast.makeText(this, "Bluetooth is already ON",
Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
.xml File :
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_marginEnd="10dp"/>
<Button
android:id="@+id/btnSubtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtract"
android:layout_marginEnd="10dp"/>
<Button
android:id="@+id/btnMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiply"
android:layout_marginEnd="10dp"/>
<Button
android:id="@+id/btnDivide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Divide" />
</LinearLayout>
</RelativeLayout>
Java File :
package com.example.hemloworld;
import android.os.Bundle;
import android.text.TextUtils;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize UI components
number1 = findViewById(R.id.number1);
number2 = findViewById(R.id.number2);
btnAdd = findViewById(R.id.btnAdd);
btnSubtract = findViewById(R.id.btnSubtract);
btnMultiply = findViewById(R.id.btnMultiply);
btnDivide = findViewById(R.id.btnDivide);
result = findViewById(R.id.result);
if (TextUtils.isEmpty(num1) || TextUtils.isEmpty(num2)) {
Toast.makeText(this, "Please enter both numbers",
Toast.LENGTH_SHORT).show();
return;
}
try {
double value1 = Double.parseDouble(num1);
double value2 = Double.parseDouble(num2);
double resultValue = 0;
switch (operation) {
case "+":
resultValue = value1 + value2;
break;
case "-":
resultValue = value1 - value2;
break;
case "*":
resultValue = value1 * value2;
break;
case "/":
if (value2 == 0) {
Toast.makeText(this, "Cannot divide by zero",
Toast.LENGTH_SHORT).show();
return;
}
resultValue = value1 / value2;
break;
}
} catch (NumberFormatException e) {
Toast.makeText(this, "Invalid input", Toast.LENGTH_SHORT).show();
}
}
}
Output: