Android Core

MVC Architecture in Android — Explained with Example

1. What is MVC Architecture?

MVC stands for Model–View–Controller. It is one of the earliest and most fundamental software architecture patterns used to separate an application into three main logical components — Model, View, and Controller. The goal of MVC is to achieve separation of concerns so that each component handles a specific responsibility, improving modularity, scalability, and code maintainability.

2. The Three Components of MVC

  • Model: Represents the data layer and business logic. It manages the data, performs CRUD operations, and defines how data is manipulated.
  • View: Handles the UI — what the user sees and interacts with. It should be as “dumb” as possible, meaning it only displays the data given by the Controller.
  • Controller: Acts as a bridge between the Model and the View. It listens to user input, processes it (possibly via the Model), and updates the View accordingly.

3. How MVC Works in Android

In traditional Android development, Activity or Fragment often serves as both the View and the Controller. While this works for small projects, it can quickly lead to the infamous “God Activity” problem — where one file handles too many responsibilities.

To implement MVC properly, you should try to separate:

  • The UI layout (View) — XML files and UI rendering.
  • The Controller — Activity or a separate class handling user actions.
  • The Model — classes for data management, APIs, or databases.

4. Example: Simple Login Screen using MVC

Let’s walk through an example implementation of a basic login feature using MVC.

📁 Folder Structure


                    ├── model/
                    │   └── UserModel.kt
                    ├── view/
                    │   └── activity_login.xml
                    ├── controller/
                    │   └── LoginController.kt
                    ├── LoginActivity.kt
                    

🧩 Model: UserModel.kt


                    package com.example.mvc.model

                    data class UserModel(val username: String, val password: String) {

                        fun isValidUser(): Boolean {
                            // Example validation logic
                            return username.isNotEmpty() && password.length >= 6
                        }
                    }
                    

🎨 View: activity_login.xml


                    <LinearLayout
                        xmlns:android="http://schemas.android.com/apk/res/android"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
                        android:padding="24dp">

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

                        <EditText
                            android:id="@+id/etPassword"
                            android:hint="Password"
                            android:inputType="textPassword"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"/>

                        <Button
                            android:id="@+id/btnLogin"
                            android:text="Login"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"/>
                    </LinearLayout>
                    

🧠 Controller: LoginController.kt


                    package com.example.mvc.controller

                    import com.example.mvc.model.UserModel

                    class LoginController {
                        fun handleLogin(username: String, password: String): Boolean {
                            val user = UserModel(username, password)
                            return user.isValidUser()
                        }
                    }
                    

🎬 Activity (acts as View + Controller): LoginActivity.kt


                    package com.example.mvc.view

                    import android.os.Bundle
                    import android.widget.Toast
                    import androidx.appcompat.app.AppCompatActivity
                    import com.example.mvc.R
                    import com.example.mvc.controller.LoginController
                    import kotlinx.android.synthetic.main.activity_login.*

                    class LoginActivity : AppCompatActivity() {

                        private lateinit var controller: LoginController

                        override fun onCreate(savedInstanceState: Bundle?) {
                            super.onCreate(savedInstanceState)
                            setContentView(R.layout.activity_login)

                            controller = LoginController()

                            btnLogin.setOnClickListener {
                                val username = etUsername.text.toString()
                                val password = etPassword.text.toString()

                                if (controller.handleLogin(username, password)) {
                                    Toast.makeText(this, "Login successful!", Toast.LENGTH_SHORT).show()
                                } else {
                                    Toast.makeText(this, "Invalid credentials", Toast.LENGTH_SHORT).show()
                                }
                            }
                        }
                    }
                    

5. Advantages of MVC

  • Improved separation of concerns between UI, logic, and data.
  • Easier testing of Model and Controller logic independently.
  • Code is easier to maintain and scale for small to medium projects.

6. Limitations of MVC in Android

  • Tight coupling between View and Controller — Activities often become bloated.
  • Difficult testing when UI and Controller logic are mixed.
  • Scalability issues for large or complex apps — often replaced by MVP or MVVM.

Conclusion

MVC was the foundation for structured app design and remains valuable for understanding architectural separation. However, in modern Android development, patterns like MVP and MVVM offer better lifecycle awareness, reactive data flow, and cleaner separation between UI and logic.