Category Archives: Blog

Inserting multiple rows into Sqlite database with Content Providers

I have just found a draft of this post. I know this is a bit outdated but I think this might provide some value to someone still using Content Providers.

So, how can you insert multiple rows into the database at one using SQLite with ContentProvider?

Many separate inserts

Naive approach would be similar to this:

for (int i = 0; i < list.size(); i++) {
    Book book = list.get(i);
    ContentValues cv = getContentValues(book);
    cr.insert(CONTENT_URI, cv);
}

Continue reading

Adding backendless sync to existing offline app with Firebase

My application Reader‘s Diary is on Google Play store for more than 4 years. It was my first more complex application when I started self-learning programming. And you can guess that from some still remaining fragments of horrible code:) I wrote application primarily for myself, I use it whenever I start or finish reading a book. However, during these years it found few hundred active users. As my coding and UI skills improved, so has app, and now it is quite different from it‘s first version. Some poor initial design choices are however still there.

Planning new version

I was quite happy with the previous version. It fulfilled all my needs. Not taking small fix into account, it was in store for 18 months. In the meantime, I got some requests from users. Some wanted to sync their books, some wanted to add book cover pages. Showing cover images was an easy thing. I already have a function to search books by ISBN code using Google Books service so I only needed to save image url from the search result. For sync, I decided to use Firebase.
Continue reading

What is Application class good for?

The Application class is the base class of Android application. It is the first class that is initialized when the app is started. Therefore you can initialize here everything that needs to be ready when an activity or other application component is started.

When to override Application class?

Application class doesn’t have to be overridden all the time. I presume most of simple apps can use default Application class implementation without problems. Android documentation states that

There is normally no need to subclass Application. In most situations, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), include Context.getApplicationContext() as a Context argument when invoking your singleton’s getInstance() method.

Main reasons to override Application class are following:
• Getting access to the Application Context
• Global initialization
• Getting access to global methods and data
Continue reading

Java threads, Android Async Task and Kotlin Coroutines

Imagine that you need to do some long time operation and update UI with its result. You probably know that you should not call things that might take some time on UI thread. If you do so, an application might become unresponsive and that would lead to poor user experience or system stepping in and offering to kill an application with infamous Application is not responding dialog.
So how can you avoid this? There are many ways. Most commonly used are threads, AsyncTasks and now also Kotlin coroutines. In this article, I will make a quick introduction and provide reference links for more details.
Continue reading

Preventing Multiple Click

There are situations where clicking on view triggers some action that takes more time. For example, saving data to the database or updating data from the network. In this case, it is good (and in case of network calls mandatory) to perform these operations on background thread.
But how to prevent user to click multiple times and trigger multiple operations? There are some options to consider.

Show dialog

You can show an uncancelable dialog that will prevent unwanted clicks until it is dismissed. For example show this progress dialog with spinning wheel and hide it when the operation finishes.
Continue reading

Default Interface Methods in Java 8 and Kotlin

Before Java 8, interfaces couldn’t implement any code. With Java 8, interfaces can include default methods implementations and also static methods.

Default Methods

Default method is method implemented in interface marked by the modifier default. These methods can be called from all classes implementing the interface. What is it good for? Let’s imagine a game with a lot of heroes that have can do some actions. E.g. going left and right. After while add a new action, for example jumping. Now we have to implement this action in all methods. And maybe this action is same for all heroes or we don’t need this action to be used by all heroes. The implementation of second option could look like this:
Continue reading

Debugging your Android SQLite database with Stetho

Have you ever wondered why your Android app doesn‘t behave as expected? Why doesn’t it show data that you expect or have you ever even wondered if there are really any data at all? The situation is even more complicated if you use Content Providers for your database. Discussion about benefits and disadvantages of Content Providers is outside scope of this post but you will probably agree that it is more complicated to write queries using Content Providers than by raw SQL.
There are more ways around this.
Continue reading

Kotlin Higher-Order Functions

A higher order function is a function that takes a function as a parameter or it returns a function. It allows us to write shorter and more beautiful code. Let‘s look at some examples.

fun <T> tryAndCatch(block: () -> T) = try {
    block()
} catch (e: Exception) {
    println(e.message)
}

This function will run code passed inside block variable inside try catch block and return result and also print exception in case it is thrown. What does it all mean?
Continue reading