package com.example.admin.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText firstNum=(EditText)findViewById(R.id.firstNum); final EditText secNum =(EditText)findViewById(R.id.secNum); Button plusButton = (Button)findViewById(R.id.plusButton); Button minusButton = (Button)findViewById(R.id.minusButton); Button mulButton = (Button)findViewById(R.id.mulButton); Button divButton = (Button)findViewById(R.id.divButton); //TextView theResult = (TextView)findViewById(R.id.theResult); plusButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if ((firstNum.getText().toString().isEmpty() || secNum.getText().toString().isEmpty())) { TextView theResult = (TextView)findViewById(R.id.theResult); theResult.setText("Missing Fields"); } else { if (isNumeric(firstNum.getText().toString()) && isNumeric(secNum.getText().toString())){ Double x = Double.parseDouble(firstNum.getText().toString()); Double y = Double.parseDouble(secNum.getText().toString()); TextView theResult = (TextView)findViewById(R.id.theResult); theResult.setText((x + y) + "");} else { TextView theResult = (TextView)findViewById(R.id.theResult); theResult.setText("Non Numeric Value"); } } } }); minusButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if ((firstNum.getText().toString().isEmpty() || secNum.getText().toString().isEmpty())) { TextView theResult = (TextView)findViewById(R.id.theResult); theResult.setText("Missing Fields"); } else { if (isNumeric(firstNum.getText().toString()) && isNumeric(secNum.getText().toString())){ Double x = Double.parseDouble(firstNum.getText().toString()); Double y = Double.parseDouble(secNum.getText().toString()); TextView theResult = (TextView)findViewById(R.id.theResult); theResult.setText((x - y) + "");} else { TextView theResult = (TextView)findViewById(R.id.theResult); theResult.setText("Non Numeric Value"); } } } }); mulButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if ((firstNum.getText().toString().isEmpty() || secNum.getText().toString().isEmpty())) { TextView theResult = (TextView)findViewById(R.id.theResult); theResult.setText("Missing Fields"); } else { if (isNumeric(firstNum.getText().toString()) && isNumeric(secNum.getText().toString())){ Double x = Double.parseDouble(firstNum.getText().toString()); Double y = Double.parseDouble(secNum.getText().toString()); TextView theResult = (TextView)findViewById(R.id.theResult); theResult.setText((x * y) + "");} else { TextView theResult = (TextView)findViewById(R.id.theResult); theResult.setText("Non Numeric Value"); } } } }); divButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if ((firstNum.getText().toString().isEmpty() || secNum.getText().toString().isEmpty())) { TextView theResult = (TextView)findViewById(R.id.theResult); theResult.setText("Missing Fields"); } else { if (isNumeric(firstNum.getText().toString()) && isNumeric(secNum.getText().toString())){ Double x = Double.parseDouble(firstNum.getText().toString()); Double y = Double.parseDouble(secNum.getText().toString()); TextView theResult = (TextView)findViewById(R.id.theResult); theResult.setText((x / y) + "");} else { TextView theResult = (TextView)findViewById(R.id.theResult); theResult.setText("Non Numeric Value"); } } } }); } public static boolean isNumeric(String strNum) { try { double d = Double.parseDouble(strNum); } catch (NumberFormatException | NullPointerException nfe) { return false; } return true; } }