Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
The Model-View-Controller (MVC) pattern divides an application into three interconnected components:
In Android, the MVC pattern can be implemented in the following way:
Let's consider a simple example where we have a screen displaying a list of items and a button to add a new item.
The Model class represents the data.
data class Item(val name: String, val description: String)
The XML layout represents the view.
<LinearLayout ...> <Button android:id="@+id/addItemButton" android:text="Add Item" ... /> <ListView android:id="@+id/itemListView" .../> </LinearLayout>
The Activity or Fragment acts as a controller, setting up the views and handling user interactions.
class ItemActivity : AppCompatActivity() { private val itemList = mutableListOf<Item>() private lateinit var adapter: ArrayAdapter<Item> override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_item) adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, itemList) itemListView.adapter = adapter addItemButton.setOnClickListener { // Update the model addItem(Item("New Item", "Description")) // Update the view adapter.notifyDataSetChanged() } } private fun addItem(item: Item) { itemList.add(item) } }
In Android, Activities or Fragments often act as both View and Controller, which sometimes makes it challenging to achieve a clean separation as in traditional web frameworks. This limitation led to the emergence of other architectural patterns more suitable for Android, such as MVP (Model-View-Presenter), MVVM (Model-View-ViewModel), and MVI (Model-View-Intent).
Implementing MVC in Android example code:
// Model public class UserModel { // Model logic } // View public class UserView { // View logic } // Controller public class UserController { private UserModel model; private UserView view; public UserController(UserModel model, UserView view) { this.model = model; this.view = view; } public void updateView() { view.displayUser(model.getUser()); } }
Handling user input and interactions in MVC Android:
// Example in Android Activity public class UserActivity extends AppCompatActivity { private UserController controller; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); UserModel model = new UserModel(); UserView view = new UserView(this); controller = new UserController(model, view); // Handle user interactions view.setOnUserClickListener(user -> { model.setUser(user); controller.updateView(); }); } }
Data binding in MVC architecture for Android:
// In UserController public void updateView() { view.displayUser(model.getUser()); }
Android MVC architecture library and frameworks:
// Example using a third-party library implementation 'com.github.roboguice:roboguice:4.0.1'
The RoboGuice library, for instance, simplifies dependency injection in Android applications.