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 5964073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:21:58+00:00 2026-05-22T19:21:58+00:00

I’ve made a custom dialog that is a simple calculator for addition, subtraction, multiplication

  • 0

I’ve made a custom dialog that is a simple calculator for addition, subtraction, multiplication and division. I’m having trouble getting the logic of my code correct. Does anyone have any word of wisdom or know of resource I could check out that could help.

Below is the code for my custom dialog and the calculator Logic that i’m working on. As it is all the calculations work fine except for when i use the equals button. Calculation I do after that point don’t come out right.

Thanks!

Calculator http://img546.imageshack.us/img546/9534/devicee.png

public class CustomCalcDialog extends Dialog {

    public CustomCalcDialog(Context context) {
        super(context);
    }

    EditText calcDialogDisplay;
    TextView enterTotal;
    TextView allClear;
    TextView seven;
    TextView eight;
    TextView nine;
    TextView division;
    TextView four;
    TextView five;
    TextView six;
    TextView multiply;
    TextView one;
    TextView two;
    TextView three;
    TextView subtract;
    TextView decimal;
    TextView zero;
    TextView equals;
    TextView addition;

    ArrayList<Float> mathVariables = new ArrayList<Float>();
    float mathVariable1;
    float mathVariable2;

    int currentOperation = 0;
    int nextOperation;

    final static int ADD = 1;
    final static int SUBTRACT = 2;
    final static int MULTIPLY = 3;
    final static int DIVISION = 4;
    final static int EQUALS = 5;

