Wednesday, August 1, 2018

Calculator

package net.samaysoftware.sampleproject12345;

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

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnplus, btnminus, btnmult, btndiv, btneq, btndot;
    TextView tvScreen;
    Button[] arr= null;


    double num1;
    int op;

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

        findAllViews();
        arr = new Button[]{btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnplus, btnminus, btnmult, btndiv, btneq, btndot};
        for(int i = 0; i<arr.length;i++){
            arr[i].setOnClickListener(this);
        }

    }

    private void findAllViews() {
        btn0 = (Button) findViewById(R.id.btn0);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);
        btn6 = (Button) findViewById(R.id.btn6);
        btn7 = (Button) findViewById(R.id.btn7);
        btn8 = (Button) findViewById(R.id.btn8);
        btn9 = (Button) findViewById(R.id.btn9);
        btndot = (Button) findViewById(R.id.btndot);
        btnplus = (Button) findViewById(R.id.btnplus);
        btnminus = (Button) findViewById(R.id.btnminus);
        btnmult = (Button) findViewById(R.id.btnmult);
        btndiv = (Button) findViewById(R.id.btndiv);
        btneq = (Button) findViewById(R.id.btneq);
        tvScreen = (TextView) findViewById(R.id.tvScreen);
    }

    @Override    public void onClick(View view) {
        Button clickedbutton = (Button) view;

        if(view== btn0 || view== btn1 || view== btn2 || view== btn3 || view== btn4 || view== btn5 || view== btn7 || view== btn6 || view== btn8 || view== btn9 || view== btndot ){
            String oldvalue = tvScreen.getText().toString();

            if(view==btndot && oldvalue.contains(".")){
                return;
            }
            String newvalue = oldvalue + clickedbutton.getText();
            tvScreen.setText(newvalue);

        }
        else if(view== btnplus || view== btnminus || view== btnmult || view== btndiv ){
            String oldvalue = tvScreen.getText().toString();
            num1 = Double.parseDouble(oldvalue);
            if(view==btnplus){
                op=1;
            }

            if(view==btnminus){
                op=2;
            }
            if(view==btnmult){
                op=3;
            }
            if(view==btndiv){
                op=4;
            }
            tvScreen.setText("");

        }
        else if(view == btneq){
            double num2 = Double.parseDouble(tvScreen.getText().toString());
            double ans = 0;
            switch (op){
                case 1:
                    ans = num1 + num2;
                    break;
                case 2:
                    ans = num1-num2;
                    break;
                case 3:
                    ans = num1 * num2;
                    break;
                case 4:
                    ans = num1 / num2;
                    break;
            }
            tvScreen.setText(ans+"");



        }
    }
}

No comments:

Post a Comment

Near by App

https://drive.google.com/file/d/0B2ag35s4X53Eb2pSQVI1SzNudE0/view