Python for Android Tutorial #1 - Using the Accelerometer

Welcome!

I’m going to write a simple tutorial series about Mobile development with Python. I’ll be using Android, but feel free to use Android or iOS. It’s a short tutorial, just to help you to understand and learn how to work with Python for Mobile.

This post will be a simple example of accelerometer usage, so let’s start.

Python Mobile development course

I - About the Tutorials

I’ll show you how to use each mobile API, like compass, camera, sensors, sound, and others. After the API, we will start to test some Python libs, as PyGame and OpenCV. I’m using Android with buildozer, but major part are compatible with iOS as well.

Must read:

Source code

Get updates Follow @aron-bordin
Star it: Star
Contribute: Fork
Download: Download

II - Index

Available tutorials:

III - Creating the app

As you can see, this tutorial is compatible with Android, iOS, OSX and Linux. So you will be able to build this Python app to Android and iOS. Create a new folder and create a new file named main.py. Now, open your terminal and run the command bellow to create Buildozer spec file.

$ buildozer init

Open your buildozer.spec and edit it. In this tutorial we are going to use Plyer, so make sure to have the following line:

requirements = kivy, plyer

You can check my version here:

[app]
title = Python Accelerometer
package.name = accelerometer
package.domain = com.wordpress.bytedebugger
source.dir = .
source.include_exts = py,png,jpg,kv,atlas
version = '1.0'
requirements = kivy, plyer
fullscreen = 1
[buildozer]
log_level = 1  

IV - Let’s code it

Now, add all imports:

from kivy.app import App #for the main app
from kivy.uix.floatlayout import FloatLayout #the UI layout
from kivy.uix.label import Label #a label to show information
from plyer import accelerometer #object to read the accelerometer
from kivy.clock import Clock #clock to schedule a method  

Create the UI:

class UI(FloatLayout):#the app ui
	def __init__(self, **kwargs):
		super(UI, self).__init__(**kwargs)
		self.lblAcce = Label(text="Accelerometer: ") #create a label at the center
		self.add_widget(self.lblAcce) #add the label at the screen
		try:
			accelerometer.enable() #enable the accelerometer
			#if you want do disable it, just run: accelerometer.disable()
			Clock.schedule_interval(self.update, 1.0/24) #24 calls per second
		except:
			self.lblAcce.text = "Failed to start accelerometer" #error

	def update(self, dt):
		txt = ""
		try:
			txt = "Accelerometer:\nX = %.2f\nY = %.2f\nZ = %2.f " %(
				accelerometer.acceleration[0], #read the X value
				accelerometer.acceleration[1], # Y
				accelerometer.acceleration[2]) # Z
		except:
			txt = "Cannot read accelerometer!" #error
		self.lblAcce.text = txt #add the correct text

This code is self explanatory. We extend the default Kivy Float Layout, and then add an Label widget with the text Accelerometer.

Then the accelerometer is enabled and an update method is scheduled.

Create the main app:

class Accelerometer(App): #our app
	def build(self):
		ui = UI()# create the UI
		return ui #show it

if __name__ == '__main__':
	Accelerometer().run() #start our app

That’s it! This is the output:

Accelerometer running on Android

To build and run it, use the command bellow:

buildozer android debug deploy run

Ok, the first tutorial is right here.

This is simple, but it is my objective. I’ll write a lot of simple tutorials as this one, so, when you need to work with a specific functionality, you will be able to find it here.

Thx for reading!

Aron Bordin

Aron Bordin
Computer Science Student and AI researcher. Always coding something fun :)

[Tutorial] Developing Android Background Services

### Welcome!In this post, I'll show you how to develop background services on Android Studio. We'll see two type of services: `Service` a...… Continue reading