0% found this document useful (0 votes)
9 views9 pages

Practical No 9 (MAD)

The document contains two practical programming tasks: creating a toggle button for Bluetooth status and developing a simple calculator. The first part includes XML and Java code for a toggle button that enables or disables Bluetooth, displaying its status on the screen. The second part provides a layout and Java code for a calculator that performs basic arithmetic operations based on user input.

Uploaded by

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

Practical No 9 (MAD)

The document contains two practical programming tasks: creating a toggle button for Bluetooth status and developing a simple calculator. The first part includes XML and Java code for a toggle button that enables or disables Bluetooth, displaying its status on the screen. The second part provides a layout and Java code for a calculator that performs basic arithmetic operations based on user input.

Uploaded by

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

Practical No 9

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;

public class MainActivity extends AppCompatActivity {


private BluetoothAdapter bluetoothAdapter;
private ToggleButton toggleButton;
private TextView bluetoothStatus;

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

// Update UI based on Bluetooth state


updateBluetoothUI();

toggleButton.setOnCheckedChangeListener((buttonView, isChecked) -> {


if (isChecked) {
// Enable Bluetooth (Prompt user)
enableBluetooth();
} else {
// Disable Bluetooth (Prompt user)
disableBluetooth();
}
});
}

@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();
}
}

private void disableBluetooth() {


if (bluetoothAdapter.isEnabled()) {
Intent disableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISABLE);
startActivity(disableIntent);
} else {
Toast.makeText(this, "Bluetooth is already OFF",
Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

// Update UI after enabling/disabling Bluetooth


if (requestCode == 1) {
updateBluetoothUI();
}
}
}
Output:
Q2. Write a program to create a simple calculator

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

<!-- First Number Input -->


<EditText
android:id="@+id/number1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter First Number"
android:inputType="numberDecimal"
android:layout_marginBottom="10dp"/>

<!-- Second Number Input -->


<EditText
android:id="@+id/number2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Second Number"
android:inputType="numberDecimal"
android:layout_below="@id/number1"
android:layout_marginBottom="20dp"/>

<!-- Buttons for Operations -->


<LinearLayout
android:id="@+id/buttonLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/number2"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginBottom="20dp">

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

<!-- Result Display -->


<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="18sp"
android:textStyle="bold"
android:layout_below="@+id/buttonLayout"
android:layout_marginTop="20dp"
android:text="Result: " />

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

public class MainActivity extends AppCompatActivity {

private EditText number1, number2;


private Button btnAdd, btnSubtract, btnMultiply, btnDivide;
private TextView result;

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

// Add button click listener


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

// Subtract button click listener


btnSubtract.setOnClickListener(view -> performOperation("-"));

// Multiply button click listener


btnMultiply.setOnClickListener(view -> performOperation("*"));

// Divide button click listener


btnDivide.setOnClickListener(view -> performOperation("/"));
}

private void performOperation(String operation) {


String num1 = number1.getText().toString();
String num2 = number2.getText().toString();

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

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

} catch (NumberFormatException e) {
Toast.makeText(this, "Invalid input", Toast.LENGTH_SHORT).show();
}
}
}
Output:

You might also like