Learn to create Calculator Android App by using OnClickListner on all buttons..
Coding is below -
package com.example.admin.designingcalcyapp; import android.content.res.Configuration; import android.net.Uri; import android.renderscript.Sampler; 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; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.appindexing.Thing; import com.google.android.gms.common.api.GoogleApiClient; public class MainActivity extends AppCompatActivity {1.
Below is Variable Declaration
private EditText result; private EditText newNumber; private TextView displayOperation; private Double operand1 = null; private String pendingOperation = "="; private static final String STATE_PENDING_OPERATION = "PendingOperation"; private static final String STATE_OPERAND1 = "Operand1"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
2.
Below is Variable Casting
result = (EditText) findViewById(R.id.result); newNumber = (EditText) findViewById(R.id.newNumber); displayOperation = (TextView) findViewById(R.id.operation); Button button0 = (Button) findViewById(R.id.button0); Button button1 = (Button) findViewById(R.id.button1); Button button2 = (Button) findViewById(R.id.button2); Button button3 = (Button) findViewById(R.id.button3); Button button4 = (Button) findViewById(R.id.button4); Button button5 = (Button) findViewById(R.id.button5); Button button6 = (Button) findViewById(R.id.button6); Button button7 = (Button) findViewById(R.id.button7); Button button8 = (Button) findViewById(R.id.button8); Button button9 = (Button) findViewById(R.id.button9); Button buttonDot = (Button) findViewById(R.id.buttonDot);
Button buttonAdd = (Button) findViewById(R.id.buttonAdd);
Button buttonSub = (Button) findViewById(R.id.buttonSub);
Button buttonDiv = (Button) findViewById(R.id.buttonDiv);
Button buttonMult = (Button) findViewById(R.id.buttonMult);
Button buttonEquals = (Button) findViewById(R.id.buttonEqual);
3 - a
Below is the Listner
method, which is called when any number ie 0 to 9 and button dot is pressed.
View.OnClickListener Listner = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button) v;
newNumber.append(b.getText().toString()); // whatever number you pressed will shown in edittext number widgets box newNumber..
}
};
@Override
public void onClick(View v) {
Button b = (Button) v;
newNumber.append(b.getText().toString()); // whatever number you pressed will shown in edittext number widgets box newNumber..
}
};
3 - b
Setting
OnClickListner on all numbers and Dot (.)
button0.setOnClickListener(Listner);
button1.setOnClickListener(Listner);
button2.setOnClickListener(Listner);
button3.setOnClickListener(Listner);
button4.setOnClickListener(Listner);
button5.setOnClickListener(Listner);
button6.setOnClickListener(Listner);
button7.setOnClickListener(Listner);
button8.setOnClickListener(Listner);
button9.setOnClickListener(Listner);
buttonDot.setOnClickListener(Listner);
4 - a
Below is the opListner
method, which is called when any operator ie +, -, *, /, = is pressed.
View.OnClickListener opListner = new View.OnClickListener(
) {
@Override
public void onClick(View v) {
Button b = (Button) v;
String op = b.getText().toString();
String value = newNumber.getText().toString();
try {
Double doubleValue = Double.valueOf(value);
performOperation(doubleValue, op);
} catch (NumberFormatException e) {
newNumber.setText("");
}
pendingOperation = op;
displayOperation.setText(pendingOperation);
}
};
) {
@Override
public void onClick(View v) {
Button b = (Button) v;
String op = b.getText().toString();
String value = newNumber.getText().toString();
try {
Double doubleValue = Double.valueOf(value);
performOperation(doubleValue, op);
} catch (NumberFormatException e) {
newNumber.setText("");
}
pendingOperation = op;
displayOperation.setText(pendingOperation);
}
};
4 - b
Setting
OnClickListner on all operators ie on +, -, /, *, =
buttonEquals.setOnClickListener(opListner);
buttonAdd.setOnClickListener(opListner);
buttonMult.setOnClickListener(opListner);
buttonDiv.setOnClickListener(opListner);
buttonSub.setOnClickListener(opListner);
5.
buttonAdd.setOnClickListener(opListner);
buttonMult.setOnClickListener(opListner);
buttonDiv.setOnClickListener(opListner);
buttonSub.setOnClickListener(opListner);
5.
Below is the
function for Clear button ( buttonC )
final Button
buttonC = (Button)findViewById(R.id.buttonC);
buttonC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.setText("");
newNumber.setText("");
operand1 = null;
pendingOperation = "=";
}
});
6.
buttonC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.setText("");
newNumber.setText("");
operand1 = null;
pendingOperation = "=";
}
});
6.
Below is the
Function for Negative button ( buttonNeg )
Button buttonNeg =
(Button)findViewById(R.id.buttonNeg);
buttonNeg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String value = newNumber.getText().toString();
if (value.length()==0){
newNumber.setText("-");
}
else {
try {
Double doubleValue = Double.valueOf(value);
doubleValue *= -1;
newNumber.setText(doubleValue.toString());
} catch (NumberFormatException e) {
//newNumber was - or .
newNumber.setText("");
}
}
}
});
}
7.
buttonNeg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String value = newNumber.getText().toString();
if (value.length()==0){
newNumber.setText("-");
}
else {
try {
Double doubleValue = Double.valueOf(value);
doubleValue *= -1;
newNumber.setText(doubleValue.toString());
} catch (NumberFormatException e) {
//newNumber was - or .
newNumber.setText("");
}
}
}
});
}
7.
Below are methods used to
save data across orientation changes ie,
OnSaveInstanceState(),
OnRestoreInstanceState(), OnConfigurationChanged()
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString(STATE_PENDING_OPERATION, pendingOperation);
if (operand1 != null) {
outState.putDouble(STATE_OPERAND1, operand1);
}
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
pendingOperation = savedInstanceState.getString(STATE_PENDING_OPERATION);
operand1 = savedInstanceState.getDouble(STATE_OPERAND1);
displayOperation.setText(pendingOperation);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
8.
Below is the
performOperation Method which is used to perform various types of operations (
+, /, *, - )
private void performOperation (Double
value, String operation){
if(null == operand1){
operand1 = value;
}
else {
if (pendingOperation.equals("=")){
pendingOperation = operation;
}
switch (pendingOperation){
case "=":
operand1 = value;
break;
case "/":
if (value==0){
operand1 = 0.0;
}else {
operand1/=value;
}
break;
case "*":
operand1 *= value;
case "-":
operand1 -= value;
break;
case "+":
operand1 += value;
break;
}
}
result.setText(operand1.toString());
newNumber.setText("");
}
}
if(null == operand1){
operand1 = value;
}
else {
if (pendingOperation.equals("=")){
pendingOperation = operation;
}
switch (pendingOperation){
case "=":
operand1 = value;
break;
case "/":
if (value==0){
operand1 = 0.0;
}else {
operand1/=value;
}
break;
case "*":
operand1 *= value;
case "-":
operand1 -= value;
break;
case "+":
operand1 += value;
break;
}
}
result.setText(operand1.toString());
newNumber.setText("");
}
}