Answered Essay: How to program a simple calculator using Android Studio? My code so far in XML:

How to program a simple calculator using Android Studio? My code so far in XML:

xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.gangstatop.calculator.MainActivity"
    tools:layout_editor_absoluteY="90dp"
    tools:layout_editor_absoluteX="0dp">


    <TextView
        android:id="@+id/outPut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:text="TextView"
        android:textAlignment="viewEnd"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        android:layout_marginEnd="39dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="39dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginRight="39dp" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="255dp"
        android:orientation="vertical"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintRight_creator="1"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp">

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

            <Button
                android:id="@+id/clear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="AC" />

            <Button
                android:id="@+id/plusorminus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="+/-" />

            <Button
                android:id="@+id/backspace"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="<-" />

            <Button
                android:id="@+id/divide"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="÷" />
        LinearLayout>

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

            <Button
                android:id="@+id/seven"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="7" />

            <Button
                android:id="@+id/eight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="8" />

            <Button
                android:id="@+id/nine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="9" />

            <Button
                android:id="@+id/multi"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="*" />
        LinearLayout>

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

            <Button
                android:id="@+id/four"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="4" />

            <Button
                android:id="@+id/five"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="5" />

            <Button
                android:id="@+id/six"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="6" />

            <Button
                android:id="@+id/minus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="-" />
        LinearLayout>

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

            <Button
                android:id="@+id/one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="1" />

            <Button
                android:id="@+id/two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="2" />

            <Button
                android:id="@+id/three"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="3" />

            <Button
                android:id="@+id/plus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="+" />

        LinearLayout>

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

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

            <Button
                android:id="@+id/dot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="." />

            <Button
                android:id="@+id/equal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="=" />

        LinearLayout>

    LinearLayout>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:text="TextView"
        android:textAlignment="viewEnd"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        android:layout_marginEnd="39dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="39dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginRight="39dp" />
android.support.constraint.ConstraintLayout>   Then my main activity code: 


package com.example.gangstatop.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    private Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
    private Button buttonAdd, buttonSub, buttonMulitiply, buttonDivide, buttonBack, buttonClear, buttonSwitch, buttonPeriod, buttonEqual;
    private TextView results, finalResults;
    private double firstValue, secondValue;
    private char OPERATION;



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


        initialize();

        results.setText("0");
        finalResults.setText("");
        pressButton();





    }

    private void initialize() {
        //Number buttons
        button0 = (Button)findViewById(R.id.zero);
        button1 = (Button)findViewById(R.id.one);
        button2 = (Button)findViewById(R.id.two);
        button3 = (Button)findViewById(R.id.three);
        button4 = (Button)findViewById(R.id.four);
        button5 = (Button)findViewById(R.id.five);
        button6 = (Button)findViewById(R.id.six);
        button7 = (Button)findViewById(R.id.seven);
        button8 = (Button)findViewById(R.id.eight);
        button9 = (Button)findViewById(R.id.nine);

        //Function buttons
        buttonAdd = (Button)findViewById(R.id.plus);
        buttonSub = (Button)findViewById(R.id.minus);
        buttonMulitiply = (Button)findViewById(R.id.multi);
        buttonDivide = (Button)findViewById(R.id.divide);

        //Misc function buttons
        buttonSwitch = (Button)findViewById(R.id.plusorminus);
        buttonClear = (Button)findViewById(R.id.clear);
        buttonPeriod = (Button)findViewById(R.id.dot);
        buttonEqual = (Button)findViewById(R.id.equal);

        //Textview displaying results
        results = (TextView)findViewById(R.id.outPut);
        finalResults = (TextView)findViewById(R.id.textView);
    }

    public void pressButton () {
        /************************************************************
         * Override the onClick function to display all numbers in
         * calculator.
         */

        //results.setText("");

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

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                results.setText(results.getText().toString() + "1");
            }}
        );

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                results.setText(results.getText().toString() + "2");
            }}
        );
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                results.setText(results.getText().toString() + "3");
            }}
        );

        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                results.setText(results.getText().toString() + "4");
            }}
        );
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                results.setText(results.getText().toString() + "5");
            }}
        );

        button6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                results.setText(results.getText().toString() + "6");
            }}
        );
        button7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                results.setText(results.getText().toString() + "7");
            }}
        );

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

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

        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                operation();
                OPERATION = '+';
                finalResults.setText(String.valueOf(firstValue));
                results.setText(null);
            }}
        );


      /* buttonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                if (results.length() > 0)  {
                    CharSequence digit = results.getText().toString();
                    results.setText(digit.subSequence(0, digit.length()-1));
                }
                else {
                    results.setText("0");
                    finalResults.setText("null");

                }
            }}
        ); */



    }

    public void operation () {
        firstValue = Double.parseDouble(results.getText().toString());
        secondValue = Double.parseDouble(results.getText().toString());

        switch (OPERATION) {
            case '+':
                firstValue = firstValue - secondValue;
                break;
            case '-':
                firstValue = firstValue - secondValue;
                break;
            case '*':
                firstValue = firstValue * secondValue;
            break;
            case '/':
                firstValue = firstValue / secondValue;
            case '=':
            break;


        }

    }




}  

manifest file: 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gangstatop.calculator">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>  
Test cases: 

1. 5+2=7
2. 6-3=3
3. 4*8=32
4. 15/3=5
5. -1.5+6=4.5
6. 19 – 27.2 = -8.2
7. 55555 * 99999 = 5.555444e9 8. 1 / 10000000 = 1e-7

9. 56+34+14+5.5=109.5
10. 44 + (Clear) 13 + 45(backspace) = 17 (Should compute 13 + 4)

