Database example app with Room ORM

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:

    // Room
    implementation "android.arch.persistence.room:runtime:1.0.0"
    annotationProcessor "android.arch.persistence.room:compiler:1.0.0"

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.

   javaCompileOptions {
        annotationProcessorOptions {
            arguments = ["room.schemaLocation":
                                 "$projectDir/schemas".toString()]
        }
    }

Continue reading Database example app with Room ORM

Like and share:

AppBarLayout scroll behavior with layout_scrollFlags

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

Like and share:

Best resources to learn RxJava

Hi,

Some time ago I got interested in reactive programming and RxJava in particular. It’s gaining more popular in Android world and I often see RxJava as an obligatory skill for a Senior or Mid Android Developer position, that’s why learning it can be of a great benefit if you’re seeking a job ūüėČ But even if not, it can simplify your app development, especially if you have lots of UI fields which you fill in with some changing data or if you want to have an interactive search, or while using Retrofit for you HTTP requests. There are even smaller libs for a particular task, like RxWear, RxNotifications or RxAndroidBle (find a¬†full list of such libs here). Choosing the right one will depend on your needs ūüėČ

And so today I just want to share a few resources which I found very useful while¬†learning how RxJava and RxAndroid libraries work (and what are they at all :). I have basically reviewed what’s available on ReactiveX Tutorials list¬†(which is huge!)¬†and listed the most intuitive and easy to get below, as well as a few articles and tutorials I found on the Internet myself.

A¬†separate and an absolutely must position¬†is a¬†RxJava for Android App Development free e-book. It’s very short (only 41 pages!), and it’s a piece of cake to read, believe me, because I read it before any other docs or tutorials, and it explained the most important principles just fine ūüôā

Continue reading Best resources to learn RxJava

Like and share:

What is it about Context in Android?

Hi,

Today I want to talk about Context in Android.¬†As you know, whether we’re attaching a view or get an asset, – almost always there is a reference to Context. From the¬†documentation, Context is an interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

On the one hand, we all know it and use it, on the other – we often use it in a wrong way or just don’t care¬†enough whether garbage collector will clean it or will the context be hanging there somewhere or, even worse, cause a memory leakage. So,¬†what are the ways of getting a context? A couple actually:

  • this / getActivity()
  • getContext()
  • getBaseContext()
  • getApplicationContext()

Continue reading What is it about Context in Android?

Like and share: