Friday, 23 February 2018

Android MVP Tutorial From Beginer to Advance Part 3

Android MVP Part 3


android-mvp-stack-learning
Android MVP Part 3



In this Article we are going to make a project with functionality Login  and HomePage via MVP Pattern.

                                                 DOWNLOAD CODE

If you are not clear with Basic of MVP Pattern, Please read first part of MVP and Second Part of MVP which shows the basic structure of MVP Pattern

Lets start . Continue with our project which have started in
MVP Part 2 .


activity_login.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:padding="30dp"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/et_email"
        android:hint="Email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:hint="Pass"
        android:id="@+id/et_pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_submit"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit"
        />

</LinearLayout>



LoginActivity
    package com.stacklearning.androidmvp.login;



import android.content.Intent;

import android.os.Bundle;

import android.support.annotation.Nullable;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;



import com.stacklearning.androidmvp.R;

import com.stacklearning.androidmvp.home.HomeActivity;



/**

 * Created by jaspreet on 23/2/18.

 */



public class LoginActivity extends AppCompatActivity implements LoginView, View.OnClickListener {



    EditText et_email,et_pass;

    Button btn_submit;

    private LoginPresenter presenter;



    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);



        et_email=(EditText) findViewById(R.id.et_email);

        et_pass=(EditText) findViewById(R.id.et_pass);

        btn_submit=(Button) findViewById(R.id.btn_submit);



        btn_submit.setOnClickListener(this);



        presenter = new LoginPresenterImplementation(this);



    }



    @Override

    public void onClick(View v) {

        switch (v.getId()){

            case R.id.btn_submit:

              presenter.onSubmitClick(et_email.getText().toString(),et_pass.getText().toString());

                break;

        }

    }



    @Override

    public void goToHomeActivity() {



        startActivity(new Intent(this, HomeActivity.class));

    }



    @Override

    public void showError(String err) {



        Toast.makeText(this,err,Toast.LENGTH_SHORT).show();

    }

}


LoginView
package com.stacklearning.androidmvp.login;



/**

 * Created by jaspreet on 23/2/18.

 */



public interface LoginView {



    void goToHomeActivity();



    void showError(String err);

}


LoginPresenter
package com.stacklearning.androidmvp.login;
/**
 * Created by jaspreet on 23/2/18.
 */
public interface LoginPresenter {
    void onSubmitClick(String email, String password );
}
LoginPresenterImplemetation
package com.stacklearning.androidmvp.login;
import android.text.TextUtils;
/**
 * Created by jaspreet on 23/2/18.
 */
public class LoginPresenterImplementation implements LoginPresenter {
    LoginView loginView;
    public LoginPresenterImplementation(LoginView loginView) {
         this.loginView=loginView;
    }
    @Override
    public void onSubmitClick(String email, String password) {
        if (TextUtils.isEmpty(email)) {
            loginView.showError("Email cannot empty");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            loginView.showError("  Passsword cannot empty");
            return;
        }
        if (email.equalsIgnoreCase("stacklearning") && password.equalsIgnoreCase("1234")){
                loginView.goToHomeActivity();
        } else {
            loginView.showError("Email and Passsword do not Match");
        }
    }
}
Hope you understand Code , If still not, Please mention in Coment .

You can download the code from github .

0 comments:

Post a Comment