0% found this document useful (0 votes)
103 views

Android Pgms

The document discusses various Android UI components and permissions. It includes code snippets to create a database, add an image button, and implement a Celsius to Fahrenheit converter. It also provides code for sending SMS messages using permissions, and describes how to retrieve a user's current location. The last section shows code for a calculator app layout using a TableLayout.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

Android Pgms

The document discusses various Android UI components and permissions. It includes code snippets to create a database, add an image button, and implement a Celsius to Fahrenheit converter. It also provides code for sending SMS messages using permissions, and describes how to retrieve a user's current location. The last section shows code for a calculator app layout using a TableLayout.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1) Create a database :

<?xml version="1.0" encoding="utf-8"?>


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

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="create_db"
android:text="Create database"/>

</LinearLayout>

package com.example.msbte_exam_practice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


// Button create;
SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void create_db(View view){
sqLiteDatabase=openOrCreateDatabase("USERS", Context.MODE_PRIVATE,null);
if(sqLiteDatabase==null){
Toast.makeText(this, "DB not Created", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "DB Created", Toast.LENGTH_SHORT).show();
}
}
}

2] image button
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/teal_700"
android:src="@drawable/img"/>
3) absolute layout

4) date and time picker

5) demonstrate declaring and using permissions with any relevant example

To send sms :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:padding="20dp"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/edtPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Phone Number"
android:textSize="20sp"></EditText>
<EditText
android:id="@+id/edtMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Message"
android:textSize="20sp"></EditText>
<Button
android:id="@+id/btnsendSMS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Send SMS"></Button>
</LinearLayout>

MANIFEST :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permisssion.SEND_SMS"/>

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.MSBTE_EXAM_Practice"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Java:
package com.example.msbte_exam_practice;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.Manifest;
public class MainActivity extends AppCompatActivity {
Button snd;
EditText phno,msg;
SmsManager smsManager;

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

requestPermissions(new String[] { Manifest.permission.SEND_SMS},100);

edtPhoneNumber = findViewById(R.id.edtPhoneNumber);
edtMessage = findViewById(R.id.edtMessage);
btnSendSMS = findViewById(R.id.btnsendSMS);

btnSendSMS.setOnClickListener(view->{
String phoneNumber = edtPhoneNumber.getText().toString();
String message = edtMessage.getText().toString();

SmsManager smsManager = SmsManager.getDefault();


smsManager.sendTextMessage(phoneNumber,"7744981364",message,null,null);
Toast.makeText(getApplicationContext(), "SMS Sent Sucessfully!!!",
Toast.LENGTH_SHORT).show();
});

}
7) Celsius to farahnite and vice a varsa
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<EditText
android:id="@+id/temp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter temperature"/>
<ToggleButton
android:id="@+id/tbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="F to C"
android:layout_gravity="center"
android:textOn="C to F"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="click"
android:id="@+id/btn"/>

</LinearLayout>

Java:
package com.example.msbte_exam_practice;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {


ToggleButton tb;
EditText e1;

Button b1;
Double a;

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

tb=findViewById(R.id.tbtn);
e1=findViewById(R.id.temp);
b1=findViewById(R.id.btn);

b1.setOnClickListener(view -> {
if(tb.isChecked()){
a=Double.parseDouble(String.valueOf(e1.getText()));
Double b=a*9/5+32;
String r=String.valueOf(b);
Toast.makeText(this,r+ "F", Toast.LENGTH_SHORT).show();
}
else{
a=Double.parseDouble(String.valueOf(e1.getText()));
Double b=a-32;
Double c=b*5/9;
String r=String.valueOf(c);
Toast.makeText(this, r+"C", Toast.LENGTH_SHORT).show();
}
});

8) camera pr program

9) send and receive sms :- pr program

10) activity lifecycle : pr pgm

11) users current location – pr pgm(notebook)

12) see que in s22 paper last que


<?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:stretchColumns="*"

tools:context=".MainActivity">

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b0"
android:layout_marginHorizontal="20dp"
android:text="0"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:layout_marginHorizontal="30dp"
android:text="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b2"
android:layout_marginHorizontal="20dp"
android:text="2"/>

</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b3"
android:layout_marginHorizontal="20dp"
android:text="3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b4"
android:layout_marginHorizontal="20dp"
android:text="4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b5"
android:layout_marginHorizontal="20dp"
android:text="5"/>

</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b6"
android:layout_marginHorizontal="20dp"
android:text="6"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b7"
android:layout_marginHorizontal="20dp"
android:text="7"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b8"
android:layout_marginHorizontal="20dp"
android:text="8"/>

</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b9"
android:layout_marginHorizontal="20dp"
android:text="9"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/submit"
android:layout_marginHorizontal="20dp"
android:text="submit"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/clear"
android:layout_marginHorizontal="20dp"
android:text="clear"/>
</TableRow>
</TableLayout>

Java :
package com.example.msbte_exam_practice;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9, clear,submit;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

StringBuilder txt=new StringBuilder();


b0=findViewById(R.id.b0);
b1=findViewById(R.id.b1);
b2=findViewById(R.id.b2);
b3=findViewById(R.id.b3);
b4=findViewById(R.id.b4);
b5=findViewById(R.id.b5);
b6=findViewById(R.id.b6);
b7=findViewById(R.id.b7);
b8=findViewById(R.id.b8);
b9=findViewById(R.id.b9);

clear=findViewById(R.id.clear);
submit=findViewById(R.id.submit);

b0.setOnClickListener(view -> {
txt.append(b0.getText().toString()+" ");
});
b1.setOnClickListener(view -> {
txt.append(b1.getText().toString()+" ");
});
b2.setOnClickListener(view -> {
txt.append(b2.getText().toString()+" ");
});
b3.setOnClickListener(view -> {
txt.append(b3.getText().toString()+" ");
});
b4.setOnClickListener(view -> {
txt.append(b4.getText().toString()+" ");
});
b5.setOnClickListener(view -> {
txt.append(b5.getText().toString()+" ");
});
b6.setOnClickListener(view -> {
txt.append(b6.getText().toString()+" ");
});
b7.setOnClickListener(view -> {
txt.append(b7.getText().toString()+" ");
});
b8.setOnClickListener(view -> {
txt.append(b8.getText().toString()+" ");
});
b9.setOnClickListener(view -> {
txt.append(b9.getText().toString()+" ");
});
submit.setOnClickListener(view -> {
Toast.makeText(this, "You have clicked :"+txt, Toast.LENGTH_SHORT).show();
});
clear.setOnClickListener(view->{
txt.setLength(0);
});

13) rectangular progress bar :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

<ProgressBar
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:indeterminate="false"
android:max="100"
android:minHeight="50dp"
android:maxWidth="200dp"
android:progress="1"/>
<TextView
android:id="@+id/txt"
android:layout_marginTop="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="progressstatus"/>

</LinearLayout>

Java :
package com.example.msbte_exam_practice;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


ProgressBar pb;
int progressStatus=0;
TextView txt;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

pb=findViewById(R.id.pb);
txt=findViewById(R.id.txt);

new Thread(new Runnable() {


@Override
public void run() {
while(progressStatus<100){
progressStatus+=1;
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
pb.setProgress(progressStatus);
txt.setText(progressStatus+"/"+pb.getMax());
}
});
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();

}
14) geocoding and reverse geocoding

15) send and receive mail -> pr pgm

16) Bluetooth connectivity on,off,discoverable-> pr pgm

17) list view of 5 items, grid view of 4*4 items, image view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:padding="30dp"
tools:context=".MainActivity">

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"/>
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/grid"
android:columnWidth="50dp"
android:layout_gravity="center"
android:numColumns="4"
android:stretchMode="columnWidth"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/image"
android:src="@drawable/img"/>

</LinearLayout>

Java :
package com.example.msbte_exam_practice;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {


String [] arr={"Apple","Banana","Mango","Orange","Kivi"};
static final String[] alphabets=new String[]{
"A","B","C","D",
"E","F","G","H",
"I","J","K","K",
"L","M","N","O"
};
ArrayAdapter adp1,adp2;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// listview->
adp1=new ArrayAdapter(this,android.R.layout.simple_list_item_1,arr);
ListView li=findViewById(R.id.list);
li.setAdapter(adp1);

// gridview
GridView grid=findViewById(R.id.grid);
adp2=new ArrayAdapter(this, android.R.layout.simple_list_item_1,alphabets);
grid.setAdapter(adp2);

18) store customer details , and retrieve info using cid


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:padding="30dp"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Insert Customer Details"
android:textSize="20sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etid"
android:hint="ID"
android:layout_marginTop="20dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etname"
android:hint="Name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etmbno"
android:hint="Mobile Number"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etaddress"
android:hint="Address"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etpin"
android:hint="PinCode"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Insert Data"
android:layout_marginTop="30dp"
android:id="@+id/btnInsert"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etsearchid"
android:layout_marginTop="30dp"
android:hint="Enter id To search"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search Data"
android:layout_marginTop="30dp"
android:id="@+id/btnSearch"/>

