How To Create Basic Calculator In Java (calculator java code)

Welcome to my Blog

I am back with another programming tutorial. In this video I would tell you how to create basic android java calculator. You would need Android Studio to create this calculator. This would be a java Calculator for Android and hence it would be an apk file. You would be able to run this apk in any android phone. This calculator might have some errors because it is a basic calculator. It need several try catch exceptions to make it perfect and it would create it lengthy. If you want to learn to create android apps follow this blog because I would helping you by providing java tutorials and codes for whatever you want and if you want to learn the basic of java just go to SoloLearn. If you want to Understand how this calculator works I have another post on that. Check out my blog for more helpful posts.

This post has been created for those who know how to set up Android Studio and also know to open a project. If you don’t know the both I have another post on that check that out. You just have to copy and paste the code provided in your Activity_main.xml file and the other code in your MainActivity.java file

 

This calculator Would Look Like This

jjjjj

Activity_main.xml

(The name of your mane activity under layout section)

MainActivity1

Just Copy And Paste The Code In Your xml file text.

This line (<?xml version=”1.0″ encoding=”utf-8″?>) might be different in your case so don’t replace that.

<?xml version="1.0" encoding="utf-8"?>
//Ignore the above line and don't replace it.

<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:layout_weight="1"
    android:gravity="bottom|left|right"
    android:orientation="vertical"
    android:weightSum="4"
    tools:context="com.matt.calc.MainActivity">

    <EditText
        android:id="@+id/edit2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:text=""
        android:textAlignment="textEnd"
        android:textColor="@android:color/holo_blue_bright"
        android:textSize="30sp" />

    <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:text=""
        android:textAlignment="textEnd"
        android:textColor="@android:color/holo_blue_bright"
        android:textSize="30sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:weightSum="10">

        <Button
            android:id="@+id/decimal"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="0dp"
            android:layout_weight="3"
            android:background="@android:color/holo_blue_dark"
            android:padding="0dp"
            android:text="."
            android:textColor="@android:color/white"
            android:textSize="30sp" />

        <Button
            android:id="@+id/Bspace"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="7"
            android:background="@android:color/holo_blue_dark"
            android:text="@string/backspace"
            android:textColor="@android:color/white"
            android:textSize="20sp" />
    </LinearLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="384dp"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="155dp">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/Seven"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_bright"
                android:text="@string/_7"
                android:textColor="@android:color/white"
                android:textSize="40sp" />

            <Button
                android:id="@+id/Eight"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_bright"
                android:text="@string/_8"
                android:textColor="@android:color/white"
                android:textSize="40sp"
                tools:ignore="ButtonStyle" />

            <Button
                android:id="@+id/Nine"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_bright"
                android:text="@string/_9"
                android:textColor="@android:color/white"
                android:textSize="40sp"
                tools:ignore="ButtonStyle" />

            <Button
                android:id="@+id/Plus"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_dark"
                android:text="@string/plus"
                android:textColor="@android:color/white"
                android:textSize="30sp"
                tools:ignore="ButtonStyle" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/Four"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_bright"
                android:text="@string/_4"
                android:textColor="@android:color/white"
                android:textSize="40sp"
                tools:ignore="ButtonStyle" />

            <Button
                android:id="@+id/Five"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_bright"
                android:text="@string/_5"
                android:textColor="@android:color/white"
                android:textSize="40sp"
                tools:ignore="ButtonStyle" />

            <Button
                android:id="@+id/Six"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_bright"
                android:text="@string/_6"
                android:textColor="@android:color/white"
                android:textSize="40sp"
                tools:ignore="ButtonStyle" />

            <Button
                android:id="@+id/Minus"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_dark"
                android:text="@string/minus"
                android:textColor="@android:color/white"
                android:textSize="40sp"
                tools:ignore="ButtonStyle" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/One"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_bright"
                android:text="@string/_1"
                android:textColor="@android:color/white"
                android:textSize="40sp"
                tools:ignore="ButtonStyle" />

            <Button
                android:id="@+id/Two"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_bright"
                android:text="@string/_2"
                android:textColor="@android:color/white"
                android:textSize="40sp"
                tools:ignore="ButtonStyle" />

            <Button
                android:id="@+id/Three"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_bright"
                android:text="@string/_3"
                android:textColor="@android:color/white"
                android:textSize="40sp"
                tools:ignore="ButtonStyle" />

            <Button
                android:id="@+id/Div"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_dark"
                android:text="@string/divide"
                android:textColor="@android:color/white"
                android:textSize="40sp"
                tools:ignore="ButtonStyle" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/Clear"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_dark"
                android:text="@string/cc"
                android:textColor="@android:color/white"
                android:textSize="40sp" />

            <Button
                android:id="@+id/Zero"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_bright"
                android:text="@string/_0"
                android:textColor="@android:color/white"
                android:textSize="40sp" />

            <Button
                android:id="@+id/Equal"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_dark"
                android:text="@string/EQU"
                android:textColor="@android:color/white"
                android:textSize="40sp" />

            <Button
                android:id="@+id/Mult"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_margin="0dp"
                android:layout_weight="1"
                android:background="@android:color/holo_blue_dark"
                android:text="@string/multiply"
                android:textColor="@android:color/white"
                android:textSize="40sp" />
        </TableRow>
    </TableLayout>

