How To Add Fractions To Android Calculator

We got an idea on how to add another challenge for Calculator project. The idea was to add fractions to our Calculator. In this blog we will show, how you can calculate with fractions in Java. Challenge is to add this knowledge to Calculator app yourself. But this is a standalone project you can do anyway.

1. This how my solution looks like, it’s simple but you can still play with fractions and get the feel how Fraction class works.

Fractions screen

2. First you will have to create a new Android project with Empty Activity. Check our blog on How To Create A New Android Project

3. I will not go into details on how to add controls on the screen, but I have used EditText (remember to use numberDecimal, so that keyboard will enable numbers only) for fractions and TextView for result. Calculate Button is a obvious choice to calculate a result. Plus and equals sign is just a TextView with larger font. Please let me know if you would love more details for setting up the screen :D

4. We will start with existing Fraction class, that was made by Diane Kramer and you can use almost out of the box. This a good insight on what you have to do, to start a good Fraction class. We encourage you to start one by yourself to practice your Java skills, but for now we will use what Diane has prepared. 

5. Add a new class to your project and name it Fraction. In that class you will have to add a code for Fraction class from link above. You can also import existing file. Now you will have to tweak it a little bit:
   ○ Delete or comment public void input()
   ○ Delete or comment public void output()

6. We will also add a new constructor to make implementation of new Fraction object easier. It will have numerator and denominator as parameters

New Fraction constructor

7. Next we will have to introduce variables in our java class (MainActivity) for all the controls we have added to our screen.

Variables for controls

8. In onCreate method of MainActivity you will have to find all the elements and assign values to variables. We do this with findViewById

Find all controls on screen

9. Next item on our To Do is to add a onClickListener for the button:


10. All the magic will happen in inside this listener:
   ○ First we will set all the values for fractions to variables

Values for the fractions
   ○ These values will enable us to initialize our fractions. Code for initializing fractions will like this

Initialize fractions

11. And now for the result. We have read values, we have created objects for fractions. We can now calculate the result.
   ○ This is the actual calculation (you can see that adding new operation will be easy, just change “.add” to desired operation)

Actual calculation for two fractions (add)
   ○ There is just one more step left for us to do and this is displaying result. For that we will use our TextView

Display result

Adding functionality to our Calculator course will require some additional work and creativity, but after that you will have built an application that is not as easy to come across, since normal calculators don’t usually offer way for working with fractions. Please send us final solutions, pictures, code, question.