Simple Android Calculator

18,112

From your code, it appears that both your operands are to be entered into the same TextView. First enter your first operand, then press any operator and then in the same TextView the second operand will go in. Moreover, each of the operands seem to be handled by individual buttons, so you just need to enter them by clicking on the buttons.

First, enter your first operand, click on an operator, then click on second operand, and then click on =. That should work fine for you.

Share:
18,112
UselessNoob
Author by

UselessNoob

Updated on June 27, 2022

Comments

  • UselessNoob
    UselessNoob almost 2 years

    I am trying to build a simple calculator. Following is the activity file:

     package com.example.calcsimple;
    
    
    import android.os.Bundle;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.view.Menu; 
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.view.View.OnClickListener;
    
    
    public class MainActivity extends Activity {
    
    Button btnClr , btn0 , btn1 , btn2 , btn3 , btn4, btn5 , btn6 , btn7 , btn8 , btn9 , btnAdd , btnSub , btnMul , btnDiv , btnEq;
    TextView textView1 ; 
    double i1 , i2 , result=0;
    boolean newValue = false;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        textView1 = (TextView) findViewById(R.id.textView1);
    
        btnClr = (Button) findViewById(R.id.btnClr);
        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);
        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnSub = (Button) findViewById(R.id.btnSub);
        btnMul = (Button) findViewById(R.id.btnMul);
        btnDiv = (Button) findViewById(R.id.btnDiv);
        btnEq = (Button) findViewById(R.id.btnEq);
    
        btn0.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
    
                textView1.append("0");
            }
        });
    
        btn1.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
    
                 if(newValue == true)
                     textView1.setText("1");
                 else
                     textView1.setText(textView1.getText().toString() +  "1");
    
                 newValue = false;
            }
        });
    
        btn2.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
    
                 if(newValue == true)
                     textView1.setText("2");
                 else
                     textView1.setText(textView1.getText().toString() +  "2");
    
                 newValue = false;
            }
        });
    
        btn3.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
    
                 if(newValue == true)
                     textView1.setText("3");
                 else
                     textView1.setText(textView1.getText().toString() +  "3");
    
                 newValue = false;
            }
        });
    
        btn4.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
    
                 if(newValue == true)
                     textView1.setText("4");
                 else
                     textView1.setText(textView1.getText().toString() +  "4");
    
                 newValue = false;
            }
        });
    
        btn5.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
    
                 if(newValue == true)
                     textView1.setText("5");
                 else
                     textView1.setText(textView1.getText().toString() +  "5");
    
                 newValue = false;
            }
        });
    
        btn6.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
    
                 if(newValue == true)
                     textView1.setText("6");
                 else
                     textView1.setText(textView1.getText().toString() +  "6");
    
                 newValue = false;
            }
        });
    
        btn7.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
    
                 if(newValue == true)
                     textView1.setText("7");
                 else
                     textView1.setText(textView1.getText().toString() +  "7");
    
                 newValue = false;
            }
        });
    
        btn8.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
    
                 if(newValue == true)
                     textView1.setText("8");
                 else
                     textView1.setText(textView1.getText().toString() +  "8");
    
                 newValue = false;
            }
        });
    
        btn9.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
    
                 if(newValue == true)
                     textView1.setText("9");
                 else
                     textView1.setText(textView1.getText().toString() +  "9");
    
                 newValue = false;
            }
        });
    
    
        btnAdd.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
    
                i1 = Double.parseDouble(textView1.getText().toString());
                textView1.setText("");
                result = result + i1;
                textView1.setText(Double.toString(result));
                newValue=true ;  
    
    
            }
        });
    
        btnSub.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
    
                i1 = Double.parseDouble(textView1.getText().toString());
                result = result - i1;
                textView1.setText(Double.toString(result));
                newValue=true ;  
                textView1.setText("");
            }
        });
    
        btnMul.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                i1 = Double.parseDouble(textView1.getText().toString());
                result = result * i1;
                textView1.setText(Double.toString(result));
                newValue=true ;  
                textView1.setText("");
            }
        });
    
        btnDiv.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
    
                i1 = Double.parseDouble(textView1.getText().toString());
                result = result / i1;
                textView1.setText(Double.toString(result));
                newValue=true ;  
                textView1.setText("");
    
            }
        });
    
        btnEq.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
    
                textView1.setText(Double.toString(result));
            }
        });
    
        btnClr.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
    
                textView1.setText("");
                newValue=true;
                result = 0 ;
                i1=0;
            }
        });
    
        }
    
    
    }
    

    Following is the XML layout file:

    <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <Button
        android:id="@+id/btn1"
        android:layout_width="44dp"
        android:layout_height="wrap_content"
        android:layout_x="6dp"
        android:layout_y="109dp"
        android:text="1" />
    
    <Button
        android:id="@+id/btn2"
        android:layout_width="48dp"
        android:layout_height="wrap_content"
        android:layout_x="88dp"
        android:layout_y="108dp"
        android:text="2" />
    
    <Button
        android:id="@+id/btn3"
        android:layout_width="44dp"
        android:layout_height="wrap_content"
        android:layout_x="174dp"
        android:layout_y="106dp"
        android:text="3" />
    
    <Button
        android:id="@+id/btn4"
        android:layout_width="45dp"
        android:layout_height="wrap_content"
        android:layout_x="6dp"
        android:layout_y="184dp"
        android:text="4" />
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_x="0dp"
        android:layout_y="4dp"
        android:text="" />
    
    <Button
        android:id="@+id/btn5"
        android:layout_width="45dp"
        android:layout_height="wrap_content"
        android:layout_x="92dp"
        android:layout_y="186dp"
        android:text="5" />
    
    <Button
        android:id="@+id/btn6"
        android:layout_width="45dp"
        android:layout_height="wrap_content"
        android:layout_x="175dp"
        android:layout_y="180dp"
        android:text="6" />
    
    <Button
        android:id="@+id/btn7"
        android:layout_width="44dp"
        android:layout_height="wrap_content"
        android:layout_x="9dp"
        android:layout_y="261dp"
        android:text="7" />
    
    <Button
        android:id="@+id/btn8"
        android:layout_width="44dp"
        android:layout_height="wrap_content"
        android:layout_x="94dp"
        android:layout_y="264dp"
        android:text="8" />
    
    <Button
        android:id="@+id/btn9"
        android:layout_width="46dp"
        android:layout_height="wrap_content"
        android:layout_x="176dp"
        android:layout_y="263dp"
        android:text="9" />
    
    <Button
        android:id="@+id/btn0"
        android:layout_width="45dp"
        android:layout_height="wrap_content"
        android:layout_x="96dp"
        android:layout_y="348dp"
        android:text="0" />
    
    <Button
        android:id="@+id/btnAdd"
        android:layout_width="45dp"
        android:layout_height="wrap_content"
        android:layout_x="248dp"
        android:layout_y="105dp"
        android:text="+" />
    
    <Button
        android:id="@+id/btnSub"
        android:layout_width="43dp"
        android:layout_height="wrap_content"
        android:layout_x="249dp"
        android:layout_y="181dp"
        android:text="-" />
    
    <Button
        android:id="@+id/btnMul"
        android:layout_width="43dp"
        android:layout_height="wrap_content"
        android:layout_x="248dp"
        android:layout_y="267dp"
        android:text="X" />
    
    <Button
        android:id="@+id/btnDiv"
        android:layout_width="44dp"
        android:layout_height="wrap_content"
        android:layout_x="248dp"
        android:layout_y="347dp"
        android:text="/" />
    
    <Button
        android:id="@+id/btnEq"
        android:layout_width="44dp"
        android:layout_height="wrap_content"
        android:layout_x="176dp"
        android:layout_y="347dp"
        android:text="=" />
    
    <Button
        android:id="@+id/btnClr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="12dp"
        android:layout_y="346dp"
        android:text="Clear" />
    
    </AbsoluteLayout>
    

    Now the problem is that I'm not finding any way to receive and store the second operand from the user.