In this post, we will see How to Create Calculator in Android Studio or Android App for Calculator.
How to create Android First App Hello World using Android Studio, check in following link:
https://www.comrevo.com/2019/09/Android-First-App-Hello-World-using-Android-Studio.html
Find How to create Android App for addition of two numbers with details in following link:
https://www.comrevo.com/2019/09/android-app-for-addition-of-two-numbers.html
Program (activity_main.xml)
Design Mode:
Add TextView, Button, EditText (Plain Text) as shown in following screenshot. Also edit their attributes as per your requirements.
Text Mode:
Text Mode will get automatically updated. If you want you can update it.
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="47dp" android:layout_marginTop="57dp" android:text="First Number" android:textSize="24sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_marginTop="57dp" android:ems="10" android:inputType="textPersonName" app:layout_constraintStart_toEndOf="@+id/textView1" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="47dp" android:layout_marginTop="47dp" android:text="Second Number" android:textSize="24sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView1" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="9dp" android:layout_marginTop="34dp" android:ems="10" android:inputType="textPersonName" android:textSize="24sp" app:layout_constraintStart_toEndOf="@+id/textView2" app:layout_constraintTop_toBottomOf="@+id/editText1" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="44dp" android:layout_marginTop="48dp" android:text="ADD" android:textSize="24sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editText2" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="40dp" android:layout_marginTop="48dp" android:text="SUB" android:textSize="24sp" app:layout_constraintStart_toEndOf="@+id/button1" app:layout_constraintTop_toBottomOf="@+id/editText2" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="148dp" android:layout_marginTop="112dp" android:text="Result" android:textSize="24sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="47dp" android:layout_marginTop="24dp" android:text="MUL" android:textSize="24sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button1" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="37dp" android:layout_marginTop="25dp" android:text="DIV" android:textSize="24sp" app:layout_constraintStart_toEndOf="@+id/button3" app:layout_constraintTop_toBottomOf="@+id/button2" /> </androidx.constraintlayout.widget.ConstraintLayout> |
Program (MainActivity.java)
package com.example.calculatorapp; 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; EditText number2; Button Add_button, Sub_button, Mul_button, Div_button; TextView result; int ans=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); number1=(EditText) findViewById(R.id.editText1); number2=(EditText) findViewById(R.id.editText2); Add_button=(Button) findViewById(R.id.button1); Sub_button=(Button) findViewById(R.id.button2); Mul_button=(Button) findViewById(R.id.button3); Div_button=(Button) findViewById(R.id.button4); result = (TextView) findViewById(R.id.textView3); // Register Add_button for event Add_button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Declare num1 and num2 as double type // get data which is in edittext, convert it to string // use parseDouble method to convert String to Double type double num1 = Double.parseDouble(number1.getText().toString()); double num2 = Double.parseDouble(number2.getText().toString()); // add both number and store it to sum double sum = num1 + num2; // set it to result textview result.setText(""+sum); } } ); Sub_button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { double num1 = Double.parseDouble(number1.getText().toString()); double num2 = Double.parseDouble(number2.getText().toString()); // Subtract second number from first number and store it to sub double sub = num1 - num2; // set it to result textview result.setText(""+sub); } } ); Mul_button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { double num1 = Double.parseDouble(number1.getText().toString()); double num2 = Double.parseDouble(number2.getText().toString()); // Multiply both numbers and store it to mul double mul = num1 * num2; // set it to result textview result.setText(""+mul); } } ); Div_button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { double num1 = Double.parseDouble(number1.getText().toString()); double num2 = Double.parseDouble(number2.getText().toString()); // Divide first number by second and store it to div double div = num1 / num2; // set it to result textview result.setText(""+div); } } ); } } |
Output:
Screenshots of the this app from mobile phone is as follows:
Addition of Two Numbers:
Subtraction:
Multiplication:
Division:
No comments:
Post a Comment