Building Android Apps With Python: Part -3 (Conclusion)

Step by Step Guide to Build Android Apps using Python

Kaustubh Gupta
Towards Data Science

--

In the last two parts, we have seen how Kivy and Kivymd make it super easy to develop apps using Python with its drawbacks. We have covered the basics of app development, how to display text, take input, and use buttons to make our app interactive. We have also seen various UI/UIX elements that are supported by Kivy and how they can be easily implemented using Kivy String Builders which are written in a hierarchical format and don't require any explicit import statements. If you haven’t read the previous parts, I recommend you have a look at them for better understanding.

In this part, we will cover all the remaining frequently used elements in Kivymd, and in another part, we will build our capstone app which will fetch weather-related information using weather API, convert that app into Android APK and deploy it on an online platform. Isn’t this cool?

Photo by Mike Szczepanski on Unsplash

MDDialog

Last time we built a basic app that takes a password as input, compares it with our keyword “root” and displays success or failure message but as text on the screen. What if a dialog box pops up which not only displays the result of the action but gives more options if applicable? This type of function can be made with the help of MDDialog. Let’s look at its implementation:

See how the Dialog was called when the comparison was made

The output looks like this:

GIF by Author

Various properties can be modified in a dialog box like:

  1. various types of events including on_pre_open, on_open, on_pre_dismiss, on_dismiss
  2. “title” of the dialog box. In the example, the Password check is the title of the box.
  3. “text” of the box. Here we have success or failure as the text.
  4. control the “radius” of the box and make it round, elliptical.
  5. add buttons, list, make it a form that can take inputs, and many more.

Read more about these features here.

MDList (One, Two & Three line list item)

The list is one of the most commonly used entities in app development. It is a vertical continuous index of text with images. MDList is combined with a scroll view and list items to make a functional list. We have different types of list items in Kivymd ranging from OneLineListItem to ThreeLineAvatarIconListItem. The example below covers all type of items in a list:

Look at its output:

All different types of list items

The basic difference between avatar and icon is that icons are mostly predefined, freely available and they don’t need to be explicitly referenced via source. Avatar is more of an image that is referenced to a profile though it can be used in a list item. The items contain text which can be modified using the text properties of MDLabel.

MDDataTables

Datatables are used to display tabular data in the form of rows and columns. It is a simple yet powerful utility offered by Kivymd. It can be linked with a button or any other action. The row items can be combined with checkboxes making a to-do list in a table format. You can add pagination, control the number of rows to be displayed. Let’s check it’s code:

And the interesting output:

GIF by Author

MDToolbar

You all have seen in android apps we have a top stripped portion where the name of the app is mentioned with a stack of three lines on the left and three dots on the right side. It is called a toolbar and it is very essential in an app to make it more easily accessible and organize different actions. As always Kivymd comes with options and this one also has two options: Top toolbar and bottom toolbar. Both have their advantages and features but for this series, we will focus on Top toolbar. If you want to know more about the bottom toolbar then follow this link. A simple toolbar is coded as follows:

Image by Author

Now if you click on the 3 dots menu, it will crash the app as it requires a callback. Callback defines what action should be taken for that particular event. To make a functional menu, we need something called Navigation Drawer, let’s see what is it.

MDNavigationDrawer

This is where things get interesting. Now we can finally build a menu in our app with different sections to browse. Navigation Drawer makes it possible to access different destinations in our app. Before I show the code explanation, there is one more concept here. There are different types of layout Kivymd offers. The layout is a type of blueprint where all the elements can be arranged. The MDToolbar is usually placed in a BoxLayout. Learn more about layouts here. Now look at this code carefully:

GIF by Author

Here I have placed the MDToolbar in a BoxLayout while in MDNavigationDrawer, I have used an AnchorLayout so that I can have an introduction section in the menu bar. After this section, we have the MDList having two destinations, Home and About. Now that we have come so far in our android app journey, let’s see how we can work with different screens and make use of the same MDNavigation bar.

Switching Screens (ScreenManager)

As the name suggests, ScreenManager is used to manage all the screens we want to include in our app. It holds all the screens with a unique id and that id can be used to switch screens on the action. To use the same toolbar in all screens, we will place the Screen Manager and NavigationDrawer inside the NavigationLayout. Now, if we go to the Navigation bar and tap on about or information, nothing happens but we can bind this to switch screen. Just refer to the id of the screen manager and change the current screen to the screen you want. See an example here:

GIF by Author

Conclusion and What’s Next

In this part of the series, we covered more elements used in app development. We learned how to make the app interactive using dialog boxes, how to create the toolbar, and switch screens on a particular action/event. In an upcoming article, I will make an app, convert it to APK and deploy it on a cloud platform so stay tuned for that article. If you liked this development series make sure to follow me on medium so you receive notifications about upcoming articles, comment down if you have any queries, and with that said, Sayonara!

Linkedin, Github

Update: The conversion/deployment article of Kivy apps is live now!

--

--

Data Analysis | Python-Dev — Working on Data related Projects, Python, interested in real-world applications | 0.8M+ views