Open In App

MVVM (Model View ViewModel) Architecture Pattern in Android

Last Updated : 18 Oct, 2022
Like Article

Developers always prefer clean and structured code for projects. Organizing the codes according to a design pattern helps in the maintenance of the software. By having knowledge of all crucial logic parts of the android application, it is easier to add and remove app features. Further, design patterns also assure that all the codes get covered in Unit Testing without the interference of other classes. 

Model — View — ViewModel (MVVM) is the industry-recognized software architecture pattern that overcomes all drawbacks of MVP and MVC design patterns. MVVM suggests separating the data presentation logic(Views or UI) from the core business logic part of the application. 

The separate code layers of MVVM are:

  • Model: This layer is responsible for the abstraction of the data sources. Model and ViewModel work together to get and save the data.
  • View: The purpose of this layer is to inform the ViewModel about the user’s action. This layer observes the ViewModel and does not contain any kind of application logic.
  • ViewModel: It exposes those data streams which are relevant to the View. Moreover, it serves as a link between the Model and the View.

MVVM (Model View ViewModel) Architecture Pattern in Android

MVVM pattern has some similarities with the MVP(Model — View — Presenter) design pattern as the Presenter role is played by ViewModel. However, the drawbacks of the MVP pattern has been solved by MVVM in the following ways:

  1. ViewModel does not hold any kind of reference to the View.
  2. Many to-1 relationships exist between View and ViewModel.
  3. No triggering methods to update the View.

Ways to Implement MVVM in the Project

There are 2 ways to implement MVVM design pattern in Android projects:

  1. Using the DataBinding library released by Google
  2. Using any tool like RxJava for DataBinding.

Data Binding:

Google releases the Data Binding Library for Android that allows the developers to bind UI components in the XML layouts with the application’s data repositories. This helps in minimizing the code of core application logic that binds with View. Further, Two – way Data Binding is done for binding the objects to the XML layouts so that object and the layout both can send data to each other. This point can be visualized by the example of this tutorial.

Syntax for the two way data binding is @={variable}

Example of MVVM Architecture Pattern

Here is an example of a single activity User-Login android application to show the implementation of the MVVM architecture pattern on projects. The application will ask the user to input the Email ID and password. Based on the inputs received the ViewModel notifies the View what to show as a toast message. The ViewModel will not have a reference to the View

To enable DataBinding in the android application, following codes needs to be added in the app’s build.gradle(build.gradle (:app)) file:

Enable DataBinding:

android {

   dataBinding {

       enabled = true



Add lifecycle dependency:

implementation ‘android.arch.lifecycle:extensions:1.1.1’


Below is the complete step-by-step implementation of the User-Login android application with MVVM pattern.

Step by Step Implementation

Note: Following steps are performed on Android Studio version 4.0

Step 1: Create a new project

  • Click on File, then New => New Project.
  • Choose Empty activity
  • Select language as Java/Kotlin
  • Select the minimum SDK as per your need.

Step 2: Modify String.xml file

All the strings which are used in the activity are listed in this file.


    <string name="app_name">GfG | MVVM Architecture</string>
    <string name="heading">MVVM Architecture Pattern</string>
    <string name="email_hint">Enter your Email ID</string>
    <string name="password_hint">Enter your password</string>
    <string name="button_text">Login</string>


Step 3: Creating the Model class


Create a new class named Model to which will hold the Email ID and password entered by the user. Below is the code to implement the proper Model class.



import androidx.annotation.Nullable;
public class Model {
    String email,password;
    // constructor to initialize
    // the variables
    public Model(String email, String password){ = email;
        this.password = password;
    // getter and setter methods
    // for email variable
    public String getEmail() {
        return email;
    public void setEmail(@Nullable String email) { = email;
    // getter and setter methods
    // for password variable
    public String getPassword() {
        return password;
    public void setPassword(@Nullable String password) {
        this.password = password;


Step 4: Working with the activity_main.xml file


Open the activity_main.xml file and add 2 EditText to get inputs for Email and Password. One Login Button is also required to validate the user’s input and display appropriate Toast message. Below is the code for designing a proper activity layout.


Note: For the proper functioning of Data Binding Library, it is required to set the layout tag at the top. The constraint layout tag of XML will not work in this case.



<?xml version="1.0" encoding="utf-8"?>
    <!-- binding object of ViewModel to the XML layout -->
            type="com.example.mvvmarchitecture.AppViewModel" />
    <!-- Provided Linear layout for the activity components -->
        <!-- TextView for the heading of the activity -->
            android:textStyle="bold" />
        <!-- EditText field for the Email -->
            android:text="@={viewModel.userEmail}" />
        <!-- EditText field for the password -->
            android:text="@={viewModel.userPassword}" />
        <!-- Login Button of the activity -->
            android:onClick="@{()-> viewModel.onButtonClicked()}"
            bind:toastMessage="@{viewModel.toastMessage}" />
            app:srcCompat="@drawable/banner" />


Step 5: Creating the ViewModel class


This class will contain all the methods which are needed to be called in the application layout. The ViewModel class will extend BaseObservable because it converts the data into streams and notifies the View when the toast message property will change.



import android.text.TextUtils;
import android.util.Patterns;
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
public class AppViewModel extends BaseObservable {
    // creating object of Model class
    private Model model;
    // string variables for
    // toast messages
    private String successMessage = "Login successful";
    private String errorMessage = "Email or Password is not valid";
    // string variable for
    // toast message
    private String toastMessage = null;
    // getter and setter methods
    // for toast message
    public String getToastMessage() {
        return toastMessage;
    private void setToastMessage(String toastMessage) {
        this.toastMessage = toastMessage;
    // getter and setter methods
    // for email variable
    public String getUserEmail() {
        return model.getEmail();
    public void setUserEmail(String email) {
    // getter and setter methods
    // for password variable
    public String getUserPassword() {
        return model.getPassword();
    public void setUserPassword(String password) {
    // constructor of ViewModel class
    public AppViewModel() {
        // instantiating object of
        // model class
        model = new Model("","");
    // actions to be performed
    // when user clicks
    // the LOGIN button
    public void onButtonClicked() {
        if (isValid())
    // method to keep a check
    // that variable fields must
    // not be kept empty by user
    public boolean isValid() {
        return !TextUtils.isEmpty(getUserEmail()) && Patterns.EMAIL_ADDRESS.matcher(getUserEmail()).matches()
                && getUserPassword().length() > 5;


Step 6: Define functionalities of View in the MainActivity file


The View class is responsible for updating the UI of the application. According to the changes in the toast message provided by ViewModel, the Binding Adapter would trigger the View layer. The setter of Toast message will notify the observer(View) about the changes in data. After that, View will take appropriate actions.



import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.databinding.BindingAdapter;
import androidx.databinding.DataBindingUtil;
import com.example.mvvmarchitecture.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState)
        // ViewModel updates the Model
        // after observing changes in the View
        // model will also update the view
        // via the ViewModel
        ActivityMainBinding activityMainBinding
            = DataBindingUtil.setContentView(
                this, R.layout.activity_main);
            new AppViewModel());
// any change in toastMessage attribute
// defined on the Button with bind prefix
// invokes this method
@BindingAdapter({ "toastMessage" })
public static void runMe(View view, String message)
  if (message != null)
    .makeText(view.getContext(), message,



Advantages of MVVM Architecture

  • Enhance the reusability of code.
  • All modules are independent which improves the testability of each layer.
  • Makes project files maintainable and easy to make changes.

Disadvantages of MVVM Architecture

  • This design pattern is not ideal for small projects.
  • If the data binding logic is too complex, the application debug will be a little harder.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads