#android #app #development #language
Android Calculator Tutorial: Creating an Calculator App
While creating the tutorial I assumed that the viewer already has a good understand of the Java language. This tutorial is a great way to see how a simple application can be so useful.
Here’s the code for the xmls and java files:
Stays the same just be wary of package names here as this one may differ to yours now if you used the previous code.
Note. My calc.xml is named activity_main.xml in this new example but I’ll point that out to you later, it doesn’t make much difference once you are aware of it.
Basically here we create a new row of buttons, backspace (bsp), dot (.) and 2 blank un-functioning buttons as seen from the highlighted code above and the graphic below.
Graphic Layout looks like this once all code is added:
Figure 1. Calculator design
Listing 4. Strings.xml
From the above code we can see the additions of backspace, dot, blank and blank2.
Okay, from the code above we have modified it so we can add extra functionality. See the highlighted text for reference and feel free to copy and paste it into your project. Not all changes are highlighted as they repeat themselves for different functionality in the code. We have declared a dot and backspace button, added their onclick listeners and added their functionality also.
With respect to the backspace button we used this code:
It basically gets the edit texts characters, if it’s greater than 0 we create a new string called newText consisting of String textInBox’s characters minus 1 character and assigns it to the display.
Adding the dot is like adding the numbers to the display, it’s the same principle. It’s functionality though requires the working out of decimals in our calculations.
So now we also have to accommodate for decimal numbers as integers will not suffice here. We did this by declaring op3 and op4, both doubles and modifying the code accordingly when carrying out the operations.
You can see where I blanked out the old code and put in the new above.
Just note my package name may differ from yours, so you will more than likely need to change it where necessary.
Listing 6. AndroidManifest.xml
You should now have the extra functionality of a decimal point button and a backspace button with the relevant fixes. It will now divide, multiply, subtract and add decimal values and also output a decimal answer for you. I hope this updated code is useful to you and I hope you enjoyed this answer. Happy coding 🙂
Has designed and developed several android applications from scratch which are available on Google Play.
What did you think of this post?
Well done, everything is nice, but when I press one number and equal my program crashing.Could you help me with that? And one more thing, I press many numbers and the cursor goes first and when i press backspace the cursor stay first and the last number deletrs. But I want the cursor to appear last char. I appreciate your work.
[+1 year ago] Answer it
This tutorial was very helpful to build my basics. I was able to build my own app for Play store. Thank you Sir. App Link. https://goo.gl/oMlqmn
[+1 year ago] Answer it
To have full access to this post (or download the associated files) you must have MrBool Credits .
See the prices for this post in Mr.Bool Credits System below:
Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.
Package of 10 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!
Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!
You must be logged to download.
[Close] You must be logged to give your feedback.