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

Mad Exp 3

The document describes how to create a simple calculator application with an activity and intents in Android. It includes code to build the user interface with EditTexts and Buttons in XML layout, and code in the activity class to handle button clicks and perform calculations by parsing the EditText values.

Uploaded by

taehyung kim
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)
33 views

Mad Exp 3

The document describes how to create a simple calculator application with an activity and intents in Android. It includes code to build the user interface with EditTexts and Buttons in XML layout, and code in the activity class to handle button clicks and perform calculations by parsing the EditText values.

Uploaded by

taehyung kim
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/ 7

Exp 3

Design simple GUI application with activity and intents e.g. calculator

Create new empty activity

Name application “simplecalculator”

Select language java

Acitivity_main.xml
<?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/number1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal" />

<EditText
android:id="@+id/number2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:hint="Enter second number" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/addButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />

<Button
android:id="@+id/subtractButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Subtract" />

<Button
android:id="@+id/multiplyButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Multiply" />

<Button
android:id="@+id/divideButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Divide" />
</LinearLayout>

<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result will be displayed here"
android:textSize="24sp" />

</LinearLayout>

Main activity.java

package com.example.simplecalculator;

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;

public class MainActivity extends AppCompatActivity {


EditText number1, number2;
Button addButton, subtractButton, multiplyButton, divideButton;
TextView result;

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

number1 = findViewById(R.id.number1);
number2 = findViewById(R.id.number2);
addButton = findViewById(R.id.addButton);
subtractButton = findViewById(R.id.subtractButton);
multiplyButton = findViewById(R.id.multiplyButton);
divideButton = findViewById(R.id.divideButton);
result = findViewById(R.id.result);

addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
double res = num1 + num2;
result.setText("Result: " + res);
}
});

subtractButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
double res = num1 - num2;
result.setText("Result: " + res);
}
});

multiplyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
double res = num1 * num2;
result.setText("Result: " + res);
}
});

divideButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double num1 = Double.parseDouble(number1.getText().toString());
double num2 = Double.parseDouble(number2.getText().toString());
double res = num1 / num2;
result.setText("Result: " + res);
}
});
}
}
Output

You might also like