MVP (Model View Presenter) Architecture Pattern in Android with Example (original) (raw)

Last Updated : 23 Jul, 2025

In the initial stages of Android development, learners do write codes in such a manner that eventually creates a **MainActivity class which contains all the implementation logic(real-world business logic) of the application. This approach of app development leads to Android activity gets closely coupled to both UI and the application data processing mechanism. Further, it causes difficulties in the maintenance and scaling of such mobile applications. To avoid such problems in maintainability, readability, scalability, and refactoring of applications, developers prefer to define well-separated layers of code. By applying software architecture patterns, one can organize the code of the application to separate the concerns. **MVP (Model - View - Presenter) architecture is one of the most popular architecture patterns and is valid in organizing the project.

**MVP (Model - View - Presenter) comes into the picture as an alternative to the traditional **MVC (Model - View - Controller) architecture pattern. Using MVC as the software architecture, developers end up with the following difficulties:

**MVP pattern overcomes these challenges of **MVC and provides an easy way to structure the project codes. The reason why **MVP is widely accepted is that it provides modularity, testability, and a more clean and maintainable codebase. It is composed of the following three components:

Android-Architecture-Pattern-2

Key Points of MVP Architecture

  1. Communication between View-Presenter and Presenter-Model happens via an **interface(also called Contract).
  2. One Presenter class manages one View at a time i.e., there is a one-to-one relationship between Presenter and View.
  3. Model and View class doesn't have knowledge about each other's existence.

Example of MVP Architecture

To show the implementation of the MVP architecture pattern on projects, here is an example of a single activity android application. The application will display some strings on the **View(Activity) by doing a random selection from the **Model. The role of the **Presenter class is to keep the business logic of the application away from the activity. Below is the complete step-by-step implementation of this android application. Note that we are going to implement the project using both **Java and **Kotlin language.

**Note: Following steps are performed on **Android Studio Ladybug 2024.2.2

Step 1: Create a new project

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

Step 2: Working with the activity_main.xml file

Open the activity_main.xml file and add a Button, a TextView to display the string, and a Progress Bar to give a dynamic feel to the application. Below is the code for designing a proper activity layout.

**activity_main.xml:

XML `

<!-- TextView to display sub heading of the activity -->
<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="GFG Online Courses"
    android:textAlignment="center"
    android:textColor="@color/colorPrimary"
    android:textSize="24sp"
    android:textStyle="bold"/>

<!-- TextView to display the random string -->
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="32dp"
    android:text="Course description"
    android:textAlignment="center" />

<!-- Button to display next random string -->
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next" />

<!-- Progress Bar to be displayed before displaying next string -->
<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone" />

`

**Design UI:

design-ui-mvp

Step 3: Defining the Contract Interface file for the Model, View, and Presenter

To establish communication between View-Presenter and Presenter-Model, an interface is needed. This interface class will contain all abstract methods which will be defined later in the View, Model, and Presenter class.

**View:

Java `

public interface Contract { interface View { // method to display progress bar // when next random course details // is being fetched void showProgress();

    // method to hide progress bar
    // when next random course details
    // is being fetched
    void hideProgress();

    // method to set random
    // text on the TextView
    void setString(String string);
}

interface Model {

    // nested interface to be
    interface OnFinishedListener {
        // function to be called
        // once the Handler of Model class
        // completes its execution
        void onFinished(String string);
    }

    void getNextCourse(Contract.Model.OnFinishedListener onFinishedListener);
}

interface Presenter {

    // method to be called when
    // the button is clicked
    void onButtonClick();

    // method to destroy
    // lifecycle of MainActivity
    void onDestroy();
}

}

Kotlin

interface Contract { interface View { // method to display progress bar // when next random course details // is being fetched fun showProgress()

    // method to hide progress bar
    // when next random course details
    // is being fetched
    fun hideProgress()

    // method to set random
    // text on the TextView
    fun setString(string: String?)
}

interface Model {
    // nested interface to be
    interface OnFinishedListener {
        // function to be called
        // once the Handler of Model class
        // completes its execution
        fun onFinished(string: String?)
    }

    fun getNextCourse(onFinishedListener: OnFinishedListener?)
}

interface Presenter {
    // method to be called when
    // the button is clicked
    fun onButtonClick()

    // method to destroy
    // lifecycle of MainActivity
    fun onDestroy()
}

}

`

