Skip to main content

Posts

Showing posts from December, 2019

Fragments ❤ ViewPager2

The ViewPager2 is a pretty nice rework of the ViewPager API. Some new features you may find with the ViewPager2 are:

1- Vertical scrolling. You can simply enable it by adding: android:orientation="vertical" in the <ViewPager2> tag in your xml file.
2- Right to left support: you can set the android:layoutDirection="rtl" to enable this.
3- Support for DiffUtil, because it is based on RecyclerView.
4 - Fragments improved support, which we will talk about below.

So, let's suppose that we have 2 Fragments: HomeFragment and SchoolFragment. In the Activity/Fragment xml file, which is going to hold these two sliding by switching with each-other, place the ViewPager2 tag:

<androidx.viewpager2.widget.ViewPager2 xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_view_pager" android:layout_width="match_parent" android:layout_height="match_parent" >" Now, let's set up co…

Setting up Gradle with Kotlin DSL, a simple guide

Kotlin is a very a pretty nice adoptive language and user friendly. It really replaced Java from my everyday programming. However, it was not enough. We all know that groovy runs on JVM. So, why do I even need a new language just for my builds? Can't it be Java? So Java is the basic language for the JVM, Kotlin runs on JVM, Groovy runs on JVM and my build system has a separated language from my business logic system. Why?

I was introduced to Gradle with Kotlin accidentally. I never heard of Kotlin DSL in terms of Gradle. I just created a new Spring project and the built file looked kind of strange. After a little Google-ing, everything was clear. Long story short, I removed groovy from my Gradle build tool in my Android project, and replaced it with Kotlin. It shouldn't take more than 15 minutes to do this, but you can struggle with some things in particular.

Note: Kotlin DSL means Kotlin Domain Specific Language. It's just a notation, the name is self descriptive.

 Here a…

Conditional navigation and single event LiveData

Conditional navigation is a little tricky when it comes to Navigation Architecture. There are plenty of nice articles and solutions about it, but I'm sharing the way I solve this important problem. So let's consider this use case:


If you don't put HomeFragment as a start destination, you will have some serious trouble with the fragment back stack and you would need to write the onBackPressDispatcher in every fragment. Therefore, don't do it.

There is also an easier step to do this directly in the OnlyActivity you have, and it's pretty nice too, but I've noticed that Google suggest the activities to be kept just for holding <fragment>s and nothing more. Otherwise it would violate the responsibility that the new Architecture Activity behaviour has (like checking from its' ViewModel user authentication etc).

So it's just fragments. A common problem you might face is when you use LiveData<T> the usual way for this case. I'm not handling the…

Are service locators in Android actually simple?

This week, I've been playing with service locators in Android. I made this repo with 3 different branches. One of them is using Dagger as a DI tool, the other branches are implementation with KodeIn and Koin.

The app is pretty simple, just retrieves some data from a Cat API, saves them to a local Room persistence and then renders them to the screen:


The screen renders just a the list of cats retrieved by the database after the data are fetched from a remote API. So let us list our dependencies:

1 - First I'm using Retrofit, to fetch the data (and the interface which implements the server request methods)
2- Second I need a database (I always use Room, but in my early beginning of Android development, I remember using Realm and it was awesome too)
3- Since I'm using coroutines, I always keep a data class with coroutine dispatchers to be used in the ViewModel.
4- The famous ViewModelFactory (for simplicity I'm not handling the savedStateHandle in this article)
5-  Picas…