Coding Python... on Android!
Use your Python skills to create apps on your Android device, including speech recognition and text-to-speech. Darren Yates explains how.
Normally to build Android apps, you’d either crack open the official Android Studio integrated development environment (IDE) and create those apps using the Java programming language, or you’d use a crossplatform development environment such as Xamarin and code in Microsoft’s C# language. But thanks to some clever software, you can also create basic apps for your Android smartphone or tablet using Python.
QPython is a free Python IDE available on Google Play that allows you to create Python projects and scripts directly on your Android device. Think of it as similar to the standard Python IDLE editor we’ve been using over the past year, but with a few more tricks. It’s available as two versions: QPython, which supports Python 2, and QPython3 for Python 3 code.
Like the IDLE app, QPython allows you to create standard console apps using standard Python code. Want to accept user input from the standard device keyboard? Just use the same input() statement we’ve always used. What’s more, it uses ‘standard input’, which means it’ll accept input from whichever keyboard you’re using, whether that’s the soft Android screen-based keyboard or an external one. You print to the console using the normal print() statement, too. In fact, if you’ve been with us for a while, you’ll find a good chunk of the code we’ve covered should work on QPython3.
But thanks to an extra layer originally developed by Google staffers called ‘Scripting Layer for Android’ (SL4A), QPython3 can also interact with Android device services, like Bluetooth wireless, on-board sensors, Android-style dialog boxes, even use Android’s Text-to-Speech (TTS) and speech recognition features.
The beauty of QPython is that you’re no longer tied to your laptop or PC — you’ve got a Python coding machine right there in your pocket.
ANDROID OS SUPPORT
QPython supports any version of Android starting from Gingerbread/2.3 and works equally well on tablets and smartphones. You’ll find it free on Google Play and it only needs 12MB or so of storage. You can also find more information about it at qpython.org. We’ve tested it on a range of devices, from a budget ZTE B816 dual-core/ Android 4.4 phone I bought on special for $19 at Coles, to a new Motorola Moto G5 with eight-core CPU and Android 7.0, and it worked a treat.
It began life as a general-purpose scripting layer to enable languages like Python and Ruby to connect to many of Android’s application programming interfaces (APIs), but even though SL4A is no longer actively being developed, a version has been ‘forked’ into QPython. The SL4A code itself is built on Java, but you wouldn’t know it — SL4A is easy to use and you can make simple, fun Android apps in just a few lines of Python code.
Tapping into Android’s text-to-speech (TTS) engine is an easy way to impress friends with your coding skills. Install QPython3 on your Android device, launch the app and from the main menu, select ‘programs’, scroll down and open up ‘speak.py’.
Apart from the licence code at the top, the actual app contains just four lines of code: import sl4a droid = sl4a.Android() message = droid. dialog Get Input(‘TTS’, ‘What would you like to say?’).result
The first line imports the SL4A module in the same way you would Python’s random or time modules. Next, you create an SL4A Android object called ‘droid’. In the third line, you create an Android dialog box using the dialogGetInput() function that asks the user to type in a text message. This function has two parameters — the first parameter (‘TTS’) is the box heading and the second (‘What would you like to say?’) is the message prompt to the user. The text the user types in is returned in the function’s ‘result’ variable and its then stored in a variable called ‘message’. The final code line fires up Android’s TTS engine, sending to it the text stored in the ‘message’ variable. You might have a delay of a second or two the first time you run the code, but after that, you’ll hear the dulcet tones of your Android device speaking the text entered.
For just four lines of code, it’s hard not to be impressed and from what you see, it’s all done using Python.
TAKE A PHOTO
If four lines of code is too many, how about taking a photo with your Android device in just three? In the main menu, select ‘programs’ and open up ‘take_ picture.py’. The first two lines again import the SL4A module and create an Android object called ‘droid’. The third line launches the Android device’s default camera app:
droi d.came raInteractive Captur ePicture(‘/sdcard/ qpython.jpg’)
It allows the user to snap a picture and auto-saves it to the path ‘/sdcard/qpython.jpg’.
If you’d prefer something that’s completely automated, you can have
“QPython is a free Python IDE available on Google Play that allows you to create Python projects and scripts directly on your Android device.”
Qpython3 code that returns current battery capacity to the screen.
Qpython3 gives you a standard working Python console.
Our QPython3 code creating an Android dialog alert box.
You can change the editor theme to give a clear white- on-black look.
Import SL4A and your Python apps can access Android components.
With SL4A, your Python apps can take input from the virtual keyboard.
Qpython3 comes with the excellent QEdit editor for coding your apps.