Professional Documents
Culture Documents
display screen.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Exp9_1:Bluetooth Toggle Button "
android:textSize="18dp"
android:textColor="#0000FF"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<ImageView
android:id="@+id/img"
android:layout_width="129dp"
android:layout_height="150dp"
android:layout_marginStart="112dp"
android:layout_marginTop="180dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="@drawable/bluetooth_off"
/>
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Bluetooth OFF"
android:textOn="Bluetooth ON"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="~Developed By Harsh.Maghnani (Roll no:06)"
android:textColor="#0000FF"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginBottom="35dp"
android:layout_marginEnd="35dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Main_Activity.java:
package com.example.exp9_1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; // Add this import
import android.widget.ImageView;
import android.widget.ToggleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb = findViewById(R.id.toggle);
im = findViewById(R.id.img);
tb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tb.isChecked()) {
im.setImageResource(R.drawable.bluetooth_on);
} else {
im.setImageResource(R.drawable.bluetooth_off);
}
}
});
}
}
Output:-
Exp9_2: Write a program to create a simple calculator.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:orientation="vertical">
<TextView
android:id="@+id/Exp"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="100dp"
android:textSize="20sp"
android:textColor="#00f"
android:text="Exp 9_2: Calculator"/>
<TextView
android:id="@+id/input"
android:layout_marginTop="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="80dp"
android:textSize="40sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
<Button
android:id="@+id/button_clear"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="C"
android:textSize="30sp"
/>
<Button
android:id="@+id/button_percent"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="%"
android:textSize="30sp" />
<Button
android:id="@+id/button_back"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="\u232b"
android:textSize="30sp" />
<Button
android:id="@+id/button_division"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="\u00F7"
android:textSize="40sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
<Button
android:id="@+id/button_seven"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="7"
android:textColor="@android:color/white"
android:textSize="30sp" />
<Button
android:id="@+id/button_eight"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="8"
android:textColor="@android:color/white"
android:textSize="30sp" />
<Button
android:id="@+id/button_nine"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="9"
android:textColor="@android:color/white"
android:textSize="30sp" />
<Button
android:id="@+id/button_multiplication"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="x"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
<Button
android:id="@+id/button_four"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="4"
android:textColor="@android:color/white"
android:textSize="30sp" />
<Button
android:id="@+id/button_five"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="5"
android:textColor="@android:color/white"
android:textSize="30sp" />
<Button
android:id="@+id/button_six"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="6"
android:textColor="@android:color/white"
android:textSize="30sp" />
<Button
android:id="@+id/button_subtraction"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="-"
android:textSize="40sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
<Button
android:id="@+id/button_one"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="1"
android:textSize="30sp" />
<Button
android:id="@+id/button_two"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="2"
android:textColor="@android:color/white"
android:textSize="30sp" />
<Button
android:id="@+id/button_three"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="3"
android:textColor="@android:color/white"
android:textSize="30sp" />
<Button
android:id="@+id/button_addition"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="+"
android:textSize="40sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
<Button
android:id="@+id/button_zero"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="@android:color/black"
android:text="0"
android:textSize="30sp" />
<Button
android:id="@+id/button_dot"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="."
android:textSize="30sp" />
<Button
android:id="@+id/button_equal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:text="="
android:textSize="40sp" />
</LinearLayout>
<TextView
android:layout_marginTop="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:textColor="#00f"
android:textSize="20sp"
android:text="Developed By Harsh Maghnani Roll No:06"/>
</LinearLayout>
MainActivity.java:
package com.example.exp9_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
b16.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (display != null) {
String displayText =
display.getText().toString().trim();
if (!displayText.isEmpty()) {
if (!add && !sub && !mul && !div) {
n1 = Float.parseFloat(displayText);
add = true;
display.setText(displayText + "+");
}
}
}
}
});
b12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (display != null) {
String displayText =
display.getText().toString().trim();
if (!displayText.isEmpty()) {
if (!add && !sub && !mul && !div){
n1 = Float.parseFloat(displayText);
sub = true;
display.setText(displayText + "-");
}
}
}
}
});
b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (display != null) {
String displayText =
display.getText().toString().trim();
if (!displayText.isEmpty()) {
if (!add && !sub && !mul && !div){
n1 = Float.parseFloat(displayText);
mul = true;
display.setText(displayText + "x");
}
}
}
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (display != null) {
String displayText =
display.getText().toString().trim();
if (!displayText.isEmpty()) {
if (!add && !sub && !mul && !div){
n1 = Float.parseFloat(displayText);
div = true;
display.setText(displayText + "\u00F7");
}
}
}
}
});
b19.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (display != null) {
String displayText =
display.getText().toString().trim();
if (!displayText.isEmpty()) {
String[] parts = displayText.split("[+\\-
x\u00F7]");
float n2 = Float.parseFloat(parts[1]);
if (add) {
display.setText(n1 + n2 + "");
} else if (sub) {
display.setText(n1 - n2 + "");
} else if (mul) {
display.setText(n1 * n2 + "");
} else if (div) {
if (n2 != 0) {
display.setText(n1 / n2 + "");
} else {
display.setText("Error: Division by zero");
}
}
Output:-