(English) Preventing Multiple Click

Není k dispozici v češtině. For the sake of viewer convenience, the content is shown below in the alternative language. You may click the link to switch the active language.

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

(English) Default Interface Methods in Java 8 and Kotlin

Není k dispozici v češtině. For the sake of viewer convenience, the content is shown below in the alternative language. You may click the link to switch the active language.

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

(English) Debugging your Android SQLite database with Stetho

Není k dispozici v češtině. For the sake of viewer convenience, the content is shown below in the alternative language. You may click the link to switch the active language.

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

(English) Kotlin Higher-Order Functions

Není k dispozici v češtině. For the sake of viewer convenience, the content is shown below in the alternative language. You may click the link to switch the active language.

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

(English) Create your own File Templates in Android Studio

Není k dispozici v češtině. For the sake of viewer convenience, the content is shown below in the alternative language. You may click the link to switch the active language.

You already know that you can make templates for code snippets in Android Studio. You can do same thing for whole files as well. And you probably use file templates even if you didn‘t yet created your own.
Everytime you create new file using New menu, you are creating files based on file templates.


Continue reading

(English) Kotlin Data Classes

Není k dispozici v češtině. For the sake of viewer convenience, the content is shown below in the alternative language. You may click the link to switch the active language.

The main purpose of many classes is to hold data. But to effectively manipulative with data, we need to override quite a lot data. Class holding 3 String values would typically look something like this in Java:
Continue reading

(English) How to check object equality in Kotlin?

Není k dispozici v češtině. For the sake of viewer convenience, the content is shown below in the alternative language. You may click the link to switch the active language.

Unlike Java, Kotlin is purely object-oriented language. There are no primitives, everything is an object. With this said, you might be surprised by results.

Let’s compare two objects with equality operator:

object1 == object2

You expected to get false, right? But answer is true. Why? This code is translated to

a?.equals(b) ?: (b === null)

Continue reading

(English) How to check object equality in Java?

Není k dispozici v češtině. For the sake of viewer convenience, the content is shown below in the alternative language. You may click the link to switch the active language.

Java is not purely object-oriented programming language. We can use both objects and primitives for our variables. We can check easily if two primitives are same by identity operator ==. It is not as easy for objects.

==

Comparing objects with == is one of the frequented beginner’s mistakes. A beginner will check if two Strings with value “apple” are same and the answer is false. Because it doesn’t really check if objects are same but whether they point to the same memory address. How can you then check real equality of objects? Equals is the method you have to use.
Continue reading

(English) Android Studio Live Templates

Není k dispozici v češtině. For the sake of viewer convenience, the content is shown below in the alternative language. You may click the link to switch the active language.

One of best programming practices is DRY. Don’t repeat yourself. And yet we often write similar blocks of code over and over again. And sometimes it really cannot be avoided. You can save yourself lot of time when you meet Android live and file templates. In this post, I write about live templates but I am sure I will come back to file templates later.
So how can you use live templates? Simply type template abbreviation, press Tab and block of code defined in template comes up. There are many live templates already included in Android Studio, but you can also define your own. To see them go to Editor, Settings and Live Templates. There they are, by default sorted by a programming language of their use. Looking like this.
Continue reading

(English) Kotlin Constructors

Není k dispozici v češtině. For the sake of viewer convenience, the content is shown below in the alternative language. You may click the link to switch the active language.

Kotlin helps you make your code shorter and more readable in many areas. One of them is constructors. In Java you have to tediously assign all variables passed to the constructor in similar style

this.something = something 

If more constructors are required, you have to overload them and repeat yourself over and over. Like in the following example.
Continue reading