Android MVP Demo(Kotlin) — Part 1

Vishal Pandey
3 min readOct 9, 2019

--

Simple weather app with MVP pattern in Kotlin.

Hello Reader,

If you are here that means you already have an idea about Android App Development and looking to upgrade yourself. I will recommend to brush up MVP pattern before getting started.
This article is divided in multiple parts, here in part 1 i will cover the basic MVP implementation then in part 2(coming soon) will describe the unit and instrumentation test cases using JUnit and Espresso respectively.

Overview

I have created a simple application which will list weather forecast of next seven days for searched city.
There are multiple ways to implement MVP in android, you can have your own way to implement it. Here i’ll list the simple steps while implementing your MVP pattern.

Model: It is responsible for providing the data required by our application, that data can be from different sources like local(SQLite, A File, Shared Preferences) or remote(REST/Graphql API, Google Firebase, IBM Cloudant).

View: Any visible screen to the user is View(Activities/Fragments/Custom Views). No logic should be written in view so that we can write our instrumentation test case(Using Espresso) without any java dependability.

Presenter: Acts as a middle man between view and model, responsible for getting the data from the model and control the view .
Note- There should not be any classes from android.* package in presenter, so that we can write our unit test cases of our presenter(Using JUnit, Mockito and Power Mockito).

MVP Workflow

Steps for implementation of MVP

1. Create a contract interface for each View, and create three inner interface for Model, View and Presenter.
2. Define all required methods in above inner interfaces.

WeatherContract.javainterface WeatherContract {
interface View {
fun onInitView()
}
interface Presenter {
fun init()
}
interface Model {
fun fetchInvalidCityMessage(): String?
}
}

3. Create Impl classes for Model and Presenter and Contract.View will be implemented by our View class(Activity/Fragment).

4. Initially view will invoke presenter’s initView() method, which will return the callback to View’s onInitView(). Here we will bind all view’s id.

If you have understood above steps well then you are not far away from implementing or converting you code to MVP pattern. I have added the github link for full source code, just go through that and let me know if you face any issues.
One clap is much appreciated, Happy Coding!!!

--

--

Vishal Pandey

JAVA | Kotlin | Android | React | Spring Boot| Graphql