Andriod programming – Posts 6: Construction Practice Calculator app

Hello everyone, in 2 before we have gathered all the interface design is relatively more then. To reduce stress and unwind a little, then this article will guide you to make a complete application Calculator.

[qads]

We will use the code interface designed eve.

calculator-layout-ok-2

ah forget, you are prompted to do with this app can calculate quickly and do not take much effort, we will use library Balan they have built. You balan.jar file download here. Next open the Project mode and copy the file to the folder balan.jar app / libs like shape

android-add-libs

Then Right-click the selected file balan.jar Add as Library… Near the bottom of the menu, the system asks if you want to add on modules as every app is worried ok. Wait a minute synchronous system is finished.

Now you open the file in the folder ActivityMain.java app/src/main/java/{package name} out. Currently jaw setContentViewOur drrdf remains R.layout.activity_main, you change it to R.layout.layout_calculator to connect to the file of the Calculator interface you've designed the day before.

package com.nguyenvanquan7826.tut5linearlayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

then ok, now really started offline.
First, to be able to work with computers, we need to connect the elements in the interface with Java code through the corresponding variable.

package com.nguyenvanquan7826.tut5linearlayout;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvMath;
    private TextView tvResult;

    private int[] idButton = {
            R.id.btn0,
            R.id.btn1, R.id.btn2, R.id.btn3,
            R.id.btn4, R.id.btn5, R.id.btn6,
            R.id.btn7, R.id.btn8, R.id.btn9,
            R.id.btnDot, R.id.btnResult,
            R.id.btnPlus, R.id.btnSub, R.id.btnMul, R.id.btnDiv,
            R.id.btnC, R.id.btnOpen, R.id.btnClose
    };

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

        connectView();
    }

    private void connectView() {
        tvMath = (TextView) findViewById(R.id.tvMath);
        tvResult = (TextView) findViewById(R.id.tvResult);

        for (int i = 0; i < idButton.length; i++) {
            findViewById(idButton[i]).setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View view) {
        
    }
}

Have you noticed in the code on the connections and Result TextView is normal Math, Button connects individual is not normal. The reason that I realized there are so many buttons to connect and start the event they need both. So the idea is to be able to enter data loop to repeat the same work that? So they have for their id into an array idButton. Per View substantive id is converted to integer types in the system, so this array of type int. Also the connection and start the event for each button in the loop also differ. We do not cast the connection button that has just captured events immediately via the chain of command findViewById(idButton[in]).setOnClickListener(this);
The reason is that the function findViewById brings us a View (and since then usually we pressed on its true style as Button, TextView,…) Per View feature that can catch the Click event (including TextView, EditText,…) Should we call for immediate setOnClickListener then without a cast on Button.

Now the next task is handling the button click event. Here we split up to do 3 kind of button: Button number, Button Button Handle math and math.
Button numbers and button operation, when clicking any buttons, we just add the text corresponding to the expressions. Button longer handle (button C và button = ) then perform the calculation or clear the screen. When you click Button by, we call valueMath of objects in the library Balan Balan that we have added to the original, it will count for us results.

package com.nguyenvanquan7826.tut5linearlayout;

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

import cachhoc.net.Balan;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvMath;
    private TextView tvResult;

    private int[] idButton = {
            R.id.btn0,
            R.id.btn1, R.id.btn2, R.id.btn3,
            R.id.btn4, R.id.btn5, R.id.btn6,
            R.id.btn7, R.id.btn8, R.id.btn9,
            R.id.btnDot,
            R.id.btnPlus, R.id.btnSub, R.id.btnMul, R.id.btnDiv,
            R.id.btnOpen, R.id.btnClose,
            R.id.btnC, R.id.btnResult
    };

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

        connectView();
    }

    private void connectView() {
        tvMath = (TextView) findViewById(R.id.tvMath);
        tvResult = (TextView) findViewById(R.id.tvResult);

        for (int i = 0; i < idButton.length; i++) {
            findViewById(idButton[i]).setOnClickListener(this);
        }
        init();
    }

    private void init() {
        tvMath.setText("|");
        tvResult.setText("0");
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();

        // check button number and button operator
        for (int i = 0; i < idButton.length - 2; i++) {
            if (id == idButton[i]) {
                String text = ((Button) findViewById(id)).getText().toString();

                // clear char | on top
                if (tvMath.getText().toString().trim().equals("|")) {
                    tvMath.setText("");
                }

                tvMath.append(text);
                return;
            }
        }

        // clear screen
        if (id == R.id.btnC) {
            init();
            return;
        }

        // calculation
        if (id == R.id.btnResult) {
            cal();
        }
    }

    private void cal() {
        String math = tvMath.getText().toString().trim();
        if (math.length() > 0) {
            Balan balan = new Balan();
            String result = balan.valueMath(math) + "";
            String error = balan.getError();

            // check error
            if (error != null) {
                tvResult.setText(error);
            } else { // show result
                tvResult.setText(result);
            }
        }
    }
}

That's it, Our results here offline.
calculator-result