Android Model View Presenter MVP Tutorial

MVP Model View Presenter is the best and most suitable architectural patterns for android development. There have many architectural patterns and why we use Model View Presenter instead of Model View Controller(MVC).  It is because activity in android contains view and controller. The Activity class in android doesn’t extend View class but it is handle displaying a dialog and toast to the user. Besides, it also handle the events of windows such as onCreate,onPause and onResume. If you using MVC pattern, the controller will actually be a View-Controller because it is handling display window and activity life cycle event. Therefore, we should use MVP for android developer since the Views in this architectural pattern are serving as a entry point, rendering components and routing user events to the presenter. In this tutorial, I will teach you how to use Android Model View Presenter MVP in your project.

Model basically gateway to the domain layer or business logic and it will help making your domain layer more tight.

View contains your UI components and its almost zero logic.

Presenter will handle communication between your model and your view. It also handler events.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “MVPExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Edit activity_main.xml layout

After create the new project, go to edit the main activity layout to the source code below.

Create new interface for View

Right click the project name and create a new interface “MainView”. This interface provide all view method in the activity.

Create new interface for Presenter

After that also create a new interface “MainPresenter”. This interface handler all event in the mainactivity.

Edit MainActivity.java class

Go to this class and edit to the following sample source code. This activity implements MainView to have all method on it and pass event to the presenter to handler.

Create new interface for interactor(Model)

Besides, we also create a new interface “LoadItemInteractor”. What is interactor? its help you to fetch all data from web server or database. But in this tutorial we only fetch some dummy data.

Create a new class for Presenter

This class will be implements all method from MainPresenter interface.

Create a new class for Interactor

This class will be implements all method from LoadItemsInteractor and fetch all the dummy data.

Run Your Project

Finally, the mvp pattern is completed in your project, now you can run it and view on your emulator or devices.

(Android Model View Presenter MVP)

Source Code

(Visited 1,319 times, 1 visits today)
Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

2 Responses

  1. Fandi Akhmad says:

    Good for quick start to using MVP in Android

  2. AAA says:

    When i click on download!! i gets this msg =>“Your requested URL has been blocked as per the directions received from Department of Telecommunications, Government of India.”

Leave a Reply

Your email address will not be published. Required fields are marked *