DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Model-View-Presenter (MVP) for Android

Model-View-Presenter (MVP) for Android

We take a look at how to build apps and web apps for this popular mobile OS by using the Model-View-Presenter (MVP) pattern.

Abhishek Tripathi user avatar by
Abhishek Tripathi
·
Aug. 21, 18 · Tutorial
Like (5)
Save
Tweet
Share
31.19K Views

Join the DZone community and get the full member experience.

Join For Free

In this blog, we are going discuss the MVP Design Pattern for Android which is a better and modified alternate to MVC.

Note: I am trying to make this (MVP) concept as easy as possible because everyone might have different opinions on MVP. So, do not get confused because it is simply a design pattern to write our code in a more readable and segregated manner. The main concepts we are going to learn are how the Model, View, and Presenter are interlinked and to get familiar with this. You can implement this design pattern in your own way.

What Is MVP?

MVP is a design pattern for developers to write their code in a more readable, maintainable, and
scalable manner. In MVP, our code is divided into three parts named Model, View, and Presenter, rather than placing the whole code in one activity.

1. Model

Everything which is related with data is a part of the Model. The Model contains a data provider and the code which fetches and updates the data. This part of MVP updates the database or communicates with a web server.

2. Presenter

The Presenter contains the application's business logic and when an operation is performed or data is changed then it will notify the View that it needs to update.

3. View

The View part of MVP contains the visual parts of our application, like showing messages, and also handles visibility. View contains only that part of the code which is related to UI and it does not contain any logic related to the displayed data. The View is controlled by the Presenter.

Why Use MVP?

This MVP design pattern helps to segregate code into three different parts: business logic (Presenter); UI (View); data interaction (Model). This modulation of code is easy to understand and maintain.

For example: In our application, if we use a content provider to persist our data and later we want to upgrade it with a SQLite database, the MVP design pattern will make this very easy.

How to Implement MVP for Android:

A simple example of a user login made with the MVP design Pattern.

/*The Interface LoginPresenter.*/

public interface LoginPresenter 
{
/*when user click on login button from Activity*/
void handleLogin(String username, String password);
}

 

/*The Interface LoginView.*/
public interface LoginView 
{
    void showValidationErrorMsg();

    void loginSuccessFully();

    void loginFail();
}

 

/* Class LoginPresenterImpl.*/

public class LoginPresenterImpl implements LoginPresenter
{

    private LoginView loginView;

    public LoginPresenterImpl(LoginView loginView) 
    {
        this.loginView = loginView;
    }
    @Override
    public void handleLogin(String username, String password) 
    {
        if ((TextUtils.isEmpty(username) || TextUtils.isEmpty(password))
        {
            loginView.showValidationErrorMsg();
        } 
        else
        {
            if (username.equals("Standerd") && password.equals("Standerd"))
            {
                loginView.loginSuccessFully();
            }
            else
            {
                loginView.loginFail();
            }
        }
    }
}

 

/* Main Activity Class.*/
public class MainActivity extends AppCompatActivity implements LoginView
{
    private LoginPresenter presenter;
    private TextView textViewUserName;
    private TextView textViewPassword;
    private Button buttonLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initializeView();
        presenter = new LoginPresenterImpl(this);

        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.login(textViewUserName.getText().toString(), textViewPassword.getText().toString());
              }
        });
    }

    private void initializeView()
    {
        textViewUserName = findViewById(R.id.textViewUserName);
        textViewPassword = findViewById(R.id.textViewPassword);
        buttonLogin = findViewById(R.id.buttonLogin);
    }

    @Override
    public void showValidationErrorMsg()
    {
        Toast.makeText(this, "Username or Password is incorrect", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void loginSuccessFully()
    {
        Toast.makeText(this, "Login SuccessFully", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void loginFail()
    {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show();
    }
}

Conclusion

In Android, it is not easy to separate the interface from the logic, but the MVP design pattern makes it easier to prevent activities which may end up degrading into coupled classes. In big applicaitons, it is important to organize and manage the code which makes the applications easy to maintain and extend.

Minimum viable product Android (robot) Design

Published at DZone with permission of Abhishek Tripathi. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Enabling DB Migrations Using Kubernetes Init
  • PostgreSQL: Bulk Loading Data With Node.js and Sequelize
  • 9 Ways You Can Improve Security Posture
  • How to Quickly Build an Audio Editor With UI

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: