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

What's hot

Android MVVM
Android MVVMAndroid MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVMAcrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVM
Dong-Ho Lee
 
Android MVVM
Android MVVMAndroid MVVM
Model view view model
Model view view modelModel view view model
Model view view model
Binu Bhasuran
 
Ppt of Basic MVC Structure
Ppt of Basic MVC StructurePpt of Basic MVC Structure
Ppt of Basic MVC Structure
Dipika Wadhvani
 
Exploring MVVM, MVC, MVP Patterns - CRB Tech
Exploring MVVM, MVC, MVP Patterns - CRB TechExploring MVVM, MVC, MVP Patterns - CRB Tech
Exploring MVVM, MVC, MVP Patterns - CRB Tech
Pooja Gaikwad
 
MVVM ( Model View ViewModel )
MVVM ( Model View ViewModel )MVVM ( Model View ViewModel )
MVVM ( Model View ViewModel )
Ahmed Emad
 
MVC(Model View Controller),Web,Enterprise,Mobile
MVC(Model View Controller),Web,Enterprise,MobileMVC(Model View Controller),Web,Enterprise,Mobile
MVC(Model View Controller),Web,Enterprise,Mobile
naral
 
The MVVM Pattern
The MVVM PatternThe MVVM Pattern
The MVVM Pattern
Chris Charabaruk
 
Model viewviewmodel2
Model viewviewmodel2Model viewviewmodel2
Model viewviewmodel2
Suraj Kulkarni
 
Design Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMDesign Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVM
Mudasir Qazi
 
MVC
MVCMVC
5 benefits of angular js
5 benefits of angular js5 benefits of angular js
5 benefits of angular js
OnGraph Technologies Pvt. Ltd.
 
Mvc, mvp, mvvm...
Mvc, mvp, mvvm...Mvc, mvp, mvvm...
Mvc, mvp, mvvm...
Yury Kisliak
 
Class 02 Objective C
Class 02   Objective CClass 02   Objective C
Class 02 Objective C
Violeta Salas
 
Structuring Your Sencha Touch Application
Structuring Your Sencha Touch ApplicationStructuring Your Sencha Touch Application
Structuring Your Sencha Touch Application
Sencha
 
Ui design patterns
Ui design patternsUi design patterns
Ui design patterns
Jorge Ortiz
 
How ASP.NET MVC Implementation Help Enterprise Web Application Development?
How ASP.NET MVC  Implementation  Help Enterprise  Web Application Development?How ASP.NET MVC  Implementation  Help Enterprise  Web Application Development?
How ASP.NET MVC Implementation Help Enterprise Web Application Development?
Grey Matter India Technologies PVT LTD
 
Intro ASP MVC
Intro ASP MVCIntro ASP MVC
Intro ASP MVC
KrishnaPPatel
 
Android MVVM TDD
Android MVVM TDDAndroid MVVM TDD
Android MVVM TDD
KyungHo Jung
 

What's hot (20)

Android MVVM
Android MVVMAndroid MVVM
Android MVVM
 
Acrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVMAcrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVM
 
Android MVVM
Android MVVMAndroid MVVM
Android MVVM
 
Model view view model
Model view view modelModel view view model
Model view view model
 
Ppt of Basic MVC Structure
Ppt of Basic MVC StructurePpt of Basic MVC Structure
Ppt of Basic MVC Structure
 
Exploring MVVM, MVC, MVP Patterns - CRB Tech
Exploring MVVM, MVC, MVP Patterns - CRB TechExploring MVVM, MVC, MVP Patterns - CRB Tech
Exploring MVVM, MVC, MVP Patterns - CRB Tech
 
MVVM ( Model View ViewModel )
MVVM ( Model View ViewModel )MVVM ( Model View ViewModel )
MVVM ( Model View ViewModel )
 
MVC(Model View Controller),Web,Enterprise,Mobile
MVC(Model View Controller),Web,Enterprise,MobileMVC(Model View Controller),Web,Enterprise,Mobile
MVC(Model View Controller),Web,Enterprise,Mobile
 
