How To Create Simple Calculator Application In Android Studio, Android Studio Application Development Tutorials

Hello Everyone, Today i am going to show you how to make a simple calculator in android studio. Basically in this calculator i will perform Five basic operation. First is SUM, SUBTRACTION, MULTIPLICATION, DIVISION AND RESET. I have tried to make this tutorial in very simple method. Hope this will be helpful for you to create Your Calculator in android studio.

First of all while building android app we first of all need the design User Interface of Application. For that the following codes are given which was used in activity_main.xml

activity_main.xml


     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-2709908168270813"
     data-ad-slot="1165711394">



<?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"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter First Number"
        android:id="@+id/txt1"
        android:inputType="number"
        />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Second Number"
        android:id="@+id/txt2"
        android:inputType="number"
        />
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/res"
        />
    <TableRow>
        <Button
            android:layout_width="190dp"
            android:layout_height="180dp"
            android:id="@+id/plus"
            android:text="+"
            android:textSize="100dp"
            android:textColor="@color/colorAccent"
            />
        <Button
            android:layout_width="190dp"
            android:layout_height="180dp"
            android:id="@+id/minus"
            android:text="-"
            android:textSize="100dp"
            android:textColor="@color/colorAccent"
            />
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="190dp"
            android:layout_height="180dp"
            android:id="@+id/multi"
            android:text="*"
            android:textSize="100dp"
            android:textColor="@color/colorAccent"
            />
        <Button
            android:layout_width="190dp"
            android:layout_height="180dp"
            android:id="@+id/divide"
            android:text="/"
            android:textSize="100dp"
            android:textColor="@color/colorAccent"
            />
    </TableRow>
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/reset"
        android:text="RESET"
        android:textSize="20dp"
        android:textColor="@color/colorAccent"
        />

</TableLayout>




Here Is The Code Used in Mainactivity.java



package com.myhindiadvice.www.simplecalculator;

import android.support.v7.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 {

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

    EditText txt1,txt2;
    TextView result;
    Button plus,minus,multiply,divide,reset;

    public void OnCLick()
    {
       txt1=(EditText)findViewById(R.id.txt1);
       txt2=(EditText)findViewById(R.id.txt2);
       result=(TextView)findViewById(R.id.res);
       plus=(Button)findViewById(R.id.plus);
        minus=(Button)findViewById(R.id.minus);
        multiply=(Button)findViewById(R.id.multi);
        divide=(Button)findViewById(R.id.divide);
        reset=(Button)findViewById(R.id.reset);

        plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a=Integer.parseInt(txt1.getText().toString());
                int b=Integer.parseInt(txt2.getText().toString());
                int c=a+b;
                result.setText(String.valueOf(c));
            }
        });

        minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a=Integer.parseInt(txt1.getText().toString());
                int b=Integer.parseInt(txt2.getText().toString());
                int c=a-b;
                result.setText(String.valueOf(c));
            }
        });

        multiply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a=Integer.parseInt(txt1.getText().toString());
                int b=Integer.parseInt(txt2.getText().toString());
                int c=a*b;
                result.setText(String.valueOf(c));
            }
        });

        divide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a=Integer.parseInt(txt1.getText().toString());
                int b=Integer.parseInt(txt2.getText().toString());
                int c=a/b;
                result.setText(String.valueOf(c));
            }
        });
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txt1.setText("");
                txt2.setText("");
                result.setText("");
            }
        });

    }
}


This is how our application looked like after designing its user interface. it is not a professional look, you can edit it as per your requirement and as you want. this is just only for example so that i have made it very simple so that everyone can understand it easily.

And after entering the values in EditText, i have performed SUM or plus operation and it showed the final result in Textview.


Hope It was helpful for you to understand how to make Simple Calculator In Android Studio. if you are facing any problem just comment below or mail me personally.
ThankYou

Previous Post
Next Post

post written by:

3 comments: