MVP Architecture with Example in Android

MVP Architecture with Example in Android

In this blog, we will take an example of a login screen in Android and cover up MVP very simply.

No alt text provided for this image

As shown in the above screen we have the Username and Password field and a Login button.

If the user enters the username and password and tries to log in we will show a success or error message in a text view below the button.

Here we will have 3 parts as MVP says

  1. Model part-> Contains logic
  2. View Part-> Contains UI part
  3. Presenter Part-> Responsible for communication between?Model?and?View

Here is the project structure of our application

No alt text provided for this image

Let's Discuss 1 By 1 in Detail

The View part?is responsible for getting the user’s input and sending to?the Model

Code of?MainActivity

class MainActivity : AppCompatActivity(), LoginResultListener {
    //declaration of the views
    private lateinit var etUserName: EditText
    private lateinit var etPassword: EditText
    private lateinit var btnLogin: Button
    private lateinit var tvResult: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initUI()
    }

    private fun initUI() {
        //Binding the xml with kotlin code
        etUserName = findViewById(R.id.etUserName)
        etPassword = findViewById(R.id.etPassword)
        btnLogin = findViewById(R.id.btnLogin)
        tvResult = findViewById(R.id.tvResult)
        //btn click listener
        btnLogin.setOnClickListener {
            //Creating the object of presenter and passing reference of login result listener
            var presenter = LoginModel(this)
            // passing user name and password to login model by do login method
            presenter.doLogin(etUserName.text.toString(), etPassword.text.toString())
        }
    }

    override fun onSuccess(result: String) {
        tvResult.text = result
    }

    override fun onFail(errorMessage: String) {
        tvResult.text = errorMessage
    }

}        

LoginResultListener?is an interface which is responsible for listening to the result status we will send the result from the model to view by using this interface.

interface LoginResultListener {
    fun onSuccess(result: String)
    fun onFail(errorMessage: String)
}        

The view part is over now

The presenter part?is responsible for presenting the data to model

The presenter has a doLogin method which will be implemented by Login Model and will have the logic for valid user login,In do login method we are sending the username and password entered by the user.

@FunctionalInterface
interface LoginPresenter {
    fun doLogin(userName: String, password: String)
}        

Model Part?is having the logic of valid-user

In the Login model we are passing the instance of LoginResultListener which will help us to send the data back to the UI again if the user has entered the correct username and password we will send success else we will send the fail message.

class LoginModel(var loginResult: LoginResultListener) : LoginPresenter {

    override fun doLogin(userName: String, password: String) {
        if (userName == "Aalishan" && password == "12345") {
            loginResult.onSuccess("Login Success Welcome $userName")
        } else {
            loginResult.onFail("Provided credentials are wrong")
        }

    }
}        

We have completed all the coding parts here and if you want to have the source code you can download it from here and play around.

https://github.com/Aalishan565/MVP-Pattern-Kotlin-Android

Yasser Assoul

Software Engineer | Native Android Developer | Kotlin Developer | Backend Engineer | Django Developer

1 年

Good explanation!

回复
Prakash Barik

Sr. Tech Lead Mobile App Developer on Android & iOS with skilled on IONIC, Kotlin & React Native | Proven Track Record of Developing User-Friendly Apps | Ready to Elevate Your Brand's Mobile Experience ????

1 年

Awesome ????

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了