Question: I can't get my head around how to apply the model-view-controller design pattern in an android studio project.

I understand that the view is the .xml layouts and that the activity classes are supposed to be the controllers, but I'm confused about whether general programming logic should be a part of the controller or model.

Can the programming logic related to a particular activity be included within the activity class or should they be separated into two classes? i.e. the activity handling just user input and a second class dealing with the associated programming logic.

Answer: 
It's always a good idea to isolate your code depending on the responsibilities. It's gives us several advantages when it's come to code readability and maintainability.

Since you have asked for MVC pattern but here we have a very good example of MVP pattern which is explained on the following link of Google samples regarding clean architecture in Android projects.

Link: <https://github.com/googlesamples/android-architecture/blob/todo-mvp/README.md>

![enter image description here](https://i.stack.imgur.com/QhVU2.png)

Components:

Model: More of your business entities/Pojo
Views: More of your fragments and activities
Repository: Data source for the information. It can be one of your database, cache, remote server, file system etc.
Presentor: It's a layer which get data from one of your repository and send result back to your views.

I have tried to implement similar pattern in one of my learning project.

Link: <https://github.com/amol-kamble/movie-finder/tree/master/app/src/main>
```html
//Student Model Class
public class Student {
   private String rollNo;
   private String name;

   public String getRollNo() {
      return rollNo;
   }

   public void setRollNo(String rollNo) {
      this.rollNo = rollNo;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

//Student View Class
public class StudentView {

  private TextView name;
  private TextView rollNo;

   public StudentView(View view){
      name=(TextView)view.findViewById(R.id.tv_name);
      rollNo=(TextView)view.findViewById(R.id.tv_roll_no);
   }

   public void setStudentDetails(Student studentModel){
      name.setText(studentModel.getName());
      rollNo.setText(studentModel.getRollNo());
   }
}

//Student Controller Class
public class StudentController {
   private Student model;
   private StudentView view;

   public StudentController(Student model, StudentView view){
      this.model = model;
      this.view = view;
   }

   public void setStudentName(String name){
      model.setName(name);  
   }

   public String getStudentName(){
      return model.getName();  
   }

   public void setStudentRollNo(String rollNo){
      model.setRollNo(rollNo);  
   }

   public String getStudentRollNo(){
      return model.getRollNo();  
   }

   public void updateView(){    
      view.setStudentDetails(model);
   } 
}
```

You can use the MVC pattern as given above.Create objects of all three classes in activity and pass the activity's view(xml file) reference to the StudentView object.Create child views in StudentView class as mentioned in xml file.