Hello Friends,
Today I am going to share a very simple example of Android MVP.
In this sample I am using MVP pattern for network call and and user
form(login form) data validation which is mainly used in most of the android
application.
Model View Presenter divides application into three layers i.e: Model, View and Presenter.
Today I am going to share a very simple example of Android MVP.
In this sample I am using MVP pattern for network call and and user
form(login form) data validation which is mainly used in most of the android
application.
Model View Presenter divides application into three layers i.e: Model, View and Presenter.
- Model: This handles the data part of our application
- Presenter: It acts as a bridge that connects a Model and a View.
- View: This is responsible for laying out views with the relevant data as instructed by the Presenter
Note: The View never communicates with Model directly
1. LoginActivity.java
package android.developer.solutions.androidmvp.activity.login.view; import android.content.Intent; import android.developer.solutions.androidmvp.R; import android.developer.solutions.androidmvp.activity.home.view.HomeActivity; import android.developer.solutions.androidmvp.activity.login.interactor.LoginInteractorImpl; import android.developer.solutions.androidmvp.activity.login.presenter.ILoginPresenter; import android.developer.solutions.androidmvp.activity.login.presenter.LoginPresenterImpl; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; public class LoginActivity extends AppCompatActivity implements ILoginView { EditText username; EditText password; ProgressBar progress; Button button; private ILoginPresenter presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); progress = (ProgressBar) findViewById(R.id.progress); button = (Button) findViewById(R.id.btnLogin); password = (EditText) findViewById(R.id.password); username = (EditText) findViewById(R.id.username); presenter = new LoginPresenterImpl(this, new LoginInteractorImpl()); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { presenter.validateCredentials(username.getText().toString(), password.getText().toString()); } }); } @Override protected void onDestroy() { presenter.onDestroy(); super.onDestroy(); } @Override public void showProgress() { progress.setVisibility(View.VISIBLE); } @Override public void hideProgress() { progress.setVisibility(View.GONE); } @Override public void setUsernameError() { username.setError("Invalid user name"); } @Override public void setPasswordError() { password.setError("Invalid password"); } @Override public void navigateToHome() { startActivity(new Intent(this, HomeActivity.class)); finish(); } }
2. ILoginView.java
Download complete code from here
Hope this will helps some one.
Enjoy Coding............ :)
package android.developer.solutions.androidmvp.activity.login.view; /** * Created by Mukesh on 12/16/2017. * androiddevelopersolutions.com */ public interface ILoginView { void showProgress(); void hideProgress(); void setUsernameError(); void setPasswordError(); void navigateToHome(); }
Download complete code from here
Hope this will helps some one.
Enjoy Coding............ :)