Mad Prac9
Mad Prac9
activity_main.xml MainActivity.java
<?xml version="1.0" encoding="utf-8"?> package com.example.exp9_1;
<RelativeLayout import android.os.Bundle;
xmlns:android="http://schemas.android.com/apk/res/android" import android.app.*;
xmlns:app="http://schemas.android.com/apk/res-auto" import android.widget.Toast;
xmlns:tools="http://schemas.android.com/tools" import android.widget.ToggleButton;
android:id="@+id/main" import android.view.*;
android:layout_width="match_parent" public class MainActivity extends Activity {
android:layout_height="match_parent" ToggleButton tobu;
tools:context=".MainActivity"> @Override
<TextView protected void onCreate(Bundle
android:layout_width="wrap_content" savedInstanceState) {
android:layout_height="wrap_content" super.onCreate(savedInstanceState);
android:text="Bluetooth " setContentView(R.layout.activity_main);
android:textSize="25sp" tobu=findViewById(R.id.bt);
android:layout_above="@id/bt" }
android:layout_centerHorizontal="true" public void toggle(View v)
android:textStyle="italic|bold"/> {
<ToggleButton if(tobu.isChecked())
android:id="@+id/bt" Toast.makeText(this,"Bluetooth is On
android:layout_width="wrap_content" ",Toast.LENGTH_SHORT).show();
android:layout_height="wrap_content" else
android:layout_centerInParent="true" Toast.makeText(this,"Bluetooth is
android:textOff="Off" Off",Toast.LENGTH_SHORT).show();
android:textOn="On" }
android:background="#3944bc" }
android:textSize="20sp"
android:textColor="#ffffff"
android:onClick="toggle"/>
</RelativeLayout>
Q.2 Write a program to create a simple calculator.
activity_main.xml MainActivity.java
<?xml version="1.0" encoding="utf-8"?> package com.example.exp9_2;
<TableLayout import android.os.Bundle;
xmlns:android="http://schemas.android.com/apk/res/android" import android.view.View;
xmlns:app="http://schemas.android.com/apk/res-auto" import android.widget.EditText;
xmlns:tools="http://schemas.android.com/tools" import android.widget.TextView;
android:id="@+id/main"
android:layout_width="match_parent" import
android:layout_height="match_parent" androidx.appcompat.app.AppCompatActivity;
tools:context=".MainActivity"
android:layout_marginTop="40dp" public class MainActivity extends
android:layout_marginLeft="15dp" AppCompatActivity {
android:layout_marginRight="15dp"> EditText etnum1,etnum2;
<TextView TextView tvres;
android:layout_width="wrap_content" @Override
android:text="Simple calculator" protected void onCreate(Bundle
android:textSize="50sp" savedInstanceState) {
android:gravity="center" super.onCreate(savedInstanceState);
android:textStyle="bold|italic" setContentView(R.layout.activity_main);
android:layout_height="wrap_content"/> etnum1=findViewById(R.id.enum1);
<TableRow etnum2=findViewById(R.id.enum2);
android:weightSum="2"> tvres=findViewById(R.id.res);
<TextView }
android:layout_weight="1" public void Add(View v)
android:layout_width="wrap_content" {
android:layout_height="wrap_content" int
android:text="Enter Num 1" n1=Integer.parseInt(etnum1.getText().toString());
android:textSize="18sp"/> int
<EditText n2=Integer.parseInt(etnum2.getText().toString());
android:layout_weight="1" tvres.setText("Addition is :"+(n1+n2));
android:layout_height="50dp" }
android:layout_width="100dp" public void Sub(View v)
android:id="@+id/enum1" {
android:textSize="18sp" int
android:inputType="numberSigned"/> n1=Integer.parseInt(etnum1.getText().toString());
</TableRow> int
<TableRow n2=Integer.parseInt(etnum2.getText().toString());
android:weightSum="2"> tvres.setText("Substracttion is :"+(n1-n2));
<TextView }
android:layout_weight="1" public void Multi(View v)
android:layout_width="wrap_content" {
android:layout_height="wrap_content" int
android:text="Enter Num 2" n1=Integer.parseInt(etnum1.getText().toString());
android:textSize="18sp"/> int
<EditText n2=Integer.parseInt(etnum2.getText().toString());
android:layout_weight="1" tvres.setText("Multiplication is :"+(n1*n2));
android:layout_height="50dp" }
android:layout_width="100dp" public void Div(View v)
android:id="@+id/enum2" {
android:textSize="18sp" int
android:inputType="numberSigned"/> n1=Integer.parseInt(etnum1.getText().toString());
</TableRow> int
<TableRow n2=Integer.parseInt(etnum2.getText().toString());
android:weightSum="4" tvres.setText("Divition is :"+(n1/n2));
android:layout_marginTop="10dp"> }
<Button }
android:text="Add"
android:onClick="Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:text="Sub"
android:onClick="Sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:text="Multi"
android:onClick="Multi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:text="Div"
android:onClick="Div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:layout_marginTop="10dp">
<TextView
android:id="@+id/res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"/>
</TableRow>
</TableLayout>
Exp9_1. Output Exp9_2. Output