Step 4: Creating the Model class

Create a new class named Model to separate all string data and the methods to fetch those data. This class will not know the existence of View Class.

**Model:

Java `

import android.os.Handler;

import java.util.Arrays; import java.util.List; import java.util.Random;

public class Model implements Contract.Model {

// array list of strings from which
// random strings will be selected
// to display in the activity
private List<String> arrayList = Arrays.asList(
        "DSA Self Paced: Master the basics of Data Structures and Algorithms to solve complex problems efficiently. ",
        "Placement 100: This course will guide you for placement with theory,lecture videos, weekly assignments " +
                "contests and doubt assistance.",
        "Amazon SDE Test Series: Test your skill & give the final touch to your preparation before applying for " +
                "product based against like Amazon, Microsoft, etc.",
        "Complete Interview Preparation: Cover all the important concepts and topics required for the interviews. " +
                "Get placement ready before the interviews begin",
        "Low Level Design for SDE 1 Interview: Learn Object-oriented Analysis and Design to prepare for " +
                "SDE 1 Interviews in top companies"
);

@Override
// this method will invoke when
// user clicks on the button
// and it will take a delay of
// 1200 milliseconds to display next course detail
public void getNextCourse(final OnFinishedListener listener) {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            listener.onFinished(getRandomString());
        }
    }, 1200);
}

// method to select random
// string from the list of strings
private String getRandomString() {
    Random random = new Random();
    int index = random.nextInt(arrayList.size());
    return arrayList.get(index);
}

}

Kotlin

package org.geeksforgeeks.demo

import android.os.Handler import java.util.*

class Model : Contract.Model { // array list of strings from which // random strings will be selected // to display in the activity private val arrayList = listOf( "DSA Self Paced: Master the basics of Data Structures and Algorithms to solve complex problems efficiently. ", "Placement 100: This course will guide you for placement with theory,lecture videos, weekly assignments " + "contests and doubt assistance.", "Amazon SDE Test Series: Test your skill & give the final touch to your preparation before applying for " + "product based against like Amazon, Microsoft, etc.", "Complete Interview Preparation: Cover all the important concepts and topics required for the interviews. " + "Get placement ready before the interviews begin", "Low Level Design for SDE 1 Interview: Learn Object-oriented Analysis and Design to prepare for " + "SDE 1 Interviews in top companies" )

// this method will invoke when
// user clicks on the button
// and it will take a delay of
// 1200 milliseconds to display next course detail
override fun getNextCourse(onFinishedListener: Contract.Model.OnFinishedListener?) {
    Handler().postDelayed({ onFinishedListener!!.onFinished(getRandomString) }, 1000)
}


// method to select random
// string from the list of strings
private val getRandomString: String
    get() {
        val random = Random()
        val index = random.nextInt(arrayList.size)
        return arrayList[index]
    }

}

`

Step 5: Creating the Presenter class

The methods of this class contain core business logic which will decide what to display and how to display. It triggers the View class to make the necessary changes to the UI.

**Presenter:

Java `

public class Presenter implements Contract.Presenter, Contract.Model.OnFinishedListener {

// creating object of View Interface
private Contract.View mainView;

// creating object of Model Interface
private Contract.Model model;

// instantiating the objects of View and Model Interface
public Presenter(Contract.View mainView, Contract.Model model) {
    this.mainView = mainView;
    this.model = model;
}

@Override
// operations to be performed
// on button click
public void onButtonClick() {
    if (mainView != null) {
        mainView.showProgress();
    }
    model.getNextCourse(this);
}

@Override
public void onDestroy() {
    mainView = null;
}

@Override
// method to return the string
// which will be displayed in the
// Course Detail TextView
public void onFinished(String string) {
    if (mainView != null) {
        mainView.setString(string);
        mainView.hideProgress();
    }
}

}

Kotlin

// instantiating the objects of View and Model Interface // creating object of View Interface // creating object of Model Interface class Presenter( private var mainView: Contract.View?, private val model: Contract.Model) : Contract.Presenter, Contract.Model.OnFinishedListener {

// operations to be performed
// on button click
override fun onButtonClick() {
    if (mainView != null) {
        mainView!!.showProgress()
    }
    model.getNextCourse(this)
}

override fun onDestroy() {
    mainView = null
}

// method to return the string
// which will be displayed in the
// Course Detail TextView
override fun onFinished(string: String?) {
    if (mainView != null) {
        mainView!!.setString(string)
        mainView!!.hideProgress()
    }
}

}

`

