SlideShare a Scribd company logo
Model View Presenter For Android
In this blog, we are going discuss about MVP Design Pattern for android which is a better
and modified alternate of MVC.
Note:
I am trying to make this(MVP) concept as easy as possible because everyone might have
different opinions for MVP. So, do not get confused because it is simply a design pattern
to write our code in more readable and segregated manner. The main concept we are
going to learn is how all three MODEL, VIEW, PRESENTER are interlinked and after
getting 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 more readable, maintainable
and
scalable manner. In MVP, our code is divided into three parts named as 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 Model. Model contains a data provider
and the code which fetches and updates the data. This part of MVP i.e Model updates the
database or communicates with a web server.
2.Presenter
The presenter will have the whole business’ logic and when an operation is performed or
data is changed then it will notify the View for updation .
3.View
A view part of MVP contains a visual part of our application like showing dialogs, toast
messages, handling visibility. View contains only that part which is related to UI and
it does not contain any logic related to displayed data, and it is controlled by presenter.
Why use MVP?
This MVP design pattern helps to segregate the code in three different parts which are
business logic (Presenter) UI part (View) and data interaction(Model). This modulation
of code is easy to understand and maintain.
For example: In our application, if we use the content provider to persist our data and
later we want to upgrade it with SQLite database then it will be very easy in case of MVP
design pattern.
How to implement MVP for Android:
A simple example for Login a user with MVP design Pattern.
1
2
3
4
5
6
7
/*The Interface LoginPresenter.*/
public interface LoginPresenter
{
/*when user click on login button from Activity*/
void handleLogin(String username, String password);
}
1
2
3
4
5
6
7
8
9
/*The Interface LoginView.*/
public interface LoginView
{
void showValidationErrorMsg();
void loginSuccessFully();
void loginFail();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* 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();
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* 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 interface from logic but MVP design pattern makes it
easier to prevent the activities which may end up degrading into coupled classes.
In big applications, it is important to organize and manage the code which makes the
applications easy to maintain and extend.

More Related Content

PDF
Model View Presenter For Android
PDF
Model View Presenter For Android
PPTX
Introduction To Model View Presenter
PPTX
Model View Presenter (MVP) In Aspnet
PDF
Mvp pattern
PPTX
Slide Presentation of MVP Pattern Concept
PPTX
Mvc vs mvp vs mvvm a guide on architecture presentation patterns
PPTX
MVVM presentation
Model View Presenter For Android
Model View Presenter For Android
Introduction To Model View Presenter
Model View Presenter (MVP) In Aspnet
Mvp pattern
Slide Presentation of MVP Pattern Concept
Mvc vs mvp vs mvvm a guide on architecture presentation patterns
MVVM presentation

What's hot (20)

PDF
PPTX
Acrhitecture deisign pattern_MVC_MVP_MVVM
PPTX
PDF
Model view view model
PPT
Ppt of Basic MVC Structure
PPTX
Exploring MVVM, MVC, MVP Patterns - CRB Tech
PPTX
MVVM ( Model View ViewModel )
PPT
MVC(Model View Controller),Web,Enterprise,Mobile
PPTX
The MVVM Pattern
PPTX
Model viewviewmodel2
PPTX
Design Pattern - MVC, MVP and MVVM
PDF
5 benefits of angular js
PDF
Mvc, mvp, mvvm...
PPTX
Class 02 Objective C
PDF
Structuring Your Sencha Touch Application
PDF
Ui design patterns
PPT
How ASP.NET MVC Implementation Help Enterprise Web Application Development?
PPTX
Intro ASP MVC
PDF
Android MVVM TDD
Acrhitecture deisign pattern_MVC_MVP_MVVM
Model view view model
Ppt of Basic MVC Structure
Exploring MVVM, MVC, MVP Patterns - CRB Tech
MVVM ( Model View ViewModel )
MVC(Model View Controller),Web,Enterprise,Mobile
The MVVM Pattern
Model viewviewmodel2
Design Pattern - MVC, MVP and MVVM
5 benefits of angular js
Mvc, mvp, mvvm...
Class 02 Objective C
Structuring Your Sencha Touch Application
Ui design patterns
How ASP.NET MVC Implementation Help Enterprise Web Application Development?
Intro ASP MVC
Android MVVM TDD
Ad

Similar to Model View Presenter For Android (20)

PPTX
Rendra Toro - Model View Presenter
PPTX
Design patterns in android
PPTX
Android DesignArchitectures.pptx
PPTX
Architectural Design Pattern: Android
PPTX
Mvp tech talks
PDF
Android Architecture MVP Pattern
PPTX
MVP in Android by Ratanak
PDF
Infinum Android Talks #12 - MVP design pattern for Android Apps
PPTX
My perspective on MVP and architecture discussions
DOCX
Mvc, mvp & mvvm (erp)
PPTX
UI Design Patterns
PDF
MVP and Multidirectional data flow
PDF
Model View Presenter
DOCX
Lecture10 oopj
PPTX
Building android app with mvp and kotlin
PPT
Design pattern in android
PDF
Clean VIP (Clean Swift) architecture
PDF
MVP Clean Architecture
PPTX
MVP vs MVVM : a fast introduction
Rendra Toro - Model View Presenter
Design patterns in android
Android DesignArchitectures.pptx
Architectural Design Pattern: Android
Mvp tech talks
Android Architecture MVP Pattern
MVP in Android by Ratanak
Infinum Android Talks #12 - MVP design pattern for Android Apps
My perspective on MVP and architecture discussions
Mvc, mvp & mvvm (erp)
UI Design Patterns
MVP and Multidirectional data flow
Model View Presenter
Lecture10 oopj
Building android app with mvp and kotlin
Design pattern in android
Clean VIP (Clean Swift) architecture
MVP Clean Architecture
MVP vs MVVM : a fast introduction
Ad

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Big Data Technologies - Introduction.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
Teaching material agriculture food technology
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Encapsulation theory and applications.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
Network Security Unit 5.pdf for BCA BBA.
The Rise and Fall of 3GPP – Time for a Sabbatical?
Spectral efficient network and resource selection model in 5G networks
Dropbox Q2 2025 Financial Results & Investor Presentation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Machine learning based COVID-19 study performance prediction
Big Data Technologies - Introduction.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Teaching material agriculture food technology
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
The AUB Centre for AI in Media Proposal.docx
Building Integrated photovoltaic BIPV_UPV.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Encapsulation theory and applications.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
20250228 LYD VKU AI Blended-Learning.pptx
NewMind AI Weekly Chronicles - August'25 Week I

Model View Presenter For Android

