0% found this document useful (0 votes)
11 views6 pages

Peactical No.9

The document contains two Android application programs: one for creating a toggle button to display Bluetooth on/off status and another for a simple calculator. The first program includes a layout with two toggle buttons and a submit button that shows the toggle states in a toast message. The second program features a layout with input fields and buttons for basic arithmetic operations, displaying the results in a text view.
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)
11 views6 pages

Peactical No.9

The document contains two Android application programs: one for creating a toggle button to display Bluetooth on/off status and another for a simple calculator. The first program includes a layout with two toggle buttons and a submit button that shows the toggle states in a toast message. The second program features a layout with input fields and buttons for basic arithmetic operations, displaying the results in a text view.
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/ 6

Practical No:- 9

Q1. write a program to create a toggle button to display on/off


Bluetooth on the display screen

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

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="80dp"
android:text="ToggleButton"
android:textOff="Off"
android:textOn="On"
app:layout_constraintEnd_toStartOf="@+id/toggleButton2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ToggleButton
android:id="@+id/toggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:layout_marginTop="80dp"
android:text="ToggleButton"
android:textOff="Off"
android:textOn="On"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="144dp"
android:layout_marginLeft="148dp"
android:text="Submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Practical No:- 9

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.Java
package com.example.a9;

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

public class MainActivity extends AppCompatActivity {


private ToggleButton toggleButton1, toggleButton2;
private Button buttonSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

addListenerOnButtonClick();
}

public void addListenerOnButtonClick(){


//Getting the ToggleButton and Button instance from the layout xml file
toggleButton1=(ToggleButton)findViewById(R.id.toggleButton);
toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2);
buttonSubmit=(Button)findViewById(R.id.button);

//Performing action on button click


buttonSubmit.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View view) {
StringBuilder result = new StringBuilder();
result.append("ToggleButton1 : ").append(toggleButton1.getText());
result.append("\nToggleButton2 : ").append(toggleButton2.getText());
//Displaying the message in toast
Toast.makeText(getApplicationContext(), result.toString(),Toast.LENGTH_LONG).show();
}

});

}
}
Practical No:- 9
Output:-

Q2. Write a Program To Create a Simple Calculator.


MainActivity.Java
<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/etNumber1"
android:layout_width="226dp"
android:layout_height="wrap_content"
android:hint="Enter number 1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
Practical No:- 9
android:id="@+id/etNumber2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter number 2"
android:minHeight="48dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:id="@+id/btnAdd"
android:layout_width="201dp"
android:layout_height="match_parent"
android:text="Add Numbers" />

<Button
android:id="@+id/btnSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtract Numbers" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:id="@+id/btnDiv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Div Numbers" />

<Button
android:id="@+id/btnMul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mul Numbers" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="69dp"
android:text="Result will be displayed here" />
</TableRow>

</TableLayout>
Practical No:- 9
MainActivity.Java
package com.example.a92;

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;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText etNumber1, etNumber2;


private Button btnAdd;

private Button btnSub;


private Button btnDiv;
private Button btnMul;

private TextView tvResult;

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

etNumber1 = findViewById(R.id.etNumber1);
etNumber2 = findViewById(R.id.etNumber2);
btnAdd = findViewById(R.id.btnAdd);
btnSub = findViewById(R.id.btnSub);
btnDiv = findViewById(R.id.btnDiv);
btnMul = findViewById(R.id.btnMul);
tvResult = findViewById(R.id.tvResult);

btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int number1 = Integer.parseInt(etNumber1.getText().toString());
int number2 = Integer.parseInt(etNumber2.getText().toString());
int sum = number1 + number2;
tvResult.setText("Result: " + sum);
}
});

btnSub.setOnClickListener(new View.OnClickListener() {
@Override
Practical No:- 9
public void onClick(View v) {
int number1 = Integer.parseInt(etNumber1.getText().toString());
int number2 = Integer.parseInt(etNumber2.getText().toString());
int diff = number1 - number2;
tvResult.setText("Result: " + diff);
}
});

btnDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int number1 = Integer.parseInt(etNumber1.getText().toString());
int number2 = Integer.parseInt(etNumber2.getText().toString());
int Div = number1 / number2;
tvResult.setText("Result: " + Div);
}
});
btnMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int number1 = Integer.parseInt(etNumber1.getText().toString());
int number2 = Integer.parseInt(etNumber2.getText().toString());
int mul = number1 * number2;
tvResult.setText("Result: " + mul);
}
});
}
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy