Skip to content

HelloWorld

João Vítor edited this page Feb 17, 2018 · 4 revisions

Creating a hello world app

Before we start creating amazing apps with Python, we'll need some things installed:

  • Python3.4 or Python3.5
  • Java 7 or Java 8 JDK, download here
  • Android SDK (with an emulator configured or with your android phone in developer mode) download here

Create your virtualenv and activate it

$ virtualenv androidapps -p python3.5
$ source androidapps/bin/activate

And install the cookiecutter via pip $ pip install cookiecutter

We need to generate the application with the cookiecutter, run:

$ cookiecutter https://github.com/eliasdorneles/beeware-android-template

This command will make a few questions about your app to generate it, it is important remember the appname. Enter into the app folder and install the requirements:

$ cd appname
$ pip install -r requirements-dev.txt

Now you're ready to run your app on your phone or emulator, just need to:

$ python setup.py android --start

You should see this on your phone:

Hello World Screenshot

Now we'll understand the code

When you open the appname/app.py file you see the class MainApp, this class is your app. The method onCreate() is called when your open the app on your phone:

def onCreate(self):
        label = TextView(self._activity)
        label.setTextSize(50)
        label.setText('Hello World')

in it we create a label that is an instance of the android widget TextView, in next lines we set the text size and the own text with the methods setTextSize() and setText(), after we setup a Linear Layout:

vlayout = LinearLayout(self._activity)
vlayout.setOrientation(LinearLayout.VERTICAL)
vlayout.addView(label)

set its orientation, and probably the most important thing in our apps, we added our TextView to the main view. At last we set the Linear Layout as the main view.

self._activity.setContentView(vlayout)
Clone this wiki locally