Working example of Android MVP
2 min readApr 11, 2020
As we already know about MVP that why and how to use MVP architecture, today will see a working example.
Firstly will write a Interface which will interact with activity and presenter.
public interface LoginActivityMVP {
interface View {
String getFirstName();
String getLastName();
void showUserNotAvailable();
void showInputError();
void showUserSavedMessage();
void setFirstName(String firstName);
void setLastName(String lastName);
}
interface Presenter {
void setView(LoginActivityMVP.View view);
void loginButtonClicked();
void getCurrentUser();
}
interface Model {
void createUser(String firstName, String lastName);
User getUser();
}
}
now will create a presenter which will interact with activity for particular event
public class LoginActivityPresenter implements LoginActivityMVP.Presenter {
@Nullable
private LoginActivityMVP.View view;
private LoginActivityMVP.Model model;
LoginActivityPresenter(LoginActivityMVP.Model model) {
this.model = model;
}
@Override
public void setView(LoginActivityMVP.View view) {
this.view = view;
}
@Override
public void loginButtonClicked() {
if (view != null) {
if (TextUtils.isEmpty(view.getFirstName()) || TextUtils.isEmpty(view.getLastName())) {
view.showInputError();
} else {
model.createUser(view.getFirstName(), view.getLastName());
view.showUserSavedMessage();
}
}
}
@Override
public void getCurrentUser() {
User user = model.getUser();
if (user == null) {
if (view != null) {
view.showUserNotAvailable();
}
} else {
if (view != null) {
view.setFirstName(user.getFirstName());
view.setLastName(user.getLastName());
}
}
}
}
Now will create a activity which will initialise the presenter and presenter contains all business logic
public class LoginActivity extends AppCompatActivity implements LoginActivityMVP.View {
LoginActivityMVP.Presenter presenter;
private EditText firstName, lastName;
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
presenter = new LoginActivityPresenter(<create a class and implement model interface which will handle create user logic>);
firstName = findViewById(R.id.loginActivity_firstName_editText);
lastName = findViewById(R.id.loginActivity_lastName_editText);
loginButton = findViewById(R.id.loginActivity_login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
@Override
protected void onResume() {
super.onResume();
presenter.setView(this);
}
@Override
public String getFirstName() {
return firstName.getText().toString();
}
@Override
public String getLastName() {
return lastName.getText().toString();
}
@Override
public void showUserNotAvailable() {
Toast.makeText(this, "User is not available", Toast.LENGTH_SHORT).show();
}
@Override
public void showInputError() {
Toast.makeText(this, "cannot be empty", Toast.LENGTH_SHORT).show();
}
@Override
public void showUserSavedMessage() {
Toast.makeText(this, "User saved", Toast.LENGTH_SHORT).show();
}
@Override
public void setFirstName(String firstName) {
this.firstName.setText(firstName);
}
@Override
public void setLastName(String lastName) {
this.lastName.setText(lastName);
}
}
and here is our user Model
public class User {
private int id;
private String firstName, lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
we have to create a model which will extends Model and will handle createUser logic.
Thanks for reading, please let me know if you have any doubts.