Android MVVM Tutorial – Build an App using MVVM Design Pattern

A badly written code is a NIGHTMARE (when needed to maintain). For any developer there is nothing better than having a clean, structured code. And if you want to achieve this, you must follow a design pattern for your projects. So this Android MVVM Tutorial is for all the people out there who learned the basics of android development and now want to learn something advanced. Because if you are seeking a JOB then knowing only the basics are not enough. You should know some design patterns.

What is MVVM?

MVVM stands for Model View ViewModel and it is a design pattern that is used to build softwares. But here our focus is only about Android App Development. MVVM helps us to separate the Business Logic of our Application from the Views or UI. And doing this thing is very important in case of any kind of software or app development.

Model : This is responsible for handling the data in the application. Model cannot directly interact with Views, but it interacts with ViewModels and then Views with the help of observables. (Sounds confusing right? Don’t worry in the course we will cover every step in detail).

View: This is the User Interface of our Application. It should not contain any application logic.

ViewModel: It is basically a link between Model and View.

android mvvm tutorial
Android MVVM Tutorial

MVVM in Android

When we want to use MVVM in Android, we use this architecture.

android mvvm architecture
Android MVVM Architecture

One more thing is involved here and that is the Repository. So repository is responsible for handling the data. Repository may use multiple data sources according to the need, it may fetch data from a Remote Source or from the local storage.

Advantages of using MVVM

Now let’s talk about what are the advantages of using MVVM in your android project.

  • If you use MVVM it will help you structuring your code in a nice way so that it is easy to understand for a new developer.
  • Using MVVM makes your project maintainable as everything is well organized and making changes are very easy.
  • Testability is easy with MVVM because all modules are independent and testable.
  • MVVM enhances the re-usability of the code.

Pre-Requisites of this Course

This course contains many advanced topic like creating repositories, dependency injection, data binding etc. And that is why this course is not for any beginner who just started learning android development. But if you sure you know the basics very well then you can give it a start.

Android MVVM Tutorial

Now, if you think you should learn about MVVM in android then, “What are you waiting for?”. Start now it is ABSOLUTELY FREE for everyone.

If you think this course is helpful and my efforts of creating this course worth something then I have a request that please SUBSCRIBE and tell your friends to SUBSCRIBE to my channel as well. 

Android MVVM Tutorial – Source Code

You can check my source code while following the course. For every lecture I have a separate commit and you can browse the code for that commit only and it will make your learning better.

Android MVVM Tutorial Source Code Download

In case you are having any problem in this course, you can contact with me by commenting here or by messaging me on my social profiles. Thank You.

33 thoughts on “Android MVVM Tutorial – Build an App using MVVM Design Pattern”

  1. Hi sir could you provide me source code as well. That will be very helpful
    “I get an error ” – Could not resolve enqueue – MyApi().userLogin(email,password).enqueue()

    Reply
  2. I’m learning mvvm pattern now by watching your tutorial on YouTube but I’m having challenges with your baseUrl…I would like to know if that api is still up and running/valid

    Reply
  3. Hello bro, I trust you are doing ok.
    Thanks for your video on using mvvm architecture.
    It was very helpful.

    I’m currently using it as a template on one of my projects.

    Please I have a little issue when using the Authviewmodel and repository in getting access to a view.
    I’m using the country picker library, and I would like to access the country code directly from the Authviewmodel class so that I can pass the code to the the UserRepository to make an API call.

    I’ve been on this for 2 whole days, I think if I can get access to any view from the viewmodel I can instantiate the country code picker class and proceed.
    Please I would really appreciate any help.

    Thanks so much

    Reply
  4. You have handled a success response but you did not handle error properly. what if an exception occurs here

    private suspend fun fetchQuotes() {
    val lastSavedAt = prefs.getLastSavedAt()

    if (lastSavedAt == null || isFetchNeeded(LocalDateTime.parse(lastSavedAt))) {
    try {
    val response = apiRequest { api.getQuotes() }
    quotes.postValue(response.quotes)
    } catch (e: Exception) {
    e.printStackTrace()
    }
    }
    }

    How will you convey to through the view?

    Reply
  5. Hi, your course is really very helpful, but even after subscribing to your channel, I am not able to download source code.
    Please help me with your source code.

    Reply
    • Click the download link,
      You will see the download page where link is locked, now follow these steps
      #1 Click on Subscribe Button, it will open a new tab, in this tab subscribe if you are not already subscribed. If you are already subscribed ignore this tab and go back to the download page. Make sure you don’t open the download page again and go back to the already opened page.
      #2 Wait for few seconds and the download link will unlock.

      Reply
  6. abstract class SafeApiRequest {

    suspend fun apiRequest(call:suspend()->Response):T{
    var response=call.invoke()
    if(response.code()==401||response.code()==422)
    {
    var error=response.errorBody().toString()
    var message=StringBuilder()
    error.let {
    try {
    message.append(JSONObject(it).getString(“message”))

    }catch (e:JSONException){}

    }
    message.append(“\n”)
    throw ApiInspection(message.toString())

    }else{
    return response.body()!!
    }
    }
    }

    class ApiInspection(message:String):IOException(message) {
    }

    Reply
    • Click the download link,
      You will see the download page where link is locked, now follow these steps
      #1 Click on Subscribe Button, it will open a new tab, in this tab subscribe if you are not already subscribed. If you are already subscribed ignore this tab and go back to the download page. Make sure you don’t open the download page again and go back to the already opened page.
      #2 Wait for few seconds and the download link will unlock.

      Reply
    • Click the download link,
      You will see the download page where link is locked, now follow these steps
      #1 Click on Subscribe Button, it will open a new tab, in this tab subscribe if you are not already subscribed. If you are already subscribed ignore this tab and go back to the download page. Make sure you don’t open the download page again and go back to the already opened page.
      #2 Wait for few seconds and the download link will unlock.

      Reply
  7. The source code is not accessing even though I subscribed your channel. Please help to download the source code of MVVM kotlin.

    Reply
  8. I have profile API view and update called from different screen.
    1.First view API called from one screen
    2.updateAPI called another screen.
    3.And then to update exist view ” view API “was called .it was solution
    4.Any better solution available

    Reply
  9. I am trying to use room database with kotlin in my project and my response is something like this in below so i don’t know how the do this
    [
    {
    “image”: “/images/8d079d68-91b4-41b9-9a9b-24b1366abab0_spweb.png”,
    “url”: “/BP”
    },
    {
    “image”: “/images/23741d5d-f88b-4815-baca-5d3f60195c07_flagship-hul-star_cleaning_essential-web-july2020-sbi.jpg”,
    “url”: “/Cleaning”
    },
    {
    “image”: “/images/87c70780-9d02-4134-8296-4a830debdd5b_msweb.png”,
    “url”: “/MONSSON”
    },
    {
    “image”: “/images/ba2167ab-024f-4094-8a09-2b04db093d37_flagship-tataq-1400×400-web-03072020.jpg”,
    “url”: “/TATAQ”
    }
    ]

    Reply
  10. Hi sir,

    I have gone through this tutorial regarding Kotlin, a few confusions are going to happen while implementing this MVVM source code

    Reply

Leave a Comment