</LinearLayout>
Java:
package com.example.msbte_exam_practice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText etid,etname,etmbno,etaddress,etpin,etsearchid;
String cid,cname,cmbno,caddress,cpin,sid,insert_query;
Button btninsert,btnsearch;
SQLiteDatabase sqLiteDatabase;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

etid=findViewById(R.id.etid);
etname=findViewById(R.id.etname);
etmbno=findViewById(R.id.etmbno);
etaddress=findViewById(R.id.etaddress);
etpin=findViewById(R.id.etpin);
etsearchid=findViewById(R.id.etsearchid);

btninsert=findViewById(R.id.btnInsert);
btnsearch=findViewById(R.id.btnSearch);
btninsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sqLiteDatabase=openOrCreateDatabase("USERS", Context.MODE_PRIVATE,null);
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS usersdata(id INTEGER
PRIMARY KEY AUTOINCREMENT NOT NULL,cid VARCHAR,name VARCHAR,mobile VARCHAR,address
VARCHAR, pincode VARCHAR );");

cid=etid.getText().toString();
cname=etname.getText().toString();
cmbno=etmbno.getText().toString();
caddress=etaddress.getText().toString();
cpin=etpin.getText().toString();

insert_query="INSERT INTO usersdata (cid,name,mobile,address,pincode)


VALUES (?,?,?,?,?);";
Object[] insertparams=new Object[]{cid,cname,cmbno,caddress,cpin};
sqLiteDatabase.execSQL(insert_query,insertparams);
Toast.makeText(MainActivity.this, "Data Inserted",
Toast.LENGTH_SHORT).show();
}
});

btnsearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sid=etsearchid.getText().toString();
Cursor cursor=sqLiteDatabase.rawQuery("select * from usersdata where
cid="+sid+"",null);
StringBuffer str=new StringBuffer();
while(cursor.moveToNext()){
String cid=cursor.getString(1);
String name=cursor.getString(2);
String mob=cursor.getString(3);
String addr=cursor.getString(4);
String pcode=cursor.getString(5);
str.append(cid+" "+name+" "+mob+" "+addr+" "+pcode+" ");
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
}
});
}

19) draw the location from current location to msbte bandra

20) calculator using relative layout:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="30dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculator"
android:textSize="20sp"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:hint="Enter number 1"
android:id="@+id/no1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/no1"
android:id="@+id/no2"
android:hint="Enter number 2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/no2"
android:layout_marginTop="30dp"
android:text="+"
android:id="@+id/add"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/add"
android:layout_below="@+id/no2"
android:layout_marginTop="30dp"
android:text="-"
android:layout_marginLeft="20dp"
android:id="@+id/sub"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/add"
android:text="*"
android:id="@+id/mul"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/div"
android:layout_below="@id/sub"
android:layout_toRightOf="@+id/mul"
android:layout_marginLeft="20dp"
android:text="/"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/mul"
android:layout_marginTop="40dp"
android:id="@+id/res"/>

</RelativeLayout>
Java:
package com.example.calculator;

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 android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText t1,t2;
Button b1,b2,b3,b4;
TextView res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

t1=findViewById(R.id.no1);
t2=findViewById(R.id.no2);

b1=findViewById(R.id.add);
b2=findViewById(R.id.sub);
b3=findViewById(R.id.mul);
b4=findViewById(R.id.div);

res=findViewById(R.id.res);

b1.setOnClickListener(view -> {
int
sum=Integer.parseInt(t1.getText().toString())+Integer.parseInt(t2.getText().toString());
res.setText("Addition is : "+sum);
});
b2.setOnClickListener(view->{
int sub=Integer.parseInt(t1.getText().toString())-
Integer.parseInt(t2.getText().toString());
res.setText("Subtraction is : "+sub);
});
b3.setOnClickListener(view -> {
int
mul=Integer.parseInt(t1.getText().toString())*Integer.parseInt(t2.getText().toString());
res.setText("Multiplication is : "+mul);
});
b4.setOnClickListener(view -> {
int
div=Integer.parseInt(t1.getText().toString())/Integer.parseInt(t2.getText().toString());
res.setText("Division is : "+div);
});

}
}

You might also like