0% found this document useful (0 votes)
14 views8 pages

Exp 9

The document contains two Android application programs: one for creating a toggle button to display ON/OFF status for Bluetooth, and another for a simple calculator. The Bluetooth program includes XML layout and Java code for toggling the Bluetooth image, while the calculator program features a layout with buttons for numbers and operations, along with Java code for handling calculations. Both programs are designed by Muskan Kateja, Roll No. 17.
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)
14 views8 pages

Exp 9

The document contains two Android application programs: one for creating a toggle button to display ON/OFF status for Bluetooth, and another for a simple calculator. The Bluetooth program includes XML layout and Java code for toggling the Bluetooth image, while the calculator program features a layout with buttons for numbers and operations, along with Java code for handling calculations. Both programs are designed by Muskan Kateja, Roll No. 17.
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/ 8

Experiment 9

1.​ Write a program to create a toggle button to display ON / OFF


Bluetooth on the display screen.
<TextView
Activity_main.xml android:layout_width="match_parent"
android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?> android:text="Programmed by Muskan
<RelativeLayout Kateja, Roll No. 17"
xmlns:android="http://schemas.android.com android:textSize="18sp"
/apk/res/android" android:textColor="#000000"
android:layout_width="match_parent" android:gravity="center"
android:layout_height="match_parent"> android:padding="10dp"
android:layout_marginTop="20dp"
<!-- Bluetooth Image --> android:background="#fff"/>
<ImageView
android:id="@+id/bluetoothImage" </RelativeLayout>
android:layout_width="200dp"
android:layout_height="200dp" MainActivity.java

android:src="@drawable/bluetooth_off"/> package com.example.prac9_1;

<!-- Toggle Button for Bluetooth ON/OFF import android.os.Bundle;


--> import android.view.View;
<Button import android.widget.Button;
import android.widget.ImageView;
android:id="@+id/toggleBluetoothButton" import
android:layout_width="wrap_content" androidx.appcompat.app.AppCompatActivit
android:layout_height="wrap_content" y;
android:text="Toggle Bluetooth"
public class MainActivity extends
android:layout_below="@id/bluetoothImag AppCompatActivity {
e"
android:layout_centerHorizontal="true" private Button toggleBluetoothButton;
android:layout_marginTop="50dp" private ImageView bluetoothImage;
android:background="#ED1E0A"/>
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Initialize the button and image view


toggleBluetoothButton =
findViewById(R.id.toggleBluetoothButton);
bluetoothImage =
findViewById(R.id.bluetoothImage);

// Set a click listener for the button

toggleBluetoothButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toggle Bluetooth ON/OFF
if (bluetoothImage.getTag() ==
null ||
bluetoothImage.getTag().equals("OFF")) {
// Change to ON state

bluetoothImage.setImageResource(R.drawa
ble.bluetooth_on); // Your ON image
bluetoothImage.setTag("ON");
} else {
// Change to OFF state

bluetoothImage.setImageResource(R.drawa
ble.bluetooth_off); // Your OFF image
bluetoothImage.setTag("OFF");
}
}
});
}
}
2.​ Write a program to create a simple calculator.

Activity_main.xml <!-- Number buttons -->


<Button
<?xml version="1.0" encoding="utf-8"?> android:id="@+id/button1"
<LinearLayout android:layout_width="0dp"
xmlns:android="http://schemas.android.com
/apk/res/android" android:layout_height="wrap_content"
android:layout_width="match_parent" android:layout_rowSpan="1"
android:layout_height="match_parent" android:layout_columnSpan="1"
android:orientation="vertical" android:layout_columnWeight="1"
android:padding="20dp" android:layout_margin="10dp"
android:background="#F0F0F0"> android:text="1"
android:textSize="24sp" />
<!-- Result TextView for displaying the
current expression and result --> <Button
<TextView android:id="@+id/button2"
android:id="@+id/resultText" android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="0" android:layout_rowSpan="1"
android:textSize="32sp" android:layout_columnSpan="1"
android:gravity="end" android:layout_columnWeight="1"
android:padding="20dp" android:layout_margin="10dp"
android:background="#FFFFFF" android:text="2"
android:textColor="#000000" android:textSize="24sp" />