The MVVM Pattern
The MVVM PatternThe MVVM Pattern
The MVVM Pattern
 
Model viewviewmodel2
Model viewviewmodel2Model viewviewmodel2
Model viewviewmodel2
 
Design Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMDesign Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVM
 
MVC
MVCMVC
MVC
 
5 benefits of angular js
5 benefits of angular js5 benefits of angular js
5 benefits of angular js
 
Mvc, mvp, mvvm...
Mvc, mvp, mvvm...Mvc, mvp, mvvm...
Mvc, mvp, mvvm...
 
Class 02 Objective C
Class 02   Objective CClass 02   Objective C
Class 02 Objective C
 
Structuring Your Sencha Touch Application
Structuring Your Sencha Touch ApplicationStructuring Your Sencha Touch Application
Structuring Your Sencha Touch Application
 
Ui design patterns
Ui design patternsUi design patterns
Ui design patterns
 
How ASP.NET MVC Implementation Help Enterprise Web Application Development?
How ASP.NET MVC  Implementation  Help Enterprise  Web Application Development?How ASP.NET MVC  Implementation  Help Enterprise  Web Application Development?
How ASP.NET MVC Implementation Help Enterprise Web Application Development?
 
Intro ASP MVC
Intro ASP MVCIntro ASP MVC
Intro ASP MVC
 
Android MVVM TDD
Android MVVM TDDAndroid MVVM TDD
Android MVVM TDD
 

Similar to Model View Presenter For Android

Android DesignArchitectures.pptx
Android DesignArchitectures.pptxAndroid DesignArchitectures.pptx
Android DesignArchitectures.pptx
SafnaSaff1
 
Architectural Design Pattern: Android
Architectural Design Pattern: AndroidArchitectural Design Pattern: Android
Architectural Design Pattern: Android
Jitendra Kumar
 
Mvp tech talks
Mvp tech talksMvp tech talks
Mvp tech talks
Uptech
 
Choosing the Right HTML5 Framework to Build your Mobile Web Application White...
Choosing the Right HTML5 Framework to Build your Mobile Web Application White...Choosing the Right HTML5 Framework to Build your Mobile Web Application White...
Choosing the Right HTML5 Framework to Build your Mobile Web Application White...
RapidValue
 
Task 2 - Educational Article – Model View Controller (MVC)
Task 2 - Educational Article – Model View Controller (MVC)Task 2 - Educational Article – Model View Controller (MVC)
Task 2 - Educational Article – Model View Controller (MVC)
Shubham Goenka
 
Building android app with mvp and kotlin
Building android app  with mvp and kotlinBuilding android app  with mvp and kotlin
Building android app with mvp and kotlin
Shivam Chopra
 
Design patterns in android
Design patterns in androidDesign patterns in android
Design patterns in android
Zahra Heydari
 
149 152
149 152149 152
Code Camp 06 Model View Presenter Architecture
Code Camp 06   Model View Presenter ArchitectureCode Camp 06   Model View Presenter Architecture
Code Camp 06 Model View Presenter Architecture
bitburner93
 
MVC in PHP
MVC in PHPMVC in PHP
MVC in PHP
Vineet Kumar Saini
 
mvc development company in UK
mvc development company in UK mvc development company in UK
mvc development company in UK
Techrishblogger
 
An overview of microsoft mvc dot net
An overview of microsoft mvc dot netAn overview of microsoft mvc dot net
An overview of microsoft mvc dot net
neha sharma
 
Why MVC?
Why MVC?Why MVC?
Why MVC?
Wayne Tun Myint
 
UI Design Patterns
UI Design PatternsUI Design Patterns
UI Design Patterns
aamiralihussain
 
Avigma Tech LLC- Why the MVC pattern so popular?
Avigma Tech LLC- Why the MVC pattern so popular?Avigma Tech LLC- Why the MVC pattern so popular?
Avigma Tech LLC- Why the MVC pattern so popular?
Mike Brown
 