Step 6: Create a Factory Object

The factory object declares a singleton object. This object is responsible for creating and providing Presenter instances, so that the View doesn't need to directly create the Model.

**PresenterFactory:

Java `

package org.geeksforgeeks.demo;

public class PresenterFactory {

public static Contract.Presenter providePresenter(Contract.View view) {
    Contract.Model model = new Model();
    return new Presenter(view, model);
}

}

Kotlin

package org.geeksforgeeks.demo

object PresenterFactory { fun providePresenter(view: Contract.View): Contract.Presenter { val model = Model() return Presenter(view, model) } }

`

Step 7: Define functionalities of View in the MainActivity file

The View class is responsible for updating the UI according to the changes triggered by the Presenter layer. The data provided by the Model will be used by View and the appropriate changes will be made in the activity.

**MainActivity File:

Java `

import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView;

import static android.view.View.GONE;

public class MainActivity extends AppCompatActivity implements Contract.View {

// creating object of TextView class
private TextView textView;

// creating object of Button class
private Button button;

// creating object of ProgressBar class
private ProgressBar progressBar;

// creating object of Presenter interface in Contract
Contract.Presenter presenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // assigning ID of the TextView
    textView = findViewById(R.id.textView);

    // assigning ID of the Button
    button = findViewById(R.id.button);

    // assigning ID of the ProgressBar
    progressBar = findViewById(R.id.progressBar);

    // object of Presenter Interface
    presenter = (Presenter) PresenterFactory.providePresenter(this);

    // operations to be performed when
    // user clicks the button
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            presenter.onButtonClick();
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    presenter.onDestroy();
}

@Override
// method to display the Course Detail TextView
public void showProgress() {
    progressBar.setVisibility(View.VISIBLE);
    textView.setVisibility(View.INVISIBLE);
}

@Override
// method to hide the Course Detail TextView
public void hideProgress() {
    progressBar.setVisibility(GONE);
    textView.setVisibility(View.VISIBLE);
}

@Override
// method to set random string
// in the Course Detail TextView
public void setString(String string) {
    textView.setText(string);
}

}

Kotlin

package org.geeksforgeeks.demo

import android.os.Bundle import android.view.View import android.widget.Button import android.widget.ProgressBar import android.widget.TextView import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity(), Contract.View { private lateinit var textView: TextView private lateinit var button: Button private lateinit var progressBar: ProgressBar private lateinit var presenter: Presenter

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    textView = findViewById(R.id.textView)
    button = findViewById(R.id.button)
    progressBar = findViewById(R.id.progressBar)

    // object of Presenter Interface
    presenter = PresenterFactory.providePresenter(this) as Presenter

    // operations to be performed when
    // user clicks the button
    this.button.setOnClickListener {
        presenter.onButtonClick()
    }
}

override fun onDestroy() {
    super.onDestroy()
    presenter.onDestroy()
}

// method to display the Course Detail TextView
override fun showProgress() {
    progressBar.visibility = View.VISIBLE
    textView.visibility = View.INVISIBLE
}

// method to hide the Course Detail TextView
override fun hideProgress() {
    progressBar.visibility = View.GONE
    textView.visibility = View.VISIBLE
}

// method to set random string
// in the Course Detail TextView
override fun setString(string: String?) {
    textView.text = string
}

}

`

Output:

Advantages of MVP Architecture

Disadvantages of MVP Architecture