App development beginners tried to make an Android calculator app

Introduction

I made an Android calculator application in Java with reference to the following site. I used Kotlin on the following sites, but since this is my first Android application creation, this article is a program written in Java. Create a calculator app in Android Studio

Ingenuity

Use Bigdecimal type instead of double type to prevent calculation error

At first, the calculation process was performed using the double type, but as shown in the following site, a calculation error occurred, so I used the Bigdecimal type to prevent it. About the cause of the error of the numerical double type ~ Talk about the troublesome decimal number of binary numbers ~

Reference site

program

-Create a calculator app in Android Studio -[Numerical value ⇔ character string conversion Java edition](http://www.techscore.com/blog/2012/11/28/ Numerical value-⇔-Character string conversion-java edition /) -[Android] [Java] Delete the last character of the string -[java] How do I get the last character in a string? -[Java] Can char type be compared with == -I want to make Java double a straightforward String, I don't need it -How much ternary operator is allowed in Java -About the process of removing unnecessary zeros from Java double values -Java BigDecimal Addition, Subtraction, Multiplication, Division and Rounding Method -Java.math.BigDecimal Summary for Java Programmers

design

-How to use layout_weight (compose screen by ratio) -Material Design Color Color Combination Samples

Source code

styles.xml


<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

Changed the parent attribute of style to NoActionBar. The calculator app doesn't need an action bar.

color.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- DefaultColorChoice -->
    <!--<color name="colorPrimary">#008577</color>-->
    <!--<color name="colorPrimaryDark">#00574B</color>-->
    <!--<color name="colorAccent">#D81B60</color>-->
    <color name="colorPrimary">#2196F3</color>
    <color name="colorPrimaryDark">#1976D2</color>
    <color name="colorAccent">#EF5350</color>
</resources>

Change the design to your favorite color. The following sites can be used as a reference for color combinations. Material Design Color Color Combination Samples

strings.xml


<resources>
    <string name="app_name">Calculator</string>
    <string name="formula_text" />
</resources>

activity_calc.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=".CalcActivity">

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.4" />

    <TableLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline">

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

            <TextView
                android:id="@+id/tvFormula"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_span="4"
                android:layout_weight="1"
                android:paddingEnd="8dp"
                android:gravity="end"
                android:text="@string/formula_text"
                android:textSize="30sp" />
        </TableRow>

        <TableRow
            style="?android:attr/buttonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/btClear"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#e9e9e9"
                android:text="C"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btDivide"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#e9e9e9"
                android:text="÷"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btMultiply"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#e9e9e9"
                android:text="×"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btDelete"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#e9e9e9"
                android:text="DEL"
                android:textColor="@color/colorPrimaryDark" />
        </TableRow>

        <TableRow
            style="?android:attr/buttonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/btSeven"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="7"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btEight"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="8"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btNine"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="9"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btPercent"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#e9e9e9"
                android:text="%"
                android:textColor="@color/colorPrimaryDark" />
        </TableRow>

        <TableRow
            style="?android:attr/buttonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/btFour"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="4"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btFive"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="5"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btSix"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="6"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btSubtract"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#e9e9e9"
                android:text="-"
                android:textColor="@color/colorPrimaryDark" />
        </TableRow>

        <TableRow
            style="?android:attr/buttonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/btOne"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="1"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btTwo"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="2"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btThree"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="3"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btAdd"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#e9e9e9"
                android:text="+"
                android:textColor="@color/colorPrimaryDark" />
        </TableRow>

        <TableRow
            style="?android:attr/buttonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:id="@+id/btPlusMinus"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1" />
            <Button
                android:id="@+id/btZero"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="0"
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btPoint"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="."
                android:textColor="@color/colorPrimaryDark" />

            <Button
                android:id="@+id/btEqual"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="="
                android:textColor="#ffffff"
                android:background="@color/colorPrimaryDark"/>
        </TableRow>
    </TableLayout>

</android.support.constraint.ConstraintLayout>

The width and height are properly devised so as not to be hard coded.

CalcActivity.java


package com.yagiyagi21.android.calculator;

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

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static android.webkit.ConsoleMessage.MessageLevel.LOG;

public class CalcActivity extends AppCompatActivity {

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

        List<Button> buttonList = new ArrayList<>();
        //Get the number button
        buttonList.add((Button) findViewById(R.id.btZero));
        buttonList.add((Button) findViewById(R.id.btOne));
        buttonList.add((Button) findViewById(R.id.btTwo));
        buttonList.add((Button) findViewById(R.id.btThree));
        buttonList.add((Button) findViewById(R.id.btFour));
        buttonList.add((Button) findViewById(R.id.btFive));
        buttonList.add((Button) findViewById(R.id.btSix));
        buttonList.add((Button) findViewById(R.id.btSeven));
        buttonList.add((Button) findViewById(R.id.btEight));
        buttonList.add((Button) findViewById(R.id.btNine));
        //Get the sign button
        buttonList.add((Button) findViewById(R.id.btAdd));
        buttonList.add((Button) findViewById(R.id.btSubtract));
        buttonList.add((Button) findViewById(R.id.btMultiply));
        buttonList.add((Button) findViewById(R.id.btDivide));
        buttonList.add((Button) findViewById(R.id.btEqual));
        //Get other buttons
        buttonList.add((Button) findViewById(R.id.btClear));
        buttonList.add((Button) findViewById(R.id.btDelete));
        buttonList.add((Button) findViewById(R.id.btPoint));
        buttonList.add((Button) findViewById(R.id.btPlusMinus));
        buttonList.add((Button) findViewById(R.id.btPercent));

        ButtonListener listener = new ButtonListener();

        for(Button button : buttonList){
            button.setOnClickListener(listener);
        }
    }

    private class ButtonListener implements View.OnClickListener {

        private List<BigDecimal> _numList = new ArrayList<>();
        private List<Character> _opeList = new ArrayList<>();
        private String _inputValue = "";

        @Override
        public void onClick(View view) {
            TextView tvFormula = findViewById(R.id.tvFormula);

            //Define processing for each button
            int btId = view.getId();
            char inputChar;
            switch (btId) {
                //For number buttons
                case R.id.btZero:
                    inputChar = '0';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btOne:
                    inputChar = '1';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btTwo:
                    inputChar = '2';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btThree:
                    inputChar = '3';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btFour:
                    inputChar = '4';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btFive:
                    inputChar = '5';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btSix:
                    inputChar = '6';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btSeven:
                    inputChar = '7';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btEight:
                    inputChar = '8';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btNine:
                    inputChar = '9';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                //For symbol buttons
                case R.id.btAdd:
                    inputChar = '+';
                    if(!(_inputValue.equals(""))) {
                        addList(tvFormula, _inputValue, inputChar);
                    }
                    break;
                case R.id.btSubtract:
                    inputChar = '-';
                    if(!(_inputValue.equals(""))) {
                        addList(tvFormula, _inputValue, inputChar);
                    }
                    break;
                case R.id.btMultiply:
                    inputChar = '×';
                    if(!(_inputValue.equals(""))) {
                        addList(tvFormula, _inputValue, inputChar);
                    }
                    break;
                case R.id.btDivide:
                    inputChar = '÷';
                    if(!(_inputValue.equals(""))) {
                        addList(tvFormula, _inputValue, inputChar);
                    }
                    break;
                case R.id.btEqual:
                    inputChar = '=';
                    if(!(_inputValue.equals(""))) {
                        addList(tvFormula, _inputValue, inputChar);
                    }
                    String result = calculate().toString();
                    tvFormula.setText(result);
                    _inputValue = result;

                    _numList.clear();
                    _opeList.clear();
                    break;
                //Processing for other buttons
                case R.id.btClear:
                    tvFormula.setText("");
                    _numList.clear();
                    _opeList.clear();
                    _inputValue= "";
                    break;
                case R.id.btDelete:
                    String formulaStr = tvFormula.getText().toString();
                    char formulaStrLastChar = formulaStr.charAt(formulaStr.length() - 1);

                    if (isFourArithmeticOpe(formulaStrLastChar)) {
                        _opeList.remove(_opeList.size() - 1);
                    }
                    if (!formulaStr.isEmpty()) {
                        tvFormula.setText(formulaStr.subSequence(0, formulaStr.length() - 1));
                    }
                    if (!_inputValue.isEmpty()) {
                        _inputValue = _inputValue.substring(0, _inputValue.length() - 1);
                    }
                    break;
                case R.id.btPoint:
                    inputChar = '.';
                    addTextView(tvFormula, inputChar);
                    _inputValue += inputChar;
                    break;
                case R.id.btPlusMinus:
                    break;
                case R.id.btPercent:
                    break;
            }
        }

        private void addList(TextView tvFormula, String inputValue, char ope) {
            addTextView(tvFormula, ope);
            _numList.add(new BigDecimal(inputValue));
            _opeList.add(ope);
            _inputValue = "";
        }

        private void addTextView(TextView textView, char addStr) {
            textView.setText(textView.getText().toString() + addStr);
        }

        private BigDecimal calculate() {
            int i = 0;

            while(i < _opeList.size()) {
                if(_opeList.get(i) == '×' | _opeList.get(i) == '÷') {
                    BigDecimal resultMultiDiv = _opeList.get(i) == '×' ? _numList.get(i).multiply(_numList.get(i+1)) : _numList.get(i).divide(_numList.get(i+1));

                    _numList.set(i, resultMultiDiv);
                    _numList.remove(i+1);
                    _opeList.remove(i);
                    i--;
                }
                else if(_opeList.get(i) == '-') {
                    _opeList.set(i, '+');
                    _numList.set(i+1, _numList.get(i+1).negate());
                }
                i++;
            }

            BigDecimal result = new BigDecimal("0");
            for(BigDecimal j : _numList) {
                result = result.add(j);
            }

            return result;
        }

        private boolean isFourArithmeticOpe(char c) {
            if(c == '+' | c == '-' | c == '*' | c == '/') return true;
            return false;
        }
    }
}

