0% found this document useful (0 votes)
10 views3 pages

Prac 7

Uploaded by

kaverinagare39
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)
10 views3 pages

Prac 7

Uploaded by

kaverinagare39
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/ 3

Practical No:=7

Title:=Implement a program using EditText and TextView


Rollno:=26 DOP:=28/01/25 Batch:=B
SOURCE CODE:= android:id="@+id/e2"
<?xml version="1.0" encoding="utf-8"?> android:hint="Enter second number"
<LinearLayout android:layout_marginTop="30sp"/>
xmlns:android="http://schemas.android.co
m/apk/res/android" <LinearLayout
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/a android:layout_height="wrap_content"
pk/res-auto" android:orientation="horizontal"
android:weightSum="4"
xmlns:tools="http://schemas.android.com/t android:gravity="center"
ools" android:layout_marginTop="30sp">
android:layout_width="match_parent"
android:layout_height="match_parent" <Button
android:orientation="vertical"> android:layout_width="0sp"

<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="@+id/b1"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="Simple Calculator" android:text="+"
android:id="@+id/t1" android:textSize="25sp"/>
android:textSize="40sp"
android:layout_gravity="center" <Button
android:layout_marginTop="20sp" android:layout_width="0sp"
android:textStyle="bold"/>
android:layout_height="wrap_content"
<EditText android:id="@+id/b2"
android:layout_width="match_parent" android:layout_weight="1"
android:layout_height="wrap_content" android:text="-"
android:id="@+id/e1" android:textSize="25sp"/>
android:hint="Enter first number"
android:layout_marginTop="30sp"/> <Button
android:layout_width="0sp"
<EditText
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:id="@+id/b3"
android:layout_weight="1" private Button b1, b2, b3, b4;
android:text="*" private TextView t2;
android:textSize="25sp"/> private EditText e1, e2;

<Button @Override
android:layout_width="0sp" protected void onCreate(Bundle
savedInstanceState) {
android:layout_height="wrap_content" super.onCreate(savedInstanceState);
android:id="@+id/b4"
android:layout_weight="1" setContentView(R.layout.activity_main);
android:text="/"
android:textSize="25sp"/> b1 = findViewById(R.id.b1);
</LinearLayout> b2 = findViewById(R.id.b2);
b3 = findViewById(R.id.b3);
<TextView b4 = findViewById(R.id.b4);
android:layout_width="wrap_content" e1 = findViewById(R.id.e1);
android:layout_height="wrap_content" e2 = findViewById(R.id.e2);
android:id="@+id/t2" t2 = findViewById(R.id.t2);
android:text="Answer:="
android:textSize="25sp" b1.setOnClickListener(this::calculate);
android:layout_marginTop="30sp" b2.setOnClickListener(this::calculate);
android:layout_gravity="center"/> b3.setOnClickListener(this::calculate);
</LinearLayout> b4.setOnClickListener(this::calculate);
MainActivity.java }
package com.example.calsample;
public void calculate(View v) {
import android.os.Bundle; String s1 = e1.getText().toString().trim();
import android.view.View; String s2 = e2.getText().toString().trim();
import android.widget.Button;
import android.widget.EditText; if (s1.isEmpty() || s2.isEmpty()) {
import android.widget.TextView; t2.setText("Please enter numbers");
import return;
androidx.appcompat.app.AppCompatActivit }
y;
int num1 = Integer.parseInt(s1);
public class MainActivity extends int num2 = Integer.parseInt(s2);
AppCompatActivity { int result = 0;
if (v == b1) { else
result = num1 + num2; {
} else if (v == b2) { t2.setText("Invalid operation");
result = num1 - num2; return;
} else if (v == b3) { }
result = num1 * num2;
} else if (v == b4) { t2.setText("Result: " + result);
result = num1 / num2; }
} }

OUTPUT:=

You might also like