0

I have a simple Android app that does some math on 2 numbers that the user inputs and returns the result. Currently I have a 'calculate' button that needs to be pressed to do the math and return the value.

How can I get rid of this button and just get the app to run the math after the uses has changed either one of the 2 numbers?

Many thanks.

Entropy1024
  • 7,847
  • 9
  • 30
  • 32

4 Answers4

2

Assuming that you are using EditText-s for the input (because they could be SeekBar-s, who knows), add on each one of them a TextWatcher with this and after each change of one of them refresh the result

apps
  • 942
  • 5
  • 8
0

The details depend on what language you are using, but basically you could just run the calculation function whenever either one of the numbers is changed. There should be a way to override the function that is called when the data in either text box has changed.

Computerish
  • 9,590
  • 7
  • 38
  • 49
0

Well, if both numbers start off with 0, you can either check to make sure neither or 0 or trigger the math when both text fields have received text changed events. I'm not sure what platform you are using so I can't give you much more info.

Dominic K
  • 6,975
  • 11
  • 53
  • 62
  • Sorry it's Android. And the 2 values do start at 0 but that all changes one the user changes them, again and again ;) – Entropy1024 Nov 13 '10 at 22:15
  • So let me get this straight. If I change both the numbers, the app should display the total. If I change one number later, I display the new total. If so, just check for text changed events. – Dominic K Nov 13 '10 at 23:55
0

I used something like this for an EditText field that I wanted to be auto-updated after changing some other EditText fields:

    myEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE)
            {
                calculateNewValue();   // Updates internal variables

                // This part will hide the keyboard after input
                InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                return true;
            }
            return false;
        }
    });

This also hides the keyboard when you're done (thanks to a bit of help).

The calculateNewValue() updated the field like this:

private void calculateNewValue()
{
    val = YourFormula()

    myEditText.setText(String.format(yourFormat, val))
}
Community
  • 1
  • 1
marshall.ward
  • 6,758
  • 8
  • 35
  • 50
  • Thanks for the help. I can sort off see how it works. However I get the following error: The method setOnEditorActionListener(TextView.OnEditorActionListener) in the type TextView is not applicable for the arguments (new OnEditorActionListener(){}) what does this mean? Cheers. – Entropy1024 Nov 17 '10 at 15:55
  • I am a bit new to Android myself, so I'm not sure. Did you include the TextView in the input argument TextView.OnEditorActionListener(){})? – marshall.ward Nov 18 '10 at 03:52
  • I basicaly copied the code above and changed the myEditText to match my EditText. At no point in my program do I use a TextView so I am not sure if all the TextView parts should be EditViews? Confused. – Entropy1024 Nov 18 '10 at 15:18
  • I tried using the code below but it does not seem to have any effect myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() { public boolean onEditorAction(EditText v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_NEXT) – Entropy1024 Nov 18 '10 at 15:28