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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:53:29+00:00 2026-06-09T18:53:29+00:00

I am new to Android development and currently learning to design for a basic

  • 0

I am new to Android development and currently learning to design for a basic calculator app. I have already self designed the layout, but sourced Main Activity codes from different websites for the operations for 0 to 9, +, -, *, / and after incorporation, they work fine.

However, I do want to further modify the MainActivity with decimal point function.
While integer can be shown properly to the screen using “current = current * 10 + number”, eg 53 = 5*10+3;

I am thinking applying the same approach for decimal point with a loop function, the idea like this:
1. current = current + remaining if dot button is pressed
2. create an integer i, i increases by 1 once any numerial button is clicked
3. so that when e.g. input 5.3, i =1, it will = 5 + 3/(10^i) = 5.3
4. 5.3 loop to here, then when e.g. input as 5.39, now i=2, it will = 5.3 + 9/(10^i) = 5.39

QUESTION >>
*Yet…really…I am so fresh that I do not know how to design the coding for the decimal button, would there be anyone can suggest the code?* first ignore the following addons where errors to be detected (such as delete the second dot if the dot is input twice or more, adding 0 in front of . if say, .5 is input)

The button id is as follows, and once clicked to refer to DecimalClickEvent
Button b_decimal = (Button) findViewById(R.id.decimal);
b_decimal.setOnClickListener(new DecimalClickEvent(???));}

Many many thanks in advance!! The codes are attached below for reference and your comments:

=========================MainActivity.java=====================================

package com.trial.newcalculator;

import java.io.Serializable;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

@SuppressLint("ParserError")
public class MainActivity extends Activity {
    State s;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        s = new State();

        int[] opNumbers = new int[] { 
                R.id.zero,
                R.id.one,
                R.id.two,
                R.id.three,
                R.id.four,
                R.id.five,
                R.id.six,
                R.id.seven,
                R.id.eight,
                R.id.nine,     
        };

        final TextView textView = (TextView) findViewById(R.id.ansEditText);
        for (int i = 0; i < 10;i++){
            final Button button = (Button) findViewById(opNumbers[i]);        
            button.setOnClickListener(new NumberClickEvent(textView,s,i));          
        }

      int[] opButtons = new int[] { R.id.add, R.id.subtract, R.id.multiply, R.id.divide };
      State.Operation[] states = new State.Operation[] {
                State.Operation.PLUS, 
                State.Operation.MINUS,
                State.Operation.MULTIPLY, 
                State.Operation.DIVIDE};

        for(int i = 0; i < opButtons.length;i++){
        Button b_op = (Button) findViewById(opButtons[i]);
        b_op.setOnClickListener(new OperationClickEvent(textView, s, states[i]));
        }

// Memory functions
        int[] memButtons = new int[] { R.id.MC, R.id.MR, R.id.Mdeduct, R.id.Mplus};
        State.Operation[] mstates = new State.Operation[] {
                State.Operation.MEMORYCLEAR, 
                State.Operation.MEMORYCALL,
                State.Operation.MEMORYMINUS, 
                State.Operation.MEMORYPLUS};

        for(int i = 0; i < memButtons.length;i++){
        Button b_mem = (Button) findViewById(memButtons[i]);
        b_mem.setOnClickListener(new OperationClickEvent(textView, s, states[i]));
        }
//  Memory functions    

//decimal
//      Button b_decimal = (Button) findViewById(R.id.decimal);
//      b_decimal.setOnClickListener(new DecimalClickEvent(textView, s, "."));   
//decimal

        Button b_eq = (Button) findViewById(R.id.equal);
        b_eq.setOnClickListener(new EqualClickEvent(textView, s));

        Button b_op = (Button) findViewById(R.id.ac);
        b_op.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                s.clear();
                textView.setText(s.getDisplay());
            }
        });               
    }      
@Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putSerializable("STATE", s);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        Serializable serializable = savedInstanceState.getSerializable("STATE");
        if(serializable!= null){
            s = (State) serializable;
        }
    }

    public void onPause(){
         super.onPause();
    }

}

===============================State.java==================================

package com.trial.newcalculator;

import java.io.Serializable;
import android.text.Editable;
import android.widget.TextView;

public class State implements Serializable {
    private static final long serialVersionUID = -1231231231231233L;
    public TextView output;
    public enum Operation {     
        PLUS,       
        MINUS,      
        MULTIPLY,       
        DIVIDE,     
        MEMORYPLUS,
        MEMORYMINUS,
        MEMORYCALL,
        MEMORYCLEAR,
    }   
    public enum IOState{        
        INPUTTING,      
        DISPLAY_RESULT, 
    }   
    private Double accu = null; 
    private double current = 0; 
    private double memory = 0;
    private Operation currentOp = null; 
    private IOState currentState = IOState.INPUTTING;   
    public Operation getCurrentOp() {       
        return currentOp;   
    }   
    public void setCurrentOp(Operation currentOp) {     
        if (currentState == IOState.INPUTTING){         
            if (accu != null && this.currentOp != null ){calculateResult(); 
            }
            else{accu = Double.valueOf(current);current = 0;
            }
        }
        this.currentOp = currentOp;     
        if (currentState == IOState.INPUTTING){
            currentState = IOState.DISPLAY_RESULT;
        }   
    }   
    private void calculateResult() {
        double res = accu.doubleValue();
        switch (currentOp) {
            case PLUS:
                res += current;
                break;
            case MINUS:
                res -= current;
                break;
            case MULTIPLY:
                res *= current;
                break;
            case DIVIDE:
                res /= current;
                break;
            case MEMORYPLUS:
                memory += current;
                break;
            case MEMORYMINUS:
                memory -= current;
                break;
            case MEMORYCLEAR:
                memory = 0;
                break;
            case MEMORYCALL:
                current = memory;
                break;
        }
        accu = Double.valueOf(res);
        current = 0;
    }
    public void number(int number) {
        if (currentState == IOState.INPUTTING){
            current = current *10 + number;
        }
        else if(currentState == IOState.DISPLAY_RESULT){
            currentState = IOState.INPUTTING;
            current = number;
        }
    }
    public String getDisplay() {
        String res;
        Double d = getCurrentDisplayValue();
        double doubleValue = d.doubleValue();
        int intVal = (int)doubleValue;
        if (intVal == doubleValue){
            res = Integer.toString(intVal);
            }
        else{
            res = d.toString();
            }
        return res;
    }
    private Double getCurrentDisplayValue() {
        Double d = accu;
        if (currentState == IOState.INPUTTING){
            d = Double.valueOf(current);
        }
        return d;
    }
    public void clear() {
        accu = null;
        currentState = IOState.INPUTTING;
        currentOp = null;
        current = 0;
    }

    public void equal() {
        if (accu == null || currentOp == null){
            return;
        }
        calculateResult();
        currentState = IOState.DISPLAY_RESULT;
        currentOp = null;
        current = getCurrentDisplayValue(); 
        }
}

====================OperationClickEvent.java===============================

package com.trial.newcalculator;

import com.trial.newcalculator.State.Operation;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

final class OperationClickEvent implements OnClickListener {
    private State s;
    private TextView textView;
    private Operation op;


    public OperationClickEvent(TextView textView, State s, State.Operation op) {
        super();
        this.op = op;
        this.s = s;
        this.textView = textView;
    }


    public void onClick(View v) {
        s.setCurrentOp(op);
        textView.setText(s.getDisplay());
    }
}

=================EqualClickEvent.java=======================================

package com.trial.newcalculator;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

final class EqualClickEvent implements OnClickListener {
    private State s;
    private TextView textView;


    public EqualClickEvent(TextView textView, State s) {
        super();
        this.s = s;
        this.textView = textView;
    }


    public void onClick(View v) {
        s.equal();
        textView.setText(s.getDisplay());
    }
}

======================NumberClickEvent.java==================================

package com.trial.newcalculator;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

final class NumberClickEvent implements OnClickListener {
    private int number;
    private State s;
    private TextView textView;


    public NumberClickEvent(TextView textView, State s, int number) {
        super();
        this.number = number;
        this.s = s;
        this.textView = textView;
    }


    public void onClick(View v) {
        s.number(number);
        textView.setText(s.getDisplay());
    }
}
  • 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-09T18:53:29+00:00Added an answer on June 9, 2026 at 6:53 pm

    Instead of creating new classes for negative click event and doublezeroclickevent, i suggest you to get their id’s and do the proper functionality when the particular button is clicked .

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

Sidebar

Related Questions

I am very new to Android development and Java. Have read around but I'm
I'm new to android game development. I currently working on a simple android app
I am new to android development but in the past few weeks I have
Hi I'm new to android development and working on a game. I currently have
I'm fairly new to Android, but have done lots of Java/JSP development and HTML
I am new to Android development. I have both Windows 7 and Mac OS
I am somewhat new to android development and development in general. I have a
I am new to android development. I have created a popup window which is
I'm new to Android application development and I'm currently experimenting with various UI ideas.
I'm new to Android development and I'm currently going through some tutorials. When I

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.