android:layout_marginBottom="20dp"/> <Button
android:id="@+id/button3"
<!-- GridLayout for calculator buttons --> android:layout_width="0dp"
<GridLayout
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:layout_rowSpan="1"
android:background="#FFFFFF" android:layout_columnSpan="1"
android:columnCount="4" android:layout_columnWeight="1"
android:orientation="horizontal"> android:layout_margin="10dp"
android:text="3" android:id="@+id/button7"
android:textSize="24sp" /> android:layout_width="0dp"

<Button android:layout_height="wrap_content"
android:id="@+id/button4" android:layout_rowSpan="1"
android:layout_width="0dp" android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_rowSpan="1" android:text="7"
android:layout_columnSpan="1" android:textSize="24sp" />
android:layout_columnWeight="1"
android:layout_margin="10dp" <Button
android:text="4" android:id="@+id/button8"
android:textSize="24sp" /> android:layout_width="0dp"

<Button android:layout_height="wrap_content"
android:id="@+id/button5" android:layout_rowSpan="1"
android:layout_width="0dp" android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_rowSpan="1" android:text="8"
android:layout_columnSpan="1" android:textSize="24sp" />
android:layout_columnWeight="1"
android:layout_margin="10dp" <Button
android:text="5" android:id="@+id/button9"
android:textSize="24sp" /> android:layout_width="0dp"

<Button android:layout_height="wrap_content"
android:id="@+id/button6" android:layout_rowSpan="1"
android:layout_width="0dp" android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_rowSpan="1" android:text="9"
android:layout_columnSpan="1" android:textSize="24sp" />
android:layout_columnWeight="1"
android:layout_margin="10dp" <Button
android:text="6" android:id="@+id/button0"
android:textSize="24sp" /> android:layout_width="0dp"

<Button android:layout_height="wrap_content"
android:layout_rowSpan="1" android:layout_margin="10dp"
android:layout_columnSpan="2" android:text="×"
android:layout_columnWeight="2" android:textSize="24sp" />
android:layout_margin="10dp"
android:text="0" <Button
android:textSize="24sp" /> android:id="@+id/buttonDivide"
android:layout_width="0dp"
<!-- Operation buttons -->
<Button android:layout_height="wrap_content"
android:id="@+id/buttonAdd" android:layout_rowSpan="1"
android:layout_width="0dp" android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_rowSpan="1" android:text="÷"
android:layout_columnSpan="1" android:textSize="24sp" />
android:layout_columnWeight="1"
android:layout_margin="10dp" <Button
android:text="+" android:id="@+id/buttonClear"
android:textSize="24sp" /> android:layout_width="0dp"

<Button android:layout_height="wrap_content"
android:id="@+id/buttonSubtract" android:layout_rowSpan="1"
android:layout_width="0dp" android:layout_columnSpan="1"
android:layout_columnWeight="1"
android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_rowSpan="1" android:text="C"
android:layout_columnSpan="1" android:textSize="24sp" />
android:layout_columnWeight="1"
android:layout_margin="10dp" <Button
android:text="-" android:id="@+id/buttonEqual"
android:textSize="24sp" /> android:layout_width="0dp"

