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

Simple Calculator App Code Example

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)
15 views3 pages

Simple Calculator App Code Example

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

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="[Link]
m/apk/res/android" <LinearLayout
android:layout_width="match_parent"
xmlns:app="[Link] android:layout_height="wrap_content"
pk/res-auto" android:orientation="horizontal"
android:weightSum="4"
xmlns:tools="[Link] 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" [Link](savedInstanceState);
android:id="@+id/b4"
android:layout_weight="1" setContentView([Link].activity_main);
android:text="/"
android:textSize="25sp"/> b1 = findViewById([Link].b1);
</LinearLayout> b2 = findViewById([Link].b2);
b3 = findViewById([Link].b3);
<TextView b4 = findViewById([Link].b4);
android:layout_width="wrap_content" e1 = findViewById([Link].e1);
android:layout_height="wrap_content" e2 = findViewById([Link].e2);
android:id="@+id/t2" t2 = findViewById([Link].t2);
android:text="Answer:="
android:textSize="25sp" [Link](this::calculate);
android:layout_marginTop="30sp" [Link](this::calculate);
android:layout_gravity="center"/> [Link](this::calculate);
</LinearLayout> [Link](this::calculate);
[Link] }
package [Link];
public void calculate(View v) {
import [Link]; String s1 = [Link]().toString().trim();
import [Link]; String s2 = [Link]().toString().trim();
import [Link];
import [Link]; if ([Link]() || [Link]()) {
import [Link]; [Link]("Please enter numbers");
import return;
[Link] }
y;
int num1 = [Link](s1);
public class MainActivity extends int num2 = [Link](s2);
AppCompatActivity { int result = 0;
if (v == b1) { else
result = num1 + num2; {
} else if (v == b2) { [Link]("Invalid operation");
result = num1 - num2; return;
} else if (v == b3) { }
result = num1 * num2;
} else if (v == b4) { [Link]("Result: " + result);
result = num1 / num2; }
} }

OUTPUT:=

You might also like