Yesterday I built an android app in 2 hours with kotlin
without even knowing how to write hello world
in kotlin
before starting to work on it.
Granted, the app is really simple and I've written some demo app at around 2014 when I'm still at school.
The point of this article is not to showing off or anything like that, the point is to share the methods to get things done quickly
Background
My father is a teacher. He always wanted an app on his phone that can do the followings:
- Store the information of his students (name, score, student id number, etc)
- Standard CRUD operation on the student information.
- Customized theme and accent color.
- Easily add the score of one of his students by 1.
- Sort the students by score or by id.
At first glance, these functions seem to be not that hard. However, my time is valuable and I do not want to spend too much time on this project (for obvious reasons).
Analyze the core
Given the task, I need to find the core part of the application. The essential parts of the system are:
- A database, to store
- A list view, to show all entities.
- A detail view, to inspect, edit or delete a specific entity.
- Three events, to link the action of user input to
- Jump from list view to detail view
- Switch between two methods of sorting
- Add the score of entity by 1
Now we have the core function of this app, we need to figure out how to implement them in android, after some quick google search and google documentations. I now know:
- Database -> room database
- List view -> recycle view
- Detail view -> just an activity
- Three events -> event listeners
- Kotlin is the preferred language for android development
The power of boilerplate
After knowing the core tech I need, I now search for android kotlin room crud recyclerview boilerplate
on google. I was very fortunate to find a boilerplate that fits my exact need on github. The tech you might need is different, but the general approach is the same.
With the boilerplate, I now need to identify the changes I need to make:
- Customize the name, theme and accent color the app.
- Change the data model to be a
student
- Change all related code to make sure CRUD operations work perfectly.
- Add event listeners like the one in the boilerplate for the actions I need.
Knowledge transfer
When making the changes, I encountered two key problems:
- There's no built-in double click event, I need to somehow make it myself
- Database operation cannot reside in main thread, it must be working with something called
suspend
functions in kotlin.
I developed a lot of javascript so I applied a few techniques I learned in the javascript world and managed to solve the problems like:
- Use a boolean to mimic double click, basically there's a boolean and a timer. The first click reverse the boolean and start the timer, if the second click occured within the timer time frame, it's counted as a double click. After timer expires, the boolean go back to its original value.
-
suspend
is basicallyasync
, in javascript we solve this kind of problem withiife
:Immediately invoked function expression
, maybe there's something likesuspend lambda
that was invoked immediately in kotlin as well.
By applying what I've learned developing using javascript, I managed to solve my problems and finished the project within 2 hours, I installed it on my dad's phone and he's super happy.
Key takeaway
When time is limited and money is short, the best way to maximize your output is to leverage all the help you can get. Open source is a gift for all developers, there's no shame in using it. All you need to do is see the core of the application and apply knowledges of different domain.
Top comments (0)