Android Open Source - Dilution Main Activity






From Project

Back to project page Dilution.

License

The source code is released under:

MIT License

If you think the Android project Dilution listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package sk.fotopriestor.dilution;
/* w w w.ja va 2  s .  c o m*/
import sk.fotopriestor.dilution.core.Core;
import sk.fotopriestor.dilution.core.Value;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends Activity {

  private EditText c1;
  private EditText v1;
  private EditText c2;
  private EditText v2;
  private TextView labelC2;
  private TextView labelV2;

  /**
   * recalculate values when changed
   */
  private TextWatcher recalcWatcher = new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before,
        int count) {
      // do nothing
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
      // do nothing
    }

    @Override
    public void afterTextChanged(Editable s) {
      calculate();
    }
  };

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    c1 = (EditText) findViewById(R.id.editC1);
    v1 = (EditText) findViewById(R.id.editV1);
    c2 = (EditText) findViewById(R.id.editC2);
    v2 = (EditText) findViewById(R.id.editV2);
    labelC2 = (TextView) findViewById(R.id.labelC2);
    labelV2 = (TextView) findViewById(R.id.labelV2);

    c1.addTextChangedListener(recalcWatcher);
    v1.addTextChangedListener(recalcWatcher);
    if (calcTypeIsVolume()) {
      c2.addTextChangedListener(recalcWatcher);
    } else {
      v2.addTextChangedListener(recalcWatcher);
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  public void swapCalculationType(View view) {
    if (calcTypeIsVolume()) {
      c2.addTextChangedListener(recalcWatcher);
      c2.setEnabled(true);
      v2.removeTextChangedListener(recalcWatcher);
      v2.setEnabled(false);
    } else {
      c2.removeTextChangedListener(recalcWatcher);
      c2.setEnabled(false);
      v2.addTextChangedListener(recalcWatcher);
      v2.setEnabled(true);
    }
  }

  /**
   * Swap values, but do not re-evaluate the equation
   * 
   * @param view
   */
  public void swapValues(View view) {
    c1.removeTextChangedListener(recalcWatcher);
    v1.removeTextChangedListener(recalcWatcher);
    c2.removeTextChangedListener(recalcWatcher);
    v2.removeTextChangedListener(recalcWatcher);
    swapTexts(c1, c2);
    swapTexts(v1, v2);
    c1.addTextChangedListener(recalcWatcher);
    v1.addTextChangedListener(recalcWatcher);
    if (calcTypeIsVolume()) {
      c2.addTextChangedListener(recalcWatcher);
    } else {
      v2.removeTextChangedListener(recalcWatcher);
    }
  }

  /**
   * main calculation method: get values, check preconditions, set labels and
   * result values
   */
  private void calculate() {
    Value[] vals = getValues();
    if (vals.length == 0) {
      return;
    }
    setThirdLabel(vals[2]);
    if (vals[2].isZero()) {
      return;
    }
    try {
      Value result = Core.dilution(vals[0], vals[1], vals[2]);
      if (calcTypeIsVolume()) {
        ((EditText) findViewById(R.id.editV2)).setText(result
            .toString());
      } else {
        ((EditText) findViewById(R.id.editC2)).setText(result
            .toString());
      }
    } catch (NumberFormatException e) {
      alert(e);
    } catch (ArithmeticException e) {
      alert(e);
    } catch (IllegalArgumentException e) {
      alert(e.getMessage());
    }
  }

  /**
   * if the thirdValue is zero, set label of the third edit text to warning
   * message, otherwise reset it to default
   */
  private void setThirdLabel(Value thirdValue) {
    int thirdLabelId;
    TextView thirdLabel;
    if (calcTypeIsVolume()) {
      thirdLabelId = R.string.c2;
      thirdLabel = labelC2;
    } else {
      thirdLabelId = R.string.v2;
      thirdLabel = labelV2;
    }
    if (thirdValue.isZero()) {
      thirdLabel.setText(getResources().getString(R.string.divByZero,
          getResources().getString(thirdLabelId)));
      thirdLabel.setTypeface(null, Typeface.ITALIC);
      thirdLabel.setTextColor(Color.RED);
    } else {
      thirdLabel.setText(thirdLabelId);
      thirdLabel.setTypeface(null, Typeface.NORMAL);
      thirdLabel.setTextColor(((TextView) findViewById(R.id.labelC1))
          .getTextColors());
    }
  }

  /**
   * @return current values from three text fields or empty array
   */
  private Value[] getValues() {
    try {
      Value valC1 = new Value(c1.getText().toString(), Core.ONE);
      Value valV1 = new Value(v1.getText().toString(), Core.ONE);
      Value third;
      if (calcTypeIsVolume()) {
        third = new Value(c2.getText().toString(), Core.ONE);
      } else {
        third = new Value(v2.getText().toString(), Core.ONE);
      }
      return new Value[] { valC1, valV1, third };
    } catch (NumberFormatException e) {
      // do nothing, input is just not completed right now
    } catch (ArithmeticException e) {
      // do nothing, input is just not completed right now
    }
    return new Value[0];
  }

  private boolean calcTypeIsVolume() {
    return ((RadioButton) findViewById(R.id.radioVolume)).isChecked();
  }

  private void alert(Exception e) {
    alert(String.format(getString(R.string.exception), e.getClass()
        .getName(), e.getMessage()), R.string.ok);
  }

  private void alert(String alertText) {
    alert(alertText, R.string.ok);
  }

  private void alert(String alertText, int buttonTextId) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(alertText).setPositiveButton(buttonTextId,
        new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
            // DO NOTHING... (zatial)
          }
        });
    AlertDialog ad = builder.create();
    ad.show();
  }

  private static void swapTexts(EditText e1, EditText e2) {
    String tmp = e1.getText().toString();
    e1.setText(e2.getText().toString());
    e2.setText(tmp);
  }

}




Java Source Code List

sk.fotopriestor.dilution.MainActivity.java
sk.fotopriestor.dilution.core.Core.java
sk.fotopriestor.dilution.core.Value.java