𝓖𝓻𝓮𝓮𝓽𝓲𝓷𝓰𝓼
Hello, beautiful members of the community. In lecture 19 we wrote all the logic for all the calculator buttons. Today when I am getting ready for our new app journey I found some mistakes in our java code. These are small mistakes but we need to cover them. I am also thankful for @eniolw for notifying me about a spelling mistake that we have made throughout the android app development series. Our thumbnail contains the word Andriod and I fixed it today. Thanks.
GitHub Link
Use this GitHub project to clone into your directory. It will constantly get updated in the following lecture so you will never miss the latest code. Happy Coding!.
What Should I Learn
- Fix the issues of Lecture 19
Assignment
- Show the correct answer on screen1.
Procedure
The first mistake that we made is, We skip the testing of our code at the end of the lecture. Today I calculate two numbers and something is missing on the screen. Let's check it first.
So we need to change a line of code in the equal_btn click listener. When the button click we need to pick the text first and store it in a variable as we did in the first_num variable. We also need to change the data type for first_num. We will use float
Here is the code for the plus sign and then we will use the same logic for other operations accordingly.
String stored_sign = screen2_tv.getText().toString();
float second_num = Float.parseFloat(screen1_tv.getText().toString());
if (stored_sign.equals("+")) {
float result = first_num + second_num);
screen1_tv.setText(first_num + stored_sign + second_num + " = " + result);
Now run the app and check the result.
We also need to fix the code for the trim_btn. If there is no text on the screen1 is crashing the app sometimes. So here we use the length rather than check the empty text in screen1. This will fix the crash on the trim button click for any time.
if (screen1_tv.getText().length() > 0) {
screen1_tv.setText(screen1_tv.getText().toString().substring(0, screen1_tv.getText().length() - 1));
}