title: How to calculate an average in android?
body: <p>I've created in my application a few EditText fields and a button. If you put values in the first and in the 2nd EditText fields, when you press the button, the 3rd EditText field shows the sum of the 2 values you entered. That's working fine, but I want to calculate an average. I mean, if I have 4 EditText fields, I want to enter values in 3 (or less) of them and the 4th to show me the average calculating from the sum divided with the number of the values entered. Example: first value: 2, 2nd value: 4 and the average = 2+4=6/2=3. I want to create the case when a EditText field or more is not enter and the application still can calculate the average. Hope you understand what I want. Thanks !</p>
  
  <p>Here is my:</p>
  
  <p>Medii.java:</p>
  
  <pre><code>    package com.cngcnasaud.orar;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.*;
  
  public class Medii extends Activity {
  
      EditText txtNum1;
      EditText txtNum2;
      EditText txtTotal;
      Button btnCompute;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.medii);
  
          txtNum1 = (EditText) findViewById(R.id.editText1);
          txtNum2 = (EditText) findViewById(R.id.editText2);
          txtTotal = (EditText) findViewById(R.id.editText3);
          btnCompute = (Button) findViewById(R.id.bmedii);
  
          btnCompute.setOnClickListener(new ClickButton ());
  
      }
  
  
      private class ClickButton implements Button.OnClickListener{
  
          @Override
          public void onClick(View v) {
  
              int x = Integer.parseInt(txtNum1.getText().toString());
              int y = Integer.parseInt(txtNum2.getText().toString());
  
              int total = (x + y)/2;
  
              txtTotal.setText(Integer.toString(total));
  
          }
  
  
      }
  
  
  
  
  
  }
  </code></pre>