Reputation: 79
I am trying to build a calculator on android studio. My problem is that in order to get a solution from either values, the user will have to press the "equals" button. I'm trying to avoid that and have real time calculations appear in the TextView (when two numbers are combined with the addition symbol, the sum will appear once the second operator is called). If anyone can provide me with some insight that would be great, I'm not a very experienced android developer as of yet. Thanks!
public class MainActivity extends AppCompatActivity {
Button button0, button1, button2, button3, button4, button5, button6, button7, button8,
button9, buttonAdd, buttonSub, buttonMult, buttonDiv, buttonDec, buttonEqual, buttonAC;
TextView calculate;
boolean addition, subtraction, division, multiplication;
float first, second;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = (Button) findViewById(R.id.button_num0);
button1 = (Button) findViewById(R.id.button_num1);
button2 = (Button) findViewById(R.id.button_num2);
button3 = (Button) findViewById(R.id.button_num3);
button4 = (Button) findViewById(R.id.button_num4);
button5 = (Button) findViewById(R.id.button_num5);
button6 = (Button) findViewById(R.id.button_num6);
button7 = (Button) findViewById(R.id.button_num7);
button8 = (Button) findViewById(R.id.button_num8);
button9 = (Button) findViewById(R.id.button_num9);
buttonAdd = (Button) findViewById(R.id.button_add);
buttonSub = (Button) findViewById(R.id.button_subtract);
buttonDiv = (Button) findViewById(R.id.button_divide);
buttonMult = (Button) findViewById(R.id.button_multiply);
buttonDec = (Button) findViewById(R.id.button_decimal);
buttonEqual = (Button) findViewById(R.id.button_equal);
buttonAC = (Button) findViewById(R.id.button_clear);
calculate = (TextView) findViewById(R.id.text_current);
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate.setText(calculate.getText() + button0.getText().toString());
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate.setText(calculate.getText() + button1.getText().toString());
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate.setText(calculate.getText() + button2.getText().toString());
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate.setText(calculate.getText() + button3.getText().toString());
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate.setText(calculate.getText() + button4.getText().toString());
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate.setText(calculate.getText() + button5.getText().toString());
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate.setText(calculate.getText() + button6.getText().toString());
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate.setText(calculate.getText() + button7.getText().toString());
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate.setText(calculate.getText() + button8.getText().toString());
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate.setText(calculate.getText() + button9.getText().toString());
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (calculate.getText().equals(null)) {
calculate.setText("");
}
else {
first = Float.parseFloat(calculate.getText() + "");
calculate.setText(calculate.getText() + "+");
addition = true;
calculate.setText(null);
}
}
});
buttonDec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (calculate.getText().equals(null)) {
calculate.setText("");
}
else {
calculate.setText(calculate.getText() + ".");
}
}
});
buttonMult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (calculate.getText().equals(null)) {
calculate.setText("");
}
else {
first = Float.parseFloat(calculate.getText() + "");
calculate.setText(calculate.getText() + "*");
multiplication = true;
calculate.setText(null);
}
}
});
buttonSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (calculate.getText().equals(null)) {
calculate.setText("");
}
else {
first = Float.parseFloat(calculate.getText() + "");
calculate.setText(calculate.getText() + "-");
subtraction = true;
calculate.setText(null);
}
}
});
buttonDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (calculate.getText().equals(null)) {
calculate.setText("");
}
else {
first = Float.parseFloat(calculate.getText() + "");
calculate.setText(calculate.getText() + "/");
division = true;
calculate.setText(null);
}
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
second = Float.parseFloat(calculate.getText() + "");
if (addition == true) {
calculate.setText(first + second + "");
addition = false;
}
if (subtraction == true) {
calculate.setText(first - second + "");
subtraction = false;
}
if (multiplication == true) {
calculate.setText(first * second + "");
multiplication = false;
}
if (division == true) {
calculate.setText(first / second + "");
division = false;
}
}
});
}
}
Upvotes: 1
Views: 492
Reputation: 107
You have a lot of repetition.
So when you would change something for any of your buttons you will have to modify for each other buttons. Try to reduce the number of lines of code by using a method that will add the whole Listener for each buttons. By this way, you will also have a cleaner onCreate
method.
private void addListenerTo(int buttonId) {
final Button button = (Button) findViewById(buttonId);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate.setText(calculate.getText() + button.getText().toString());
}
});
}
Then you could easily add all your buttons like this:
addListenerTo(R.id.button_num0);
addListenerTo(R.id.button_num1);
addListenerTo(R.id.button_num2);
addListenerTo(R.id.button_num3);
addListenerTo(R.id.button_num4);
addListenerTo(R.id.button_num5);
addListenerTo(R.id.button_num6);
addListenerTo(R.id.button_num7);
addListenerTo(R.id.button_num8);
addListenerTo(R.id.button_num9);
You should also be able to do something similar with your operators buttons. If you need help about that, I'll help you. By doing that, your code will be much easier to read and this is a great help for us.
Upvotes: 0
Reputation: 458
Try to build logic first, keep always asking yourself, What can be done and How ?
In engineering or in schools you might have listened words like :
( +, - , *, / )
( 0 to 9 )
What should happen when operands ( Numbers ) are pressed ?
Make a boolean variable operandAdded
to false first. like :
operandAdded = false;
Whenever in each number press event make it true.
What should happen when operators ( +, -, *, / ) are pressed ?
Make a boolean variable operatorAdded
to false first. like :
operatorAdded = false;
Whenever in each operator press event make it true.
Now whenever numbers are pressed call a function which will check for 2 booleans :
if (operatorAdded && operandAdded) {
// this is where you will perform maths
// this is where you show it in the textView
// this is where make both false again
}
Hope it helps
Upvotes: 1
Reputation: 191
You can use Edittext instead of textview and implement text change listener on it. Thus whenever text changes you can get the changes and do real time calculations.
You can use following urls for references :-
https://developer.android.com/reference/android/text/TextWatcher
android on Text Change Listener
Upvotes: 0