    final static int CLEAR = 1;
    final static int DONT_CLEAR = 0;
    int clearCalcDisplay = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculator_dialog);

        calcDialogDisplay = (EditText) findViewById(R.id.calc_dialog_display);
        enterTotal = (TextView) findViewById(R.id.enter_total);
        allClear = (TextView) findViewById(R.id.all_clear);
        seven = (TextView) findViewById(R.id.seven);
        eight = (TextView) findViewById(R.id.eight);
        nine = (TextView) findViewById(R.id.nine);
        division =(TextView) findViewById(R.id.division);
        four = (TextView) findViewById(R.id.four);
        five = (TextView) findViewById(R.id.five);
        six =(TextView) findViewById(R.id.six);
        multiply = (TextView) findViewById(R.id.multiply);
        one = (TextView) findViewById(R.id.one);
        two = (TextView) findViewById(R.id.two);
        three = (TextView) findViewById(R.id.three);
        subtract = (TextView) findViewById(R.id.subtract);
        decimal = (TextView) findViewById(R.id.decimal);
        zero = (TextView) findViewById(R.id.zero);
        equals = (TextView) findViewById(R.id.equals);
        addition = (TextView) findViewById(R.id.addition);

        calcDialogDisplay.setKeyListener(DigitsKeyListener.getInstance(true,true));

        registerListeners();
    }

    public void registerListeners () {

        enterTotal.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                activeTextView.setText(calcDialogDisplay.getText().toString());
                mathCalculations();
                CustomCalcDialog.this.dismiss();
            }
        });

        allClear.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                calcDialogDisplay.setText("");
                mathVariable1 = 0;
                mathVariable2 = 0;
                mathVariables.removeAll(mathVariables);
                currentOperation = 0;
                nextOperation = 0;                  
            }
        });

        seven.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearCalcDisplay == CLEAR) {
                    calcDialogDisplay.setText("");
                }
                clearCalcDisplay = DONT_CLEAR;
                calcDialogDisplay.append("7");

            }
        });

        eight.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearCalcDisplay == CLEAR) {
                    calcDialogDisplay.setText("");
                }
                clearCalcDisplay = DONT_CLEAR;
                calcDialogDisplay.append("8");

            }
        });

        nine.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearCalcDisplay == CLEAR) {
                    calcDialogDisplay.setText("");
                }
                clearCalcDisplay = DONT_CLEAR;
                calcDialogDisplay.append("9");

            }
        });

        division.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                calcLogic(DIVISION);                    
            }
        });

        four.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearCalcDisplay == CLEAR) {
                    calcDialogDisplay.setText("");
                }
                clearCalcDisplay = DONT_CLEAR;
                calcDialogDisplay.append("4");

            }
        });

        five.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearCalcDisplay == CLEAR) {
                    calcDialogDisplay.setText("");
                }
                clearCalcDisplay = DONT_CLEAR;
                calcDialogDisplay.append("5");

            }
        });

        six.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearCalcDisplay == CLEAR) {
                    calcDialogDisplay.setText("");
                }
                clearCalcDisplay = DONT_CLEAR;
                calcDialogDisplay.append("6");

            }
        });

        multiply.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                calcLogic(MULTIPLY);

            }
        });

        one.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearCalcDisplay == CLEAR) {
                    calcDialogDisplay.setText("");
                }
                clearCalcDisplay = DONT_CLEAR;
                calcDialogDisplay.append("1");

            }
        });

        two.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearCalcDisplay == CLEAR) {
                    calcDialogDisplay.setText("");
                }
                clearCalcDisplay = DONT_CLEAR;
                calcDialogDisplay.append("2");

            }
        });

        three.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearCalcDisplay == CLEAR) {
                    calcDialogDisplay.setText("");
                }
                clearCalcDisplay = DONT_CLEAR;
                calcDialogDisplay.append("3");

            }
        });

        subtract.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                calcLogic(SUBTRACT);
            }               
        });

        decimal.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearCalcDisplay == CLEAR) {
                    calcDialogDisplay.setText("");
                }
                clearCalcDisplay = DONT_CLEAR;
                calcDialogDisplay.append(".");

            }
        });

        zero.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (clearCalcDisplay == CLEAR) {
                    calcDialogDisplay.setText("");
                }
                clearCalcDisplay = DONT_CLEAR;
                calcDialogDisplay.append("0");

            }
        });

        equals.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                calcLogic(EQUALS);

            }
        });

        addition.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                calcLogic(ADD);
            }
        });
    }

    private void calcLogic(int operator) {

        mathVariables.add(Float.parseFloat(calcDialogDisplay.getText().toString()));

        if (operator != EQUALS) {
            nextOperation = operator;
        }else if (operator == EQUALS){
            nextOperation = 0;
        }

        switch (currentOperation) {
        case ADD:               
            mathVariable1 = mathVariables.get(0);
            mathVariable2 = mathVariables.get(1);

            mathVariables.removeAll(mathVariables);

            mathVariables.add(mathVariable1 + mathVariable2);

            calcDialogDisplay.setText(String.format("%.3f", mathVariables.get(0)));
            break;
        case SUBTRACT:
            mathVariable1 = mathVariables.get(0);
            mathVariable2 = mathVariables.get(1);

            mathVariables.removeAll(mathVariables);

            mathVariables.add(mathVariable1 - mathVariable2);

            calcDialogDisplay.setText(String.format("%.3f", mathVariables.get(0)));
            break;
        case MULTIPLY:
            mathVariable1 = mathVariables.get(0);
            mathVariable2 = mathVariables.get(1);

            mathVariables.removeAll(mathVariables);

            mathVariables.add(mathVariable1 * mathVariable2);

            calcDialogDisplay.setText(String.format("%.3f", mathVariables.get(0)));
            break;
        case DIVISION:
            mathVariable1 = mathVariables.get(0);
            mathVariable2 = mathVariables.get(1);

            mathVariables.removeAll(mathVariables);

            mathVariables.add(mathVariable1 / mathVariable2);

            calcDialogDisplay.setText(String.format("%.3f", mathVariables.get(0)));
            break;
        }

        clearCalcDisplay = CLEAR;
        currentOperation = nextOperation;
        if (operator == EQUALS) {
            mathVariable1 = 0;
            mathVariable2 = 0;
            mathVariables.removeAll(mathVariables);
        }
    }
}
  • 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-05-22T19:21:58+00:00Added an answer on May 22, 2026 at 7:21 pm

    Consider separating your app into two parts. The gui gathers the input and selects the action. Now create another class to do the calculations, say Model.java. This class should provide methods that take parameters and provide a return value. Test these isolated methods for accuracy and invalid input, out of range values and senseless input. Design the exceptions for your model to throw. Now connect the GUI to the Model.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am doing a simple coin flipping experiment for class that involves flipping a
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build

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.