Recommended Posts

App development beginners tried to make an Android calculator app
Make an android app. (Day 5)
Make an android app. (First day)
How to make an app using Tensorflow with Android Studio
I want to make an ios.android app
I tried to make an Android application with MVC now (Java)
How to make an crazy Android music player
[Introduction to Android application development] Let's make a counter
Make an instagram clone app ④
Android app personal development kickoff
Introduction to Android application development
Make an instagram clone app ②
Is it an Android app?
Make an instagram clone app ③
Make an instagram clone app ①
I tried to make an application in 3 months from inexperienced
I tried to make an introduction to PHP + MySQL with Docker
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
I tried to make Venn diagram an easy-to-understand GIF animation
I tried to make an app that allows you to post and chat by genre ~ App overview ~
What Android development beginners did before releasing the app in 2 weeks
I tried to create a simple map app in Android Studio
I tried to build an http2 development environment with Eclipse + Tomcat
Notes for Android application development beginners
I tried to make it an arbitrary URL using routing nesting
Introduction to Android App Development 1 Installing JDK and Android Studio for mac
[Android] How to make Dialog Fragment
I tried to make a simple face recognition Android application using OpenCV
Trial and error to display national holidays in Android app development. Part 2
I tried to summarize the stumbling points when developing an Android application
How to make an app with a plugin mechanism [C # and Java]
Make an executable jar using Android Studio
I tried to introduce CircleCI 2.0 to Rails app
A memorandum for android application development beginners
Introduced Vue.js to an existing Rails app
[Java] Beginners want to make dating. 1st
I made a calculator app on Android
Introduced Vuetify to an existing Rails app
Swift beginners tried to implement microwave logic!
[Android] I tried to make a material list screen with ListView + Bottom Sheet
I'm making an Android app and I'm stuck with errors and how to solve it
[3rd] Mahjong app development from 0 with two beginners
Riot (chat app) development (settings) in Android Studio
I tried to make Basic authentication with Java
Android app: Try to summarize events and listeners
[1st] Mahjong app development from 0 with two beginners
I made an Android app for MiRm service
Make software that mirrors Android screen to PC 1
I tried to create a LINE clone app
Android Studio development for the first time (for beginners)
How to make Unity Native Plugin (Android version)
[2nd] Mahjong app development from 0 with two beginners
Rails6.0 ~ How to create an eco-friendly development environment
Swift beginners tried to implement vending machine logic!
Rails beginners tried to get started with RSpec
I tried to develop an application in 2 languages
From building an AWS cloud environment to deploying a Spring Boot app (for beginners)
I tried to summarize Android development, RGB, hexadecimal number, color implementation from resource files