Python for Android Tutorial #1 - Using the Accelerometer
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.
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.
|Get updates||Follow @aron-bordin|
II - Index
- Tutorial #1 - Accelerometer
- Tutorial #2 - Camera
- Tutorial #3 - Kivy Designer
- Tutorial #4 - Debug
- Tutorial #5 - Android and iOS game with Python
- Tutorial #6 - Reading and saving user data
- Tutorial #7 - Use Android classes with PyJNIus
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.
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:
IV - Let’s code it
Now, add all imports:
Create the UI:
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:
That’s it! This is the output:
To build and run it, use the command bellow:
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!