</LinearLayout>

MainActivity.java

(The name of your main activity under java section)

This is the code you have to put inside your MainActivity.java file.

MainActivity

This line (package com.matt.calc;) might be different in your case so don’t replace that.

package com.matt.calc;

//this is my package name your would be different so change it accordingly

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button one, two, three, four, five, six, seven, eight, nine, zero, plus, minus, mult, div, clear, equal, bspace, decimal;
    private EditText edit1, edit2;
    private char ACTION = '0';
    private final char ADDITION = '+';
    private final char SUBTRACTION = '-';
    private final char MULTIPLICATION = '*';
    private final char DIVISION = '/';
    private final char CLEAR = 'c';
    private char BACK = '<';
    private Double val1, val2;


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

        setUpUiViews();
        setOnClickListeners();
    }

    private void setOnClickListeners() {
        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit1.setText(edit1.getText().toString() + "1");

            }
        });
        two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit1.setText(edit1.getText().toString() + "2");

            }
        });
        three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit1.setText(edit1.getText().toString() + "3");

            }
        });
        four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit1.setText(edit1.getText().toString() + "4");

            }
        });
        five.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit1.setText(edit1.getText().toString() + "5");

            }
        });
        six.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit1.setText(edit1.getText().toString() + "6");

            }
        });
        seven.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit1.setText(edit1.getText().toString() + "7");

            }
        });
        eight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit1.setText(edit1.getText().toString() + "8");

            }
        });
        nine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit1.setText(edit1.getText().toString() + "9");

            }
        });
        zero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit1.setText(edit1.getText().toString() + "0");

            }
        });
        plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edit1.getText().length() <= 0) {
                    edit2.setText(null);
                    edit1.setText(null);
                }else if (edit2.getText().length() <= 0){
                    val1 = Double.parseDouble(edit1.getText().toString());
                    ACTION = ADDITION;
                    edit2.setText(edit1.getText().toString() + "+");
                    edit1.setText(null);
                }else{
                    val2 = Double.parseDouble(edit1.getText().toString());
                    compute();
                    edit2.setText(edit2.getText().toString() + String.valueOf(val2) + "+");
                    edit1.setText(String.valueOf(val1));
                }
            }
        });
        minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edit1.getText().length() <= 0) {
                    edit2.setText(null);
                    edit1.setText(null);
                }else if (edit2.getText().length() <= 0){
                    val1 = Double.parseDouble(edit1.getText().toString());
                    ACTION = SUBTRACTION;
                    edit2.setText(edit1.getText().toString() + "-");
                    edit1.setText(null);
                }else{
                    val2 = Double.parseDouble(edit1.getText().toString());
                    compute();
                    edit2.setText(edit2.getText().toString() + String.valueOf(val2) + "-");
                    edit1.setText(String.valueOf(val1));
                }
            }
        });
        div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edit1.getText().length() <= 0){
                    edit2.setText(null);
                    edit1.setText(null);
                }else if (edit2.getText().length() <= 0){
                    val1 = Double.parseDouble(edit1.getText().toString());
                    ACTION = DIVISION;
                    edit2.setText(String.valueOf(val1) + "/");
                    edit1.setText(null);
                }else{
                    val2 = Double.parseDouble(edit1.getText().toString());
                    compute();
                    edit2.setText(edit2.getText().toString() + String.valueOf(val2) + "/");
                    edit1.setText(String.valueOf(val1));
                }
            }
        });
        mult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edit1.getText().length() <= 0) {
                    edit2.setText(null);
                    edit1.setText(null);
                }else if (edit2.getText().length() <= 0){
                    val1 = Double.parseDouble(edit1.getText().toString());
                    ACTION = MULTIPLICATION;
                    edit2.setText(String.valueOf(val1) + "*");
                }else{
                    val2 = Double.parseDouble(edit1.getText().toString());
                    compute();
                    edit2.setText(edit2.getText().toString() + String.valueOf(val2) + "*");
                    edit1.setText(String.valueOf(val1));
                }
            }
        });
        equal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(edit1.getText().length() <= 0) {
                    edit2.setText(null);
                    edit1.setText(null);
                }else if (edit2.getText().length() <= 0){
                    edit2.setText(null);
                    edit1.setText(null);
                }else{
                    compute();
                    val2 = Double.parseDouble(edit1.getText().toString());
                    edit2.setText(edit2.getText().toString() + String.valueOf(val2) + "=");
                    edit1.setText(String.valueOf(val1));
                }
            }
        });
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit1.setText(null);
                edit2.setText(null);
            }
        });
        bspace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edit1.getText().length() > 0){
                    CharSequence value = edit1.getText().toString();
                    edit1.setText(value.subSequence(0, value.length() - 1));
                }else{
                    edit2.setText(null);
                    edit1.setText(null);
                }
            }
        });
        decimal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    double d= Double.valueOf(edit1.getText().toString());
                    if (d==(int)d){
                        edit1.setText(edit1.getText().toString() + ".");
                    }else{
                        edit1.setText(edit1.getText().toString());
                        Toast.makeText(getApplicationContext(), "Two Decimles Are Not Possible", Toast.LENGTH_SHORT).show();
                    }
                }catch(Exception e){
                    Toast.makeText(getApplicationContext(), "Type Properly", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    private void setUpUiViews() {
        one = findViewById(R.id.One);
        two = findViewById(R.id.Two);
        three = findViewById(R.id.Three);
        four = findViewById(R.id.Four);
        five = findViewById(R.id.Five);
        six = findViewById(R.id.Six);
        seven = findViewById(R.id.Seven);
        eight = findViewById(R.id.Eight);
        nine = findViewById(R.id.Nine);
        zero = findViewById(R.id.Zero);
        plus = findViewById(R.id.Plus);
        minus = findViewById(R.id.Minus);
        mult = findViewById(R.id.Mult);
        div = findViewById(R.id.Div);
        clear = findViewById(R.id.Clear);
        equal = findViewById(R.id.Equal);
        bspace = findViewById(R.id.Bspace);
        decimal = findViewById(R.id.decimal);
        edit1 = findViewById(R.id.edit1);
        edit2 = findViewById(R.id.edit2);
    }
    private void compute() {
        if (!Double.isNaN(val1)){
            val2 = Double.parseDouble(edit1.getText().toString());
            switch (ACTION) {
                case ADDITION:
                    val1 = val1 + val2;
                    break;
                case SUBTRACTION:
                    val1 = val1 - val2;
                    break;
                case MULTIPLICATION:
                    val1 = val1 * val2;
                    break;
                case DIVISION:
                    val1 = val1 / val2;
                    break;
                case CLEAR:
                    val1 = 0.0;
                    val2 = 0.0;
                    break;
            }
            }else{
            edit2.setText(null);
            edit1.setText(edit1.getText().toString());
        }
        }
    }


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s