Android application using Kivy in Python

In this tutorial, we’ll learn how to make a simple Android Application using Kivy in Python. If you, like me, find it difficult to make an app in Java, using Kivy is your best bet. Plus, an android application using Kivy is very simple to understand in Python.

Installing Kivy and Buildozer in Python

For this tutorial, you need to make sure you have installed Kivy and Buildozer.

pip3 install kivy
pip3 install --user --upgrade buildozer

Buildozer runs your python program on Android.
For more information visit Kivy documentation and Buildozer documentation.

Imports

Since this is a simple tutorial, we’ll only be importing App, label, button, and BoxLayout.

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout

Adding Labels and Buttons in Kivy for Python

Now, we’ll see how to create a label and a button. The only thing you have to do is to call the Label() function if you’re creating a label and call Button() function for buttons. You will also have to give a text parameter that decides the text to be displayed on the UIX. You can also add more parameters like colors, height, width, etc.
Write the below code in the main driver code.

label=Label(text="Hello world!")
but=Button(text="Click here",size_hint=(0.5,0.5),pos_hint={'center_x': 0.5, 'center_y': 0.5})

Creating Myapp() class in Kivy

We need to create a class Myapp. An object of this class will be instantiated everytime we call the app.

class Myapp(App):		
	def build(self):
		
		but.bind(on_press=self.button_pressed)
	
		l=BoxLayout(orientation="vertical")
		l.add_widget(label)
		l.add_widget(but)
		
		return l
	
	def button_pressed(self,instance):
		label.text="How are you?"

The build() function will automatically be called when the class is instantiated.

The bind() function on the button is called to define what happens in the event of a button click.
We then create a box layout in which we can add our existing widgets i.e. label and button in a vertical fashion.

We now create a button_pressed() function which is called when we press the button. This function changes the text on the label to “How are you?”.

Running our App – Kivy

We now move on to the final phase of our code. We create an instance of our App and run it in the main driver code.

myApp=Myapp()
myApp.run()

Code: Android application using Kivy in Python

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout

class Myapp(App):
        
  def build(self):
    
    but.bind(on_press=self.button_pressed)
  
    l=BoxLayout(orientation="vertical")
    l.add_widget(label)
    l.add_widget(but)
    
    return l
  
  def button_pressed(self,instance):
    label.text="How are you?"	
    
if __name__=="__main__":
  label=Label(text="Hello world!")
  but=Button(text="Click here",size_hint=(0.5,0.5),pos_hint={'center_x': 0.5, 'center_y': 0.5})
    
  myApp=Myapp()
  myApp.run()

Output:

Before clicking the button:

Android application using Kivy in Python

After clicking the button:

Android application using Kivy in Python

Running on android: Kivy application

First, rename your file to “main.py” and move to the directory with the “main.py” file.

Then type the following on the terminal:

buildozer init

This creates a “buildozer.spec” file.

Now, connect your Android mobile to your computer and type the following on the terminal:

buildozer -v android debug deploy run logcat

This will compile, deploy the application on your mobile, run it, and get back the log into the console.

Also read:

Prediction Intervals in Python using Machine learning

KNN Classification using Scikit-Learn in Python

Leave a Reply

Your email address will not be published. Required fields are marked *