{"id":4780,"date":"2018-07-24T16:50:31","date_gmt":"2018-07-24T11:20:31","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=4780"},"modified":"2023-01-20T18:55:38","modified_gmt":"2023-01-20T13:25:38","slug":"mvp-for-android","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/","title":{"rendered":"Model View Presenter For Android"},"content":{"rendered":"<p>In this blog, we are going discuss about MVP Design Pattern for android which is a better and modified alternate of MVC.<\/p>\n<p><strong>Note:<\/strong><\/p>\n<p>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<br \/>\nto write our code in more readable and segregated manner. The main concept we are<br \/>\ngoing to learn is how all three MODEL, VIEW, PRESENTER are interlinked and after<br \/>\ngetting familiar with this, you can implement this design pattern in your own way.<\/p>\n<p><strong>What is MVP?<\/strong><\/p>\n<p>MVP is a design pattern for developers to write their code in more readable, maintainable and<br \/>\nscalable 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.<\/p>\n<p><b>1.Model<\/b><\/p>\n<p>Everything which is related with data is a part of Model.\u00a0Model contains a data provider and the code which fetches and updates the data. This part of MVP i.e <strong>Model<\/strong> updates the database or communicates with a web server.<\/p>\n<p><b>2.Presenter<\/b><\/p>\n<p>The presenter will have the whole business&#8217; logic and when an operation is performed or data is changed then it will notify the View for updation .<\/p>\n<p><b>3.View<\/b><\/p>\n<p>A view part of MVP contains a visual part of our application like showing dialogs, toast<br \/>\nmessages,\u00a0 handling\u00a0visibility. View contains only that part which is related to UI and<br \/>\nit does not contain any logic related to displayed data,\u00a0 and it is controlled by presenter.<\/p>\n<p><strong>Why use MVP?<\/strong><\/p>\n<p>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<br \/>\nof\u00a0code is easy to understand and maintain.<br \/>\n<strong>For example<\/strong>: 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.<\/p>\n<p><strong>How to implement MVP for Android:<\/strong><\/p>\n<p>A simple example for Login a user with MVP design Pattern.<\/p>\n<pre class=\"\">\/*The Interface LoginPresenter.*\/\r\n\r\npublic interface LoginPresenter \r\n{\r\n\/*when user click on login button from Activity*\/\r\nvoid handleLogin(String username, String password);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"\">\/*The Interface LoginView.*\/\r\npublic interface LoginView \r\n{\r\n    void showValidationErrorMsg();\r\n\r\n    void loginSuccessFully();\r\n\r\n    void loginFail();\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"\">\/* Class LoginPresenterImpl.*\/\r\n\r\npublic class LoginPresenterImpl implements LoginPresenter\r\n{\r\n\r\n    private LoginView loginView;\r\n\r\n    public LoginPresenterImpl(LoginView loginView) \r\n    {\r\n        this.loginView = loginView;\r\n    }\r\n    @Override\r\n    public void handleLogin(String username, String password) \r\n    {\r\n        if ((TextUtils.isEmpty(username) || TextUtils.isEmpty(password))\r\n        {\r\n            loginView.showValidationErrorMsg();\r\n        } \r\n        else\r\n        {\r\n            if (username.equals(\"Standerd\") &amp;&amp; password.equals(\"Standerd\"))\r\n            {\r\n                loginView.loginSuccessFully();\r\n            }\r\n            else\r\n            {\r\n                loginView.loginFail();\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"\">\/* Main Activity Class.*\/\r\npublic class MainActivity extends AppCompatActivity implements LoginView\r\n{\r\n    private LoginPresenter presenter;\r\n    private TextView textViewUserName;\r\n    private TextView textViewPassword;\r\n    private Button buttonLogin;\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState)\r\n    {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        initializeView();\r\n        presenter = new LoginPresenterImpl(this);\r\n\r\n        buttonLogin.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n                presenter.login(textViewUserName.getText().toString(), textViewPassword.getText().toString());\r\n              }\r\n        });\r\n    }\r\n\r\n    private void initializeView()\r\n    {\r\n        textViewUserName = findViewById(R.id.textViewUserName);\r\n        textViewPassword = findViewById(R.id.textViewPassword);\r\n        buttonLogin = findViewById(R.id.buttonLogin);\r\n    }\r\n\r\n    @Override\r\n    public void showValidationErrorMsg()\r\n    {\r\n        Toast.makeText(this, \"Username or Password is incorrect\", Toast.LENGTH_SHORT).show();\r\n    }\r\n    @Override\r\n    public void loginSuccessFully()\r\n    {\r\n        Toast.makeText(this, \"Login SuccessFully\", Toast.LENGTH_SHORT).show();\r\n    }\r\n    @Override\r\n    public void loginFail()\r\n    {\r\n        Toast.makeText(this, \"Something went wrong\", Toast.LENGTH_SHORT).show();\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>In android,\u00a0it is not easy to separate interface from logic but MVP design pattern makes it<br \/>\neasier to prevent the activities which may end\u00a0up degrading into coupled classes.<br \/>\nIn big applicaitons, it is important to organize and manage the code which makes the applications easy to maintain and extend.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4916,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,71],"tags":[293,291,292],"class_list":["post-4780","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-mobile","tag-mvp","tag-mvp-architecture","tag-mvp-pattern-for-android"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Model View Presenter For Android - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Model View Presenter For Android - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"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 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-24T11:20:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-20T13:25:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/MVP.png\" \/>\n\t<meta property=\"og:image:width\" content=\"624\" \/>\n\t<meta property=\"og:image:height\" content=\"359\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/mvp-for-android\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/mvp-for-android\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"Model View Presenter For Android\",\"datePublished\":\"2018-07-24T11:20:31+00:00\",\"dateModified\":\"2023-01-20T13:25:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/mvp-for-android\\\/\"},\"wordCount\":412,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/mvp-for-android\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/MVP.png\",\"keywords\":[\"MVP\",\"MVP Architecture\",\"MVP Pattern for android\"],\"articleSection\":[\"Android\",\"Mobile\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/mvp-for-android\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/mvp-for-android\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/mvp-for-android\\\/\",\"name\":\"Model View Presenter For Android - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/mvp-for-android\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/mvp-for-android\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/MVP.png\",\"datePublished\":\"2018-07-24T11:20:31+00:00\",\"dateModified\":\"2023-01-20T13:25:38+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/mvp-for-android\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/mvp-for-android\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/mvp-for-android\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/MVP.png\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/MVP.png\",\"width\":624,\"height\":359},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/mvp-for-android\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Model View Presenter For Android\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Model View Presenter For Android - InnovationM - Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/","og_locale":"en_US","og_type":"article","og_title":"Model View Presenter For Android - InnovationM - Blog","og_description":"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 [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/","og_site_name":"InnovationM - Blog","article_published_time":"2018-07-24T11:20:31+00:00","article_modified_time":"2023-01-20T13:25:38+00:00","og_image":[{"width":624,"height":359,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/MVP.png","type":"image\/png"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"Model View Presenter For Android","datePublished":"2018-07-24T11:20:31+00:00","dateModified":"2023-01-20T13:25:38+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/"},"wordCount":412,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/MVP.png","keywords":["MVP","MVP Architecture","MVP Pattern for android"],"articleSection":["Android","Mobile"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/mvp-for-android\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/","url":"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/","name":"Model View Presenter For Android - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/MVP.png","datePublished":"2018-07-24T11:20:31+00:00","dateModified":"2023-01-20T13:25:38+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/mvp-for-android\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/MVP.png","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/MVP.png","width":624,"height":359},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/mvp-for-android\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Model View Presenter For Android"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=4780"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4780\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/4916"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=4780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=4780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=4780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}