Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8410587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:13:01+00:00 2026-06-10T00:13:01+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:13:02+00:00Added an answer on June 10, 2026 at 12:13 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a simple calculator to learn javascript/jquery but seem to
I'm trying to build a simple calculator in Angular in which I can override
I'm trying to use JavaCC to build a simple command line calculator that can
I'm trying to build a simple autocomplete list: DOM: <input id=example/> <div id=results></div> Javascript:
I am trying to build a very simple loan calculator in js where the
I just started trying to build simple GUIs because I've always used command-line script
i am trying to build simple php crawler for this purpose i am getting
When trying to build a simple test program that uses atomic operations, I get
I am trying to build a simple script which must be able to navigate
I'm trying to build a simple user setting feature for my school project. I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.