In 2016 (which seems like a whole eternity from now :), I wrote an Android app called Look Counter for counting how many times you have switched the screen on, as well as unlocked it. Up until recently I haven’t updated it. The app used such libs as greenDAO , ButterKnife , GoogleAnalytics, and RecyclerView. After the update, only the last one remained 🙂
I’ve decided to completely re-write Look Counter, using the modern tools. The first step was to update all of the dependencies and set project’s target API to 27. It’s a good idea to set yours to 26 as a min, because of Google Play’s new requirements — “Google Play will require that new apps target at least Android 8.0 (API level 26) from August 1, 2018, and that app updates target Android 8.0 from November 1, 2018.”
As the second step I chose to refactor layouts and use ConstraintLayout in order to flatten the hierarchy. It allowed me to make both Calendar and About screens completely flat, which I think is awesome 🙂
Next I wanted to convert Java to Kotlin. I used Android Studio’s Convert Java file to Kotlin file option, and then cleaned up the code. I got rid of !! and used vals instead of vars whenever possible. Also, objects instead of classes, extension methods, etc. In general, simply converting Java to Kotlin is easy, but to make Kotlin code look good and not like some Java-adaption, requires an extra effort.
Continue reading Look Counter app is now in Kotlin and stores data in Room DB
Android development can be (and usually is) much easier and satisfying with Kotlin, compared to Java. But it’s also quite different. Lots of things, like predefined nullability (or lack of it), no static as we know it, extension and top-level functions are there to be used in our favor. No wonder that it may be confusing what approach to choose, especially when the same thing can be done in different ways.
Today I’m gonna present you a better way of initializing and using SharedPreferences in your Kotlin app. No more repeating code with initialization in every place you want to get a preference, no more long lines to get or set a pref. How to accomplish this? Use object with lateinit and custom getters & setters.
Continue reading [Tutorial] How to use SharedPreferences in Kotlin
With Android Architecture Components getting a stable 1.0 version, there are now no excuses of not using it 🙂 Along with handling lifecycle events, realtime data updates in UI (ViewModel with LiveData) and pagination of loaded data (Paging), comes Room – small, yet powerful SQLite ORM. In this post I’m gonna demonstrate its core capabilities on an example Android application.
Remember those times implementing SQLiteOpenHelper and checking SQL queries in run-time? Good news is that you don’t have to do it anymore! Room performs compile-time checks on your SQL queries and you don’t have to write any SQLite code which is not in a direct relation with your data queries. Great, lets use it!
First of all, Room is a part of Architecture Components, which means it works really well with ViewModel, LiveData and Paging (but does not depend on them!). Also, RxJava and Kotlin are perfectly fine too. In order to add Room to the project, I’m adding the following lines in app’s build.gradle file:
Check for the latest library version here.
Additionally, provide a location of DB’s schema in defaultConfig scope. This way you can always check how it looks, maybe decide to modify your tables once you notice something in scheme.
arguments = ["room.schemaLocation":
Continue reading Database example app with Room ORM
Since the introduction of AppBar in 2015, Android developers have spent lots of time styling and modifying it, making beautiful and unique apps. The ways of modifying Toolbar and flexible area beneath it are quite impressive. Yet, still the entry threshold is quite high for those who want to make their first steps in Material design world.
Partly, this is because of an incomplete documentation and the lack of diverse examples. I, myself struggled to make the layout I wanted and as easy as it sounds – to make it scroll the way I want to. This was the moment I decided to write this blog post, so it helps others 🙂
Maybe you want to scroll a Toolbar, so it hides completely and the only thing visible is the text? Or expand and collapse an image below the Toolbar? Or, doesn’t matter if the user is on the bottom of the layout, – you want to show him a Toolbar immediately on a scroll up action (there is a description of various scrolling techniques here). All of this is possible and easy to do with scroll flags!
Continue reading AppBarLayout scroll behavior with layout_scrollFlags