4 Steps to MVVM in Android (Java)

The easiest guide to getting started with MVVM instantly.

Kashif Anwaar
2 min readOct 23, 2019

For Kotlin version check https://medium.com/@kashifo/4-steps-to-mvvm-in-android-kotlin-8f392426373

Hello Android Devz!

Two years ago during Google IO 2017, Google introduced MVVM (Model View View Model) Architecture for android app development, even though it’s trending nowadays but not every Android Developer knows it — one of the main reasons is that nobody has explained it in an easy way with a simple example. That’s what I’m trying to do here.

I am going to create a very simple and straight-forward app to demonstrate it, I am going to fetch U.S public holidays from a public API and display it in recycler view.

The 4 steps of MVVM

  1. Create Model (aka POJO)
  2. Create Repository (fetch data from API or DB)
  3. Create ViewModel (extend ViewModel, get liveData from Repository)
  4. Create Activity (observe ViewModel & display data)

Before starting, if you aren’t using AndroidX you need to add LiveData dependency in your app gradle.

implementation “android.arch.lifecycle:extensions:1.0.0”

Step1: Create Model (aka POJO)

Let’s create a Pojo class to store the value of each holiday.

Step 2: Create Repository

A repository is a class where we fetch data from API or DB.

In the repository getter method create a variable with MutableLiveData

final MutableLiveData<List<HolidayModel>> mutableLiveData = new MutableLiveData<>();

Fetch data from API and set the data into this variable.

mutableLiveData.setValue(response.body());

Refer to the example below.

In the example, I’m using Retrofit to fetch public holiday list

Step 3: Create ViewModel

ViewModel is just a class created by extending ViewModel to hold data, the official definition is

The ViewModel class is designed to hold and manage UI-related data in a life-cycle conscious way. This allows data to survive configuration changes such as screen rotations.

To create a ViewModel, extend a class with ViewModel, initialize the repository object in constructor, create LiveData variable, create a getter method to get value from the repository and set into LiveData.

ViewModel Example.

Now that we have created Model, Repository and ViewModel. let’s go the final step.

Step 4: Create Activity

In Activity create an object of ViewModel, call the getter method and observe the data using LiveData as shown below.

HolidayViewModel holidayViewModel = new HolidayViewModel();

holidayViewModel.getHolidays().observe(this, new Observer<List<HolidayModel>>() {
@Override
public void onChanged(List<HolidayModel> currencyPojos) {
adapter.addHolidayList(currencyPojos);
adapter.notifyDataSetChanged();
}
});

Whenever data is changed you’ll get a callback in the onChanged method. Inside the onChanged method I’m setting the ArrayList to my adapter.

To view the complete code, check the ‘java’ package in this project on Github.

This is my first article, your appreciation will motivate me to write more.

--

--

Kashif Anwaar

Technologist, Products & Startup Enthusiast, Passionate Software Developer active in Android development.