Android Stuff

Dagger 2 – Rules of Engagement

Nov 11, 2017

For the love of logic, stop reading some Dagger tutorial, copy-pasting some code, and then expecting magic to happen. Dagger does not magically set your variables and remove nulls from your code. You have to understand how Dagger works before using it, or you will end up creating more work than you tried saving.

This said, Dagger is not as complicated as it may seem. It follows a few basic rules, so by knowing those rules, you know Dagger.

Read More

How to Add a Splash Screen—The Right Way

Sep 2, 2017

There are a lot of different approaches on how to add a splash screen to your app. Whatever your approach, it will be fine unless you use a dedicated SplashActivity. Using an activity as a splash screen is just wrong, and it will mess up your navigation sooner or later. After all, a splash screen should be shown while the app is loading and it should not depend on where a user comes from or what started the app. In the following, I want to show an easy setup that needs only minimal changes to your existing code base.

Read More

Paging your RecyclerView

Jun 24, 2017

Everyone knows how to display multiple pages using a ViewPager, but since support library version 24.2.0 came out this is no longer the only way. With SnapHelper you can easily add a pager-like feel to your RecyclerView and maybe even make your life easier in the process. This post is about how to setup your RecyclerView along with those page indicators that everyone loves. If you read some of my blog, you might already know what’s coming next:

More about ItemDecorations! :D

ViewPagerDecoration

Read More

Providing Interface Implementations with Dagger

Apr 28, 2017

A lot of guides cover the basic setup of Dagger and how to use it in your projects, but how would you provide different implementations of an interface with Dagger? In the following I want to look at a real world example: A Car, that either could take a PetrolEngine or an ElectricEngine!

Now that we have a goal in mind all we need to figure out is how to do this with Dagger.

Read More

Keeping State with RxJava using Loaders

Mar 17, 2017

Keeping state on Android is always a little bit tricky and confusing. You either have to use some sort of singleton, store data in your Application object, or make use of the good old onSaveInstanceState. There is also always the option to use a service.

Saving instance state can’t persist running operations like loading data. Using singletons or the application object to store data makes all of this possible, but global state is also something you should try to avoid unless really necessary. I will not go into detail about whether or not and how to use services, since this is a whole topic by itself.

I tend to use RxJava with Retrofit and a while ago I was wondering whether there was another way to keep state over configuration changes. Loaders came to my mind, which I do believe are well underused lately. And Loaders are supposed to handle long running tasks and keeping data, right?

Read More

Displaying Password Strength

Jan 14, 2017

While I personally wish there was OAuth everywhere, most apps still require a password somewhere. So while building this sign up page you might just want to give your user a hint about the strength of their chosen password, whether you validate it or not.

New constant variable

Read More

Testing your HTTP requests offline

Nov 22, 2016

Most apps have to talk with servers in some ways and testing is not always easy. There are multiple steps involved to set up the test data, and sometimes a specific request can only be triggered once. Whatever the reason, often there is some work involved to test even the most basic things. So why not just cut out the server and provide your own data?

You might even get something like a demo or offline mode in the process.

Read More

Working with multiple flavors and build variants

Nov 9, 2016

Flavors enable you to have multiple similar versions of your app within a single code base, like different colors, or the common example of a paid and a free version of the same app. They greatly increase maintainability, as well as publishing updates to multiple apps gets significantly easier. But while having different settings for every flavor is simple enough, things start to get really tricky if you introduce flavor dimensions and find yourself dependent on combinations of different flavors and build types.

Let’s assume I have a news and a blog app. Both could be some sort of RSS reader and they share the same code base. I also have a development and a production environment, because I don’t always test my code, but when I do, I test it in development! :)

The basic setup is simple and I define my flavor dimensions as well as my flavors. Because I actually have two different apps, each one has it’s own unique applicationId.

flavorDimensions "app", "environment"
productFlavors {
  news {
    dimension "app"
    applicationId "com.example.news"
  }
  blog {
    dimension "app"
    applicationId "com.example.blog"
  }
  development {
    dimension "environment"
  }
  production {
    dimension "environment"
  }
}

Now that things are set up I want to go ahead and add the URLs to my server and this is where things get complicated.

Since I have two apps each app has its own URLs, one for development, and one for production.

Read More