Calculator Android App using Python | Convert .Py to APK | #Kivy Tutorial

Updated: Sep 16

Hello readers, I am back with a very different project. In this project, we are going to build the calculator android app using Python Kivy. So let's start this interesting project.




Requirements of this project

  • Kivy

  • KivyMD

  • Google Colab

Now, we need to divide this work into steps.

  • First, create a simple calculator program using Python which can run easily on the desktop.

  • Now, we need to use Google Colab for converting python file to APK.

Let's Start the code.


We need to install kivy and kivymd using pip into the local system.

pip install kivy
pip install kivymd

Create a new python file which is called "Main.py"

- Let's import the necessary requirements

from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager

- Now we need to build the app design using kivy, For these, it's required to have knowledge of the kivy design.


Builder_string = '''
ScreenManager:
    Main:
<Main>:
    name : 'Calculator App'
    BoxLayout:
        orientation: "vertical"
        MDToolbar:
            title: 'Calculator App'
            md_bg_color: app.theme_cls.primary_color
            specific_text_color: 1, 1, 1, 1
        MDTextField:
            id: val1
            input_filter: 'float'
            hint_text: "Enter the first value"
            color_mode: 'custom'
            helper_text_mode: "on_focus"
        MDTextField:
            id: val2
            input_filter: 'float'
            hint_text: "Enter the second value"
            color_mode: 'custom'
            helper_text_mode: "on_focus"
        MDTextField:
            id: val3
            hint_text: "Result"
            readonly : "True"
            color_mode: 'custom'
            icon_right_color: app.theme_cls.primary_color
            icon_right: 'equal-box'
        
        MDRoundFlatIconButton:
            id:add
            text: "Addition"
            icon:"calculator"
            pos_hint: {"center_x": .5, "center_y": .6}
            on_press: app.addition()
        MDRoundFlatIconButton:
            id:sub
            text: "Substraction"
            icon:"calculator"
            pos_hint: {"center_x": .5, "center_y": .6}
            on_press: app.sub()
        MDRoundFlatIconButton:
            id:add
            text: "Multification"
            icon:"calculator"
            pos_hint: {"center_x": .5, "center_y": .6}
            on_press: app.multi()
        MDRoundFlatIconButton:
            id:div
            text: "Division"
            icon:"calculator"
            pos_hint: {"center_x": .5, "center_y": .6}
            on_press: app.div()
        MDSpinner:
            id: rc_spin
            size_hint: None, None
            size: dp(46), dp(46)
            pos_hint: {'center_x': .5, 'center_y': .5}
            active: False
        MDLabel:
            id: result
    '''

- We have created the design of our android app, now we need to create the functionality of the backend. We need to initialize the screen manager so we can give the screen to our app.


classMain(Screen):
    pass
sm=ScreenManager()
sm.add_widget(Main(name='Calculator_App'))

- Now, we need to build the design, "Builder.load_string" will help us to do it. We need to pass our kivy design to load_string. After that, we need to create functionality for each button like addition, sub, div, multiplication. Our code will look like this.



class MainApp(MDApp):
    def build(self):
        self.help_string = Builder.load_string(Builder_string)
        self.title = 'Calculator App'
        # Here we are returning our design
        return self.help_string
    # Function for addition
    def addition(self):
    # get the values from the textbox
        val1 = int(self.help_string.get_screen('Calculator App').ids.val1.text)
        val2 = int(self.help_string.get_screen('Calculator App').ids.val2.text)
        # Do the addition and display it into result textbox
        res = val1 + val2
        self.help_string.get_screen('Calculator App').ids.val3.text = "The Addition is: " +str(res)

    def sub(self):
        val1 = int(self.help_string.get_screen('Calculator App').ids.val1.text)
        val2 = int(self.help_string.get_screen('Calculator App').ids.val2.text)
        res = val1 - val2
        self.help_string.get_screen('Calculator App').ids.val3.text = "The Subtraction is: " +str(res)

    def multi(self):
        val1 = int(self.help_string.get_screen('Calculator App').ids.val1.text)
        val2 = int(self.help_string.get_screen('Calculator App').ids.val2.text)
        res = val1 * val2
        self.help_string.get_screen('Calculator App').ids.val3.text = "The Multification is: " +str(res)

    def div(self):
        val1 = int(self.help_string.get_screen('Calculator App').ids.val1.text)
        val2 = int(self.help_string.get_screen('Calculator App').ids.val2.text)
        res = val1 / val2
        self.help_string.get_screen('Calculator App').ids.val3.text = "The Division is: " +str(res)

We have defined the functionality of the backend code, now we need to see the running of code, for now we can check the code is running fine or not on the desktop.



Now our next part is to convert this kivy app to an android app.

  • For this task, we need to have Linux. It will not work with the windows.

  • That's why we will use Google Colab here.

Let's start it.


- Download this notebook, and upload it into colab. (all the notes described in the notebook).

- Upload our Main.py file into colab runtime, run each cell one by one.

- It will create one buildozer.spec file, open that file in colab editor,

- Go to line no 4, give the name of your app.

# (str) Title of your applicationtitle = My Calculator
# (str) Package namepackage.name = calc
# (str) Package domain (needed for android/ios packaging)package.domain = org.calc

- Now go to the 39 lines of .spec file, and mentioned requirements

# (list) Application requirements# comma separated
requirements = python3,kivy,kivymd

- Now run "!buildozer -v android debug" in the colab, wait for 15-20 minutes, it will generate the .APK file in bin directory in colab.

- Download that .APK into your mobile and check it is running.



Access the full project on the Github

Watch the full tutorial with an explanation.




Donation

Here we are just asking for the donations, if you want to donate us something then my UPI id and Paypal ID are mentioned below. We are creating quality content on youtube and making blogs for the same and publishing code openly. We are doing an open-source contribution.


UPI ID - kushalbhavsar58@ybl

Paypal - https://paypal.me/spidy1820

Wise - kushalbhavsar58@gmail.com


Note - We are just asking for donations.