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

Practical No. 09

The document contains code snippets for two Android applications: one for a toggle button to display Bluetooth status and another for a simple calculator. The toggle button uses a ToggleButton widget to show 'Wifi on' or 'Wifi off' based on its state, while the calculator allows users to perform addition, subtraction, multiplication, and division using two input numbers. Both applications utilize XML for layout design and Java for functionality in the MainActivity class.

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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Practical No. 09

The document contains code snippets for two Android applications: one for a toggle button to display Bluetooth status and another for a simple calculator. The toggle button uses a ToggleButton widget to show 'Wifi on' or 'Wifi off' based on its state, while the calculator allows users to perform addition, subtraction, multiplication, and division using two input numbers. Both applications utilize XML for layout design and Java for functionality in the MainActivity class.

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 DOCX, 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
import android.widget.TextView; is :"+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
e2 = (EditText) findViewById(R.id.num2); is :"+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
Integer.parseInt(e2.getText().toString()); is :"+Integer.toString(sum));
int sum = num1 + num2; }
t1.setText("Addtion });
is :"+Integer.toString(sum)); }
} }
});

You might also like