Digging Macroid – The first sod (Scala on Android)

Table of contents
Reading Time: 3 minutes

Android, an operating system runs on millions of devices, based on Linux Kernel and and uses Dalvik/ ART as their process virtual machine with trace-based just-in-time compilation to run Dalvik “dex-code”, which is usually translated from the Java bytecode.Most of the application that runs on Android are made with Java , which is an imperative programming language with powerful features. However rise of the functional programming paradigm in recent years, make us think about taking alternative approach in building new softwares. When we talk about functional programming now a days, Scala always takes a special place in our thoughts because of its strong functional features. Scala however supports both functional and object features. Scala source code is intended to be compiled to Java bytecode, so that the resulting executable code runs on a Java Virtual Machine. Hence we can definitely have some second thoughts on building android apps with Scala, and there are libraries built already for it e.g vanilla android, scaloid etc. Recently I have gone through a new scala on android library called Macroid. In the recent scala conference Scala days 2015, the official android app was built with this library and after watching Nikolay Stanchenko’s presentation video I had the urge to go and play with it.

So Macroid is another library for making android apps with scala, having pros like concise code, fantastic concurrency support, advance dsls and with cons like build time according to the macroid docs. It definitely has strong features within it, however we have to reconsider the fact that learning to build android app with macroid would not be an easy task 😉 , as most of the learning sources are based on java. One has to know how to build the app in java first or at least has to know what sort of components or activities need to use for it. I have tried my hands on macroid and will try to explain it. The ideal IDE for it is Intellij Idea or android studio(which is the same).

Android UIs are mainly based on xml files, all the view and viewgroup are mentioned on the xml file. However using macroid we can do these stuffs by avoiding xml upto and extent and coding directly in scala with a cleaner approach.

It uses Bricks, which defines the parts of the UI like layouts, buttons, Text etc. It gets denoted as follows w[Button], l[LinearLayout] etc. It uses tweaks to change the styles and behaviour of the widgets. It has something call tweaks, using what we change the behaviour of the widgets or layers. For examplehttp://

Here, l[LinearLayout] has defined the layout and within the layout we have a text box, so basically a widget for text view is created with w[TextView] and text is being put here using the tweak.

In this way it will be well composable for any complex view, with a cleaner and concise approach with macroid. For example

Here we are using different layout for the view as well as for each layer it has specific layer, and it is applied easily. (For the drawer layout click here). The DrawerLayout is the main layout here under which we have two more layouts, Linear Layout and Frame Layout. In the LinearLayout we have some text, and we have a FrameLayout inside, which gets the fragmentContentStyle applied. And on the LinearLayout the ContentStyle is applied. The other framelayout is got applied with drawerLayoutStyle and drawerStyle is applied to the main drawer layout.

DiggingMacroid

The example given here is just a portion of the complete app and we can see that doing it this way makes the code quite concise without playing with the xml files. In order to run the complete app we have to deal with some more stuffs like Activity, Contexts etc. In the next blog I will try to cover the remaining stuffs to build a complete app in Scala’s way and will explore the other advantages of it.

Written by 

Pranjut Gogoi is the enthusiast of Machine Learning and AI with 8+ years of experience. He is been implementing different machine learning projects in Knoldus. He started an initiative called MachineX through which they share knowledge with the world. With this initiative, he broadcasts different free webinars, write different blogs and contributes to open source communities on machine learning and AI.

2 thoughts on “Digging Macroid – The first sod (Scala on Android)4 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading