Cod­ing Python... on An­droid!

Use your Python skills to cre­ate apps on your An­droid de­vice, in­clud­ing speech recog­ni­tion and text-to-speech. Dar­ren Yates ex­plains how.

APC Australia - - Contents -

Nor­mally to build An­droid apps, you’d ei­ther crack open the of­fi­cial An­droid Stu­dio in­te­grated devel­op­ment en­vi­ron­ment (IDE) and cre­ate those apps us­ing the Java pro­gram­ming lan­guage, or you’d use a cross­plat­form devel­op­ment en­vi­ron­ment such as Xa­marin and code in Mi­crosoft’s C# lan­guage. But thanks to some clever soft­ware, you can also cre­ate ba­sic apps for your An­droid smart­phone or tablet us­ing Python.


QPython is a free Python IDE avail­able on Google Play that al­lows you to cre­ate Python projects and scripts di­rectly on your An­droid de­vice. Think of it as sim­i­lar to the stan­dard Python IDLE ed­i­tor we’ve been us­ing over the past year, but with a few more tricks. It’s avail­able as two ver­sions: QPython, which sup­ports Python 2, and QPython3 for Python 3 code.

Like the IDLE app, QPython al­lows you to cre­ate stan­dard con­sole apps us­ing stan­dard Python code. Want to ac­cept user in­put from the stan­dard de­vice key­board? Just use the same in­put() state­ment we’ve al­ways used. What’s more, it uses ‘stan­dard in­put’, which means it’ll ac­cept in­put from whichever key­board you’re us­ing, whether that’s the soft An­droid screen-based key­board or an ex­ter­nal one. You print to the con­sole us­ing the nor­mal print() state­ment, too. In fact, if you’ve been with us for a while, you’ll find a good chunk of the code we’ve cov­ered should work on QPython3.

But thanks to an ex­tra layer orig­i­nally de­vel­oped by Google staffers called ‘Script­ing Layer for An­droid’ (SL4A), QPython3 can also in­ter­act with An­droid de­vice ser­vices, like Blue­tooth wire­less, on-board sen­sors, An­droid-style dia­log boxes, even use An­droid’s Text-to-Speech (TTS) and speech recog­ni­tion fea­tures.

The beauty of QPython is that you’re no longer tied to your lap­top or PC — you’ve got a Python cod­ing ma­chine right there in your pocket.


QPython sup­ports any ver­sion of An­droid start­ing from Gin­ger­bread/2.3 and works equally well on tablets and smart­phones. You’ll find it free on Google Play and it only needs 12MB or so of stor­age. You can also find more in­for­ma­tion about it at We’ve tested it on a range of de­vices, from a bud­get ZTE B816 dual-core/ An­droid 4.4 phone I bought on spe­cial for $19 at Coles, to a new Mo­torola Moto G5 with eight-core CPU and An­droid 7.0, and it worked a treat.


It be­gan life as a gen­eral-pur­pose script­ing layer to en­able lan­guages like Python and Ruby to con­nect to many of An­droid’s ap­pli­ca­tion pro­gram­ming in­ter­faces (APIs), but even though SL4A is no longer ac­tively be­ing de­vel­oped, a ver­sion has been ‘forked’ into QPython. The SL4A code it­self is built on Java, but you wouldn’t know it — SL4A is easy to use and you can make sim­ple, fun An­droid apps in just a few lines of Python code.


Tap­ping into An­droid’s text-to-speech (TTS) en­gine is an easy way to im­press friends with your cod­ing skills. In­stall QPython3 on your An­droid de­vice, launch the app and from the main menu, se­lect ‘pro­grams’, scroll down and open up ‘’.

Apart from the li­cence code at the top, the ac­tual app con­tains just four lines of code: im­port sl4a droid = sl4a.An­droid() mes­sage = droid. dia­log Get In­put(‘TTS’, ‘What would you like to say?’).re­sult

droid.tts Speak(mes­sage)

The first line im­ports the SL4A mod­ule in the same way you would Python’s ran­dom or time mo­d­ules. Next, you cre­ate an SL4A An­droid ob­ject called ‘droid’. In the third line, you cre­ate an An­droid dia­log box us­ing the di­alogGetIn­put() func­tion that asks the user to type in a text mes­sage. This func­tion has two pa­ram­e­ters — the first pa­ram­e­ter (‘TTS’) is the box head­ing and the sec­ond (‘What would you like to say?’) is the mes­sage prompt to the user. The text the user types in is re­turned in the func­tion’s ‘re­sult’ vari­able and its then stored in a vari­able called ‘mes­sage’. The fi­nal code line fires up An­droid’s TTS en­gine, send­ing to it the text stored in the ‘mes­sage’ vari­able. You might have a de­lay of a sec­ond or two the first time you run the code, but af­ter that, you’ll hear the dul­cet tones of your An­droid de­vice speak­ing the text en­tered.

For just four lines of code, it’s hard not to be im­pressed and from what you see, it’s all done us­ing Python.


If four lines of code is too many, how about tak­ing a photo with your An­droid de­vice in just three? In the main menu, se­lect ‘pro­grams’ and open up ‘take_ pic­’. The first two lines again im­port the SL4A mod­ule and cre­ate an An­droid ob­ject called ‘droid’. The third line launches the An­droid de­vice’s de­fault cam­era app:

droi d.came raIn­ter­ac­tive Captur ePic­ture(‘/sd­card/ qpython.jpg’)

It al­lows the user to snap a pic­ture and auto-saves it to the path ‘/sd­card/qpython.jpg’.

If you’d pre­fer some­thing that’s com­pletely au­to­mated, you can have

“QPython is a free Python IDE avail­able on Google Play that al­lows you to cre­ate Python projects and scripts di­rectly on your An­droid de­vice.”

Qpython3 code that re­turns cur­rent bat­tery ca­pac­ity to the screen.

Qpython3 gives you a stan­dard work­ing Python con­sole.

Our QPython3 code cre­at­ing an An­droid dia­log alert box.

You can change the ed­i­tor theme to give a clear white- on-black look.

Im­port SL4A and your Python apps can ac­cess An­droid com­po­nents.

With SL4A, your Python apps can take in­put from the vir­tual key­board.

Qpython3 comes with the ex­cel­lent QEdit ed­i­tor for cod­ing your apps.

Newspapers in English

Newspapers from Australia

© PressReader. All rights reserved.