Software architecture patterns
Software architecture patternsSoftware architecture patterns
Software architecture patterns
Md. Sadhan Sarker
 
MVC Architecture: A Detailed Insight to the Modern Web Applications Developme...
MVC Architecture: A Detailed Insight to the Modern Web Applications Developme...MVC Architecture: A Detailed Insight to the Modern Web Applications Developme...
MVC Architecture: A Detailed Insight to the Modern Web Applications Developme...
CrimsonpublishersPRSP
 
mvc development company in UK.
mvc development company in UK.mvc development company in UK.
mvc development company in UK.
Techrishblogger
 
mvc development company in UK
mvc development company in UKmvc development company in UK
mvc development company in UK
Techrishblogger
 
mvc development company in UK
mvc development company in UK mvc development company in UK
mvc development company in UK
Techrishblogger
 

Similar to Model View Presenter For Android (20)

Android DesignArchitectures.pptx
Android DesignArchitectures.pptxAndroid DesignArchitectures.pptx
Android DesignArchitectures.pptx
 
Architectural Design Pattern: Android
Architectural Design Pattern: AndroidArchitectural Design Pattern: Android
Architectural Design Pattern: Android
 
Mvp tech talks
Mvp tech talksMvp tech talks
Mvp tech talks
 
Choosing the Right HTML5 Framework to Build your Mobile Web Application White...
Choosing the Right HTML5 Framework to Build your Mobile Web Application White...Choosing the Right HTML5 Framework to Build your Mobile Web Application White...
Choosing the Right HTML5 Framework to Build your Mobile Web Application White...
 
Task 2 - Educational Article – Model View Controller (MVC)
Task 2 - Educational Article – Model View Controller (MVC)Task 2 - Educational Article – Model View Controller (MVC)
Task 2 - Educational Article – Model View Controller (MVC)
 
Building android app with mvp and kotlin
Building android app  with mvp and kotlinBuilding android app  with mvp and kotlin
Building android app with mvp and kotlin
 
Design patterns in android
Design patterns in androidDesign patterns in android
Design patterns in android
 
149 152
149 152149 152
149 152
 
Code Camp 06 Model View Presenter Architecture
Code Camp 06   Model View Presenter ArchitectureCode Camp 06   Model View Presenter Architecture
Code Camp 06 Model View Presenter Architecture
 
MVC in PHP
MVC in PHPMVC in PHP
MVC in PHP
 
mvc development company in UK
mvc development company in UK mvc development company in UK
mvc development company in UK
 
An overview of microsoft mvc dot net
An overview of microsoft mvc dot netAn overview of microsoft mvc dot net
An overview of microsoft mvc dot net
 
Why MVC?
Why MVC?Why MVC?
Why MVC?
 
UI Design Patterns
UI Design PatternsUI Design Patterns
UI Design Patterns
 
Avigma Tech LLC- Why the MVC pattern so popular?
Avigma Tech LLC- Why the MVC pattern so popular?Avigma Tech LLC- Why the MVC pattern so popular?
Avigma Tech LLC- Why the MVC pattern so popular?
 
Software architecture patterns
Software architecture patternsSoftware architecture patterns
Software architecture patterns
 
MVC Architecture: A Detailed Insight to the Modern Web Applications Developme...
MVC Architecture: A Detailed Insight to the Modern Web Applications Developme...MVC Architecture: A Detailed Insight to the Modern Web Applications Developme...
MVC Architecture: A Detailed Insight to the Modern Web Applications Developme...
 
mvc development company in UK.
mvc development company in UK.mvc development company in UK.
mvc development company in UK.
 
mvc development company in UK
mvc development company in UKmvc development company in UK
mvc development company in UK
 
mvc development company in UK
mvc development company in UK mvc development company in UK
mvc development company in UK
 

Recently uploaded

Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 

Recently uploaded (20)

Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 

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.