Call
(317)643-0663
for consultation
Experience Programming
.
Home
Pricing
About
Contact
Android Development Example: Children's Calculator
Main Activity Class
.
Back to Examples
package com.example.childrencalculator;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends Activity {
private Button button0;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
private Button button8;
private Button button9;
private Button buttonClear;
private Button buttonDivide;
private Button buttonMultiply;
private Button buttonAdd;
private Button buttonSubtract;
private TextView numberTextView;
private TextView smileyTextView;
public int currentAmount=0;
public int buttonClicked =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberTextView = (TextView) findViewById(R.id.numberTextView);
smileyTextView = (TextView) findViewById(R.id.smileyTextView);
buttonClear = (Button) findViewById(R.id.clearButton);
button0 = (Button) findViewById(R.id.zeroButton);
button1 = (Button) findViewById(R.id.oneButton);
button2 = (Button) findViewById(R.id.twoButton);
button3 = (Button) findViewById(R.id.threeButton);
button4 = (Button) findViewById(R.id.fourButton);
button5 = (Button) findViewById(R.id.fiveButton);
button6 = (Button) findViewById(R.id.sixButton);
button7 = (Button) findViewById(R.id.sevenButton);
button8 = (Button) findViewById(R.id.eightButton);
button9 = (Button) findViewById(R.id.nineButton);
buttonDivide = (Button) findViewById(R.id.divideButton);
buttonMultiply = (Button) findViewById(R.id.multiplyButton);
buttonAdd = (Button) findViewById(R.id.plusButton);
buttonSubtract = (Button) findViewById(R.id.minusButton);
buttonClear = (Button) findViewById(R.id.clearButton);
//Write the event handler here:
buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberTextView.setText("");
smileyTextView.setText("");
currentAmount = 0;
buttonClicked = 0;
}
});
button1 = (Button) findViewById(R.id.oneButton);
//Write the event handler here:
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberTextView.setText("1");
buttonClicked = 1;
}
});
button2 = (Button) findViewById(R.id.twoButton);
//Write the event handler here:
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberTextView.setText("2");
buttonClicked = 2;
}
});
button3 = (Button) findViewById(R.id.threeButton);
//Write the event handler here:
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberTextView.setText("3");
buttonClicked = 3;
}
});
button4 = (Button) findViewById(R.id.fourButton);
//Write the event handler here:
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberTextView.setText("4");
buttonClicked = 4;
}
});
button5 = (Button) findViewById(R.id.fiveButton);
//Write the event handler here:
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberTextView.setText("5");
buttonClicked = 5;
}
});
button6 = (Button) findViewById(R.id.sixButton);
//Write the event handler here:
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberTextView.setText("6");
buttonClicked = 6;
}
});
button7 = (Button) findViewById(R.id.sevenButton);
//Write the event handler here:
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberTextView.setText("7");
buttonClicked = 7;
}
});
button8 = (Button) findViewById(R.id.eightButton);
//Write the event handler here:
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberTextView.setText("8");
buttonClicked = 8;
}
});
button9 = (Button) findViewById(R.id.nineButton);
//Write the event handler here:
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberTextView.setText("9");
buttonClicked = 9;
}
});
button0 = (Button) findViewById(R.id.zeroButton);
//Write the event handler here:
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberTextView.setText("0");
buttonClicked = 0;
}
});
buttonDivide = (Button) findViewById(R.id.divideButton);
//Write the event handler here:
buttonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (buttonClicked != 0) {
numberTextView.setText(Integer.toString(Calculator.divideInput(currentAmount, buttonClicked)));
currentAmount = Calculator.divideInput(currentAmount, buttonClicked);
smileyTextView.setText(Faces.determineFace(currentAmount));
}
}
});
buttonMultiply = (Button) findViewById(R.id.multiplyButton);
//Write the event handler here:
buttonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberTextView.setText(Integer.toString(Calculator.multiplyInput(currentAmount, buttonClicked)));
currentAmount = Calculator.multiplyInput(currentAmount, buttonClicked);
smileyTextView.setText(Faces.determineFace(currentAmount));
}
});
buttonAdd = (Button) findViewById(R.id.plusButton);
//Write the event handler here:
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberTextView.setText(Integer.toString(Calculator.addInput(currentAmount, buttonClicked)));
currentAmount = Calculator.addInput(currentAmount, buttonClicked);
smileyTextView.setText(Faces.determineFace(currentAmount));
}
});
buttonSubtract = (Button) findViewById(R.id.minusButton);
//Write the event handler here:
buttonSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
numberTextView.setText(Integer.toString(Calculator.subtractInput(currentAmount, buttonClicked)));
currentAmount = Calculator.subtractInput(currentAmount, buttonClicked);
smileyTextView.setText(Faces.determineFace(currentAmount));
}
});
}
}
Back to Examples
Startup Discount
Small Business?
Contact us for an
Enormous
Discount!
Contact Us Today!