0% found this document useful (0 votes)
8 views5 pages

Practical No. 09

The document contains code snippets for creating an Android application with two main features: a toggle button to display the ON/OFF status of Bluetooth and a simple calculator that performs addition, subtraction, multiplication, and division. The layout is defined in XML, and the functionality is implemented in Java within the MainActivity class. The application utilizes Toast messages to provide feedback to the user based on the toggle button's state and the calculator's operations.

Uploaded by

ganeshkumbhar638
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)
8 views5 pages

Practical No. 09

The document contains code snippets for creating an Android application with two main features: a toggle button to display the ON/OFF status of Bluetooth and a simple calculator that performs addition, subtraction, multiplication, and division. The layout is defined in XML, and the functionality is implemented in Java within the MainActivity class. The application utilizes Toast messages to provide feedback to the user based on the toggle button's state and the calculator's operations.

Uploaded by

ganeshkumbhar638
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/ 5

Q. Write a program to create a toggle button to import android.widget.

Toast;
display ON / OFF Bluetooth on the display import android.widget.ToggleButton;
screen.
activity_main.xml public class MainActivity extends
AppCompatActivity {
<?xml version="1.0" encoding="utf-8"?> ToggleButton tb;
<AbsoluteLayout @Override
xmlns:android="http://schemas.android.com/apk/res protected void onCreate(Bundle
/android" savedInstanceState) {
xmlns:app="http://schemas.android.com/apk/res- super.onCreate(savedInstanceState);
auto" setContentView(R.layout.activity_main);
xmlns:tools="http://schemas.android.com/tools" tb=(ToggleButton)findViewById(R.id.tb11);
android:layout_width="match_parent" tb.setOnCheckedChangeListener(new
android:background="@drawable/immm" CompoundButton.OnCheckedChangeListener() {
android:layout_height="match_parent" @Override
tools:context=".MainActivity"> public void
onCheckedChanged(CompoundButton
<ToggleButton compoundButton, boolean b) {
android:id="@+id/tb11" if (b == true) {
android:layout_width="132dp" Toast.makeText(MainActivity.this,
android:layout_height="68dp" "Wifi on", Toast.LENGTH_SHORT).show();
android:layout_x="141dp" } else {
android:layout_y="296dp" Toast.makeText(MainActivity.this,
android:text="ToggleButton" "Wifi off", Toast.LENGTH_SHORT).show();
android:textOff="Off" }
android:background="@color/white"
android:textOn="On" }
android:fontFamily="times new roman" });
tools:layout_editor_absoluteX="99dp"
tools:layout_editor_absoluteY="273dp" /> }
}

</AbsoluteLayout>

MainActivity.java

package com.example.practicalno09_1;

import
androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CompoundButton;
Q. Write a program to create a simple android:layout_height="wrap_content"
calculator. android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
activity_main.java
android:textSize="20dp"
<?xml version="1.0" encoding="utf-8"?> android:text="Result" />
<RelativeLayout <Button
android:id="@+id/sum"
xmlns:android="http://schemas.android.com/apk/res android:layout_below="@id/result"
/android" android:layout_width="wrap_content"
xmlns:app="http://schemas.android.com/apk/res- android:layout_height="wrap_content"
auto" android:textSize="20dp"
xmlns:tools="http://schemas.android.com/tools" android:layout_marginLeft="5dp"
android:layout_width="match_parent" android:text="+" />
android:layout_height="match_parent" <Button
android:padding="25dp" android:id="@+id/sub"
android:gravity="center" android:layout_below="@id/result"
tools:context=".MainActivity"> android:layout_toRightOf="@id/sum"
<TextView android:layout_width="wrap_content"
android:id="@+id/heading" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:textSize="20dp"
android:layout_height="wrap_content" android:layout_marginLeft="5dp"
android:text=" Calculator" android:text="-" />
android:layout_centerHorizontal="true" <Button
android:textStyle="bold" android:id="@+id/div"
android:textSize="30dp" /> android:layout_below="@id/result"
<EditText android:layout_toRightOf="@id/sub"
android:id="@+id/num1" android:layout_width="wrap_content"
android:layout_below="@+id/heading" android:layout_height="wrap_content"
android:layout_width="match_parent" android:textSize="20dp"
android:layout_height="wrap_content" android:layout_marginLeft="5dp"
android:hint="Enter Number 1" android:text="/" />
android:inputType="number" /> <Button
<EditText android:id="@+id/mul"
android:id="@+id/num2" android:layout_below="@id/result"
android:layout_below="@+id/num1" android:layout_toRightOf="@id/div"
android:hint="Enter Number 2" android:layout_width="wrap_content"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:textSize="20dp"
android:inputType="number" /> android:layout_marginLeft="5dp"
<TextView android:text="x"/>
android:id="@+id/result" </RelativeLayout>
android:layout_below="@+id/num2"
android:layout_width="match_parent"
});
MainActivity.java sub.setOnClickListener(new
View.OnClickListener() {
package com.example.practicalno09_2; @Override
public void onClick(View view) {
import num1 =
androidx.appcompat.app.AppCompatActivity; Integer.parseInt(e1.getText().toString());
import android.os.Bundle; num2 =
import android.view.View; Integer.parseInt(e2.getText().toString());
import android.widget.Button; int sum = num1 - num2;
import android.widget.EditText; t1.setText("Substraction is
import android.widget.TextView; :"+Integer.toString(sum));
public class MainActivity extends }
AppCompatActivity { });
public EditText e1, e2; mul.setOnClickListener(new
Button add, sub , mul, div; View.OnClickListener() {
TextView t1; @Override
int num1, num2; public void onClick(View view) {
@Override num1 =
protected void onCreate(Bundle Integer.parseInt(e1.getText().toString());
savedInstanceState) { num2 =
super.onCreate(savedInstanceState); Integer.parseInt(e2.getText().toString());
setContentView(R.layout.activity_main); int sum = num1 * num2;
e1 = (EditText) findViewById(R.id.num1); t1.setText("Multiplication is
e2 = (EditText) findViewById(R.id.num2); :"+Integer.toString(sum));
t1 = (TextView) findViewById(R.id.result); }
add = (Button) findViewById(R.id.sum); });
mul = (Button) findViewById(R.id.mul); div.setOnClickListener(new
div = (Button) findViewById(R.id.div); View.OnClickListener() {
sub = (Button) findViewById(R.id.sub); @Override
add.setOnClickListener(new public void onClick(View view) {
View.OnClickListener() { num1 =
@Override Integer.parseInt(e1.getText().toString());
public void onClick(View view) { num2 =
num1 = Integer.parseInt(e2.getText().toString());
Integer.parseInt(e1.getText().toString()); int sum = num1 / num2;
num2 = t1.setText("Division is
Integer.parseInt(e2.getText().toString()); :"+Integer.toString(sum));
int sum = num1 + num2; }
t1.setText("Addtion is });
:"+Integer.toString(sum)); }
} }

You might also like