Expert Answer

 

activity_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.varun.basiccalculator.MainActivity"
    tools:layout_editor_absoluteY="90dp"
    tools:layout_editor_absoluteX="0dp">
    <TextView
        android:id="@+id/outPut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:text="TextView"
        android:textAlignment="viewEnd"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        android:layout_marginEnd="39dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="39dp"
        tools:layout_editor_absoluteY="39dp" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="255dp"
        android:orientation="vertical"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_constraintRight_creator="1"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp">

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

            <Button
                android:id="@+id/clear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onClickClear"
                android:text="AC" />

            <Button
                android:id="@+id/plusorminus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="OnClickOperator"
                android:text="+/-" />

            <Button
                android:id="@+id/backspace"
                android:onClick="onClickClear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="C" />

            <Button
                android:id="@+id/divide"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="OnClickOperator"
                android:text="/" />
        </LinearLayout>

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

            <Button
                android:id="@+id/seven"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onClickNumber"
                android:layout_weight="1"
                android:text="7" />

            <Button
                android:id="@+id/eight"
                android:layout_width="wrap_content"
                android:onClick="onClickNumber"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="8" />

            <Button
                android:id="@+id/nine"
                android:layout_width="wrap_content"
                android:onClick="onClickNumber"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="9" />

            <Button
                android:id="@+id/multi"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="OnClickOperator"
                android:text="*" />
        </LinearLayout>

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

            <Button
                android:id="@+id/four"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onClickNumber"
                android:layout_weight="1"
                android:text="4" />

            <Button
                android:id="@+id/five"
                android:onClick="onClickNumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="5" />

            <Button
                android:id="@+id/six"
                android:onClick="onClickNumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="6" />

            <Button
                android:id="@+id/minus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="OnClickOperator"
                android:text="-" />
        </LinearLayout>

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

            <Button
                android:id="@+id/one"
                android:onClick="onClickNumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="1" />

            <Button
                android:id="@+id/two"
                android:onClick="onClickNumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="2" />

            <Button
                android:id="@+id/three"
                android:onClick="onClickNumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="3" />

            <Button
                android:id="@+id/plus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="OnClickOperator"
                android:text="+" />

        </LinearLayout>

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

            <Button
                android:id="@+id/zero"
                android:layout_width="wrap_content"
                android:onClick="onClickNumber"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="0" />

            <Button
                android:id="@+id/dot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="OnClickOperator"
                android:text="." />

            <Button
                android:id="@+id/equal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onClickEqual"
                android:text="=" />

        </LinearLayout>
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="end"
            android:text="TextView"
            android:textAlignment="viewEnd"
            tools:layout_constraintTop_creator="1"
            tools:layout_constraintRight_creator="1"
            android:layout_marginEnd="39dp"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginTop="39dp"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginRight="39dp" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

java file MainActivity

package com.example.varun.basiccalculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {
    private Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9;
    private Button buttonAdd, buttonSub, buttonMulitiply, buttonDivide, buttonBack, buttonClear, buttonSwitch, buttonPeriod, buttonEqual,buttondot;
    private TextView results, finalResults;
    private double firstValue, secondValue;
    private char OPERATION;
    private TextView screen;
    private String display= "";
    private String currentOperator = "";
    private String result= "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        screen = (TextView)findViewById(R.id.outPut);
        screen.setText(display);
        buttonBack = (Button)findViewById(R.id.clear);
    }
    private void updateScreen(){
        screen.setText(display);
    }
    public void onClickNumber(View v){
        if(result != ""){
            clear();
            updateScreen();
        }
        Button b = (Button) v;
        display += b.getText();
        updateScreen();
    }
    private boolean isOperator(char op){
        switch (op){
            case '+':
            case '-':
            case 'x':
            case '÷':return true;
            default: return false;
        }
    }
    public void OnClickOperator(View v){
        if(display == "") return;

        Button b = (Button)v;

        if(result != ""){
            String _display = result;
            clear();
            display = _display;
        }

        if(currentOperator != ""){
           if(isOperator(display.charAt(display.length()-1))){
                display = display.replace(display.charAt(display.length()-1), b.getText().charAt(0));
                updateScreen();
                return;
            }else{
                getResult();
                display = result;
                result = "";
            }
            currentOperator = b.getText().toString();
        }
        display += b.getText();
        currentOperator = b.getText().toString();
        updateScreen();
    }

    private void clear(){
        display = "";
        currentOperator = "";
        result = "";
    }

    public void onClickClear(View v) {
        clear();
        updateScreen();
    }
    public void OnClickBack(){
        display.substring(0,display.length()-1);
        updateScreen();
    }
    private double operate(String a, String b, String op){
        switch (op){
            case "+": return Double.valueOf(a) + Double.valueOf(b);
            case "-": return Double.valueOf(a) - Double.valueOf(b);
            case "*": return Double.valueOf(a) * Double.valueOf(b);
            case "÷": try{
                return Double.valueOf(a) / Double.valueOf(b);
            }catch (Exception e){
               // Log.d("Calc", e.getMessage());
            }
            case "+/-": return Double.valueOf(a) * -1;
            default: return -1;
        }
    }

    private boolean getResult(){
        if(currentOperator == "") return false;
        String[] operation = display.split(Pattern.quote(currentOperator));
         if(operation.length < 2) return false;
        result = String.valueOf(operate(operation[0], operation[1], currentOperator));
        return true;
    }

    public void onClickEqual(View v){
        if(display == "") return;
        if(!getResult()) return;
        screen.setText(display + "n" + String.valueOf(result));
    }
}

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.