<Button android:layout_height="wrap_content"
android:id="@+id/buttonMultiply" android:layout_rowSpan="1"
android:layout_width="0dp" android:layout_columnSpan="2"
android:layout_columnWeight="2"
android:layout_height="wrap_content" android:layout_margin="10dp"
android:layout_rowSpan="1" android:text="="
android:layout_columnSpan="1" android:textSize="24sp" />
android:layout_columnWeight="1" </GridLayout>
<TextView
android:layout_width="match_parent" // Initialize the display TextView
android:layout_height="wrap_content" display = findViewById(R.id.display);
android:text="Programmed by Muskan
Kateja, Roll No. 17" // Number buttons (0-9)
android:textSize="18sp" Button button0 =
android:textColor="#000000" findViewById(R.id.button0);
android:gravity="center" Button button1 =
android:padding="10dp" findViewById(R.id.button1);
android:layout_marginTop="20dp" Button button2 =
android:background="#fff"/> findViewById(R.id.button2);
</LinearLayout> Button button3 =
findViewById(R.id.button3);
MainActivity.java Button button4 =
findViewById(R.id.button4);
package com.example.prac9_2; Button button5 =
findViewById(R.id.button5);
import android.os.Bundle; Button button6 =
import android.view.View; findViewById(R.id.button6);
import android.widget.Button; Button button7 =
import android.widget.TextView; findViewById(R.id.button7);
Button button8 =
import findViewById(R.id.button8);
androidx.appcompat.app.AppCompatActivit Button button9 =
y; findViewById(R.id.button9);

public class MainActivity extends // Operator buttons (+, -, *, /)


AppCompatActivity { Button buttonAdd =
findViewById(R.id.buttonAdd);
private TextView display; Button buttonMinus =
private String currentText = ""; findViewById(R.id.buttonMinus);
private String lastOperator = ""; Button buttonMultiply =
private double firstNumber = 0; findViewById(R.id.buttonMultiply);
Button buttonDivide =
@Override findViewById(R.id.buttonDivide);
protected void onCreate(Bundle
savedInstanceState) { // Equals and Clear buttons
super.onCreate(savedInstanceState); Button buttonEquals =
findViewById(R.id.buttonEquals);
setContentView(R.layout.activity_main);
Button buttonClear =
findViewById(R.id.buttonClear); // Update the display
private void updateDisplay(String value)
// Set OnClickListener for each button {
button0.setOnClickListener(view -> currentText += value;
updateDisplay("0")); display.setText(currentText);
button1.setOnClickListener(view -> }
updateDisplay("1"));
button2.setOnClickListener(view -> // Set the operator
updateDisplay("2")); private void setOperator(String operator)
button3.setOnClickListener(view -> {
updateDisplay("3")); if (!currentText.isEmpty()) {
button4.setOnClickListener(view -> firstNumber =
updateDisplay("4")); Double.parseDouble(currentText);
button5.setOnClickListener(view -> lastOperator = operator;
updateDisplay("5")); currentText = "";
button6.setOnClickListener(view -> }
updateDisplay("6")); }
button7.setOnClickListener(view ->
updateDisplay("7")); // Calculate the result based on the
button8.setOnClickListener(view -> operator
updateDisplay("8")); private void calculateResult() {
button9.setOnClickListener(view -> if (!currentText.isEmpty()) {
updateDisplay("9")); double secondNumber =
Double.parseDouble(currentText);
buttonAdd.setOnClickListener(view -> double result = 0;
setOperator("+"));
buttonMinus.setOnClickListener(view switch (lastOperator) {
-> setOperator("-")); case "+":
result = firstNumber +
buttonMultiply.setOnClickListener(view -> secondNumber;
setOperator("*")); break;
buttonDivide.setOnClickListener(view case "-":
-> setOperator("/")); result = firstNumber -
secondNumber;
buttonEquals.setOnClickListener(view break;
-> calculateResult()); case "*":
buttonClear.setOnClickListener(view result = firstNumber *
-> clearDisplay()); secondNumber;
} break;
case "/":
if (secondNumber != 0) {
result = firstNumber /
secondNumber;
} else {
currentText = "Error";
display.setText(currentText);
return;
}
break;
}

currentText = String.valueOf(result);
display.setText(currentText);
firstNumber = result;
}
}

// Clear the display


private void clearDisplay() {
currentText = "";
firstNumber = 0;
lastOperator = "";
display.setText("0");
}
}

You might also like