-3

So I have 6 edit texts and a button as shown below: imgur link

My question is how do I use the input from the EditTexts (which I have stored in content_main.xml) to do mathematical operations like calculating an average which I want to show up in a toast when the calculate button is pressed. I have already written some code in the MainActivity.java file that brings up a toast when the calculate button is pressed (also in content_main.xml), I just need to figure out how to use the inputs from the EditTexts in the toast.

haraman
  • 2,744
  • 2
  • 27
  • 50
  • Do you mean how get the input of the EditText? – Héctor Nov 30 '15 at 14:34
  • 1
    Please share your code. Have a look a simple EditText tutorial, e.g. this one: http://examples.javacodegeeks.com/android/core/widget/edittext/android-edittext-example/ – Christopher Nov 30 '15 at 14:35
  • Possible duplicate of [Get Value of a Edit Text field](http://stackoverflow.com/questions/4531396/get-value-of-a-edit-text-field) – Zahan Safallwa Nov 30 '15 at 14:46

2 Answers2

0
        EditText myText // = findViewById...
        String text = myText.getText().toString();
Luca Ziegler
  • 3,236
  • 1
  • 22
  • 39
DmitryBorodin
  • 4,584
  • 4
  • 17
  • 29
0

What you should do first is to give each of its elements ID to also recognize from the Activity.

Then you should use the click event of the button

     //Here it is referring to the id that gave his element in its layout
     Button button = (Button) findViewById(R.id.button_id);
     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {

         }
     });

And finally, like the button, get their input values EditText

//Here it is referring to the id that gave his element in its layout
EditText text = (EditText)findViewById(R.id.editText01);

And in order to do math, parse the string value remaining on a double (double for decimals can give the exact calculation if you want something, if you want to be an int approximately)

try{
    Double value = Double.parseDouble(text);
}catch(NumberFormatException e){
    //Message for error parse
}
NHTorres
  • 1,528
  • 2
  • 21
  • 39