Introduction
In this article, I will demonstrate how the latest MVVM (Model View View Model) framework functions for the Login form. The following requirements should be fulfilled before starting this article.
- Android Studio
- Android SDK
- Java
- XML
Once the SDK library has installed, we can create a new project in Android Studio. Open your project inside an app-level Gradle file and add the code shown below in the following format,
- dataBinding{
- enabled=true;
- }
Once we've added data binding we can go to the layout folder. This folder is displayed in the resource folder inside. Open the activity_main.xml file. You will then need to change the root layout name to “Layout”.
The code is below,
- <?xml version="1.0" encoding="utf-8"?>
- <layout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context="com.learning.designpattern.MainActivity">
- </LinearLayout>
- </layout>
Next, go to the Java folder and create a new Java class. In this class, we added view model data and we need to add more to BaseObservable. We now need to create a String value. This value will be shown on our XML input page. Next, create a constructor in the view model. Then create a model class and add the String value. This string value is for receiving and setting data in the class file or view model file. Finally, we need to create two constructors.
The first one is the dummy constructor and the second one is the single parameter constructor.
The code is shown below,
- public class CommonModel {
- public String name;
- public String mStrname;
- public CommonModel() {
- }
- public CommonModel(String mStrname) {
- this.mStrname = mStrname;
- }
- }

Florian LefevrePosted Mar 22, 2022, 9:07 AM
Why even a C# blog don't explain how to implement data binding for MVVM in Xamarin.Android ? There is so much tutorial for java and no one for C# in Xamarin.Android