  • 1. Model View Presenter For Android In this blog, we are going discuss about MVP Design Pattern for android which is a better and modified alternate of MVC. Note: I am trying to make this(MVP) concept as easy as possible because everyone might have different opinions for MVP. So, do not get confused because it is simply a design pattern to write our code in more readable and segregated manner. The main concept we are going to learn is how all three MODEL, VIEW, PRESENTER are interlinked and after getting 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 more readable, maintainable and scalable manner. In MVP, our code is divided into three parts named as 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 Model. Model contains a data provider and the code which fetches and updates the data. This part of MVP i.e Model updates the database or communicates with a web server. 2.Presenter The presenter will have the whole business’ logic and when an operation is performed or data is changed then it will notify the View for updation . 3.View A view part of MVP contains a visual part of our application like showing dialogs, toast messages, handling visibility. View contains only that part which is related to UI and it does not contain any logic related to displayed data, and it is controlled by presenter. Why use MVP? This MVP design pattern helps to segregate the code in three different parts which are business logic (Presenter) UI part (View) and data interaction(Model). This modulation of code is easy to understand and maintain. For example: In our application, if we use the content provider to persist our data and later we want to upgrade it with SQLite database then it will be very easy in case of MVP design pattern. How to implement MVP for Android: A simple example for Login a user with MVP design Pattern.
  • 2. 1 2 3 4 5 6 7 /*The Interface LoginPresenter.*/ public interface LoginPresenter { /*when user click on login button from Activity*/ void handleLogin(String username, String password); } 1 2 3 4 5 6 7 8 9 /*The Interface LoginView.*/ public interface LoginView { void showValidationErrorMsg(); void loginSuccessFully(); void loginFail(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 /* 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(); } } } }
  • 3. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 /* 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 interface from logic but MVP design pattern makes it easier to prevent the activities which may end up degrading into coupled classes. In big applications, it is important to organize and manage the code which makes the applications easy to maintain and extend.