Search code examples
androidandroid-studiokotlindata-bindingview

How does data-binding work in the Android IDE?


How data-binding work in the Android IDE? What is the behind-the-scenes process?

To nail down what I'm talking about, this guy tries to explain it but does an "eh" job of it. I can't really understand him.

https://youtu.be/scJGJgcbMGQ


Solution

  • What is Data Binding?

    The principal of Data Binding is to synchronise two objects so they hold the same information. This is used in both directions, from UI object change to underlining object and vice versa. Data Binding is often used between two different languages in our example XML and android. The Data Binding library provides all the necessary methods to import data and variables into the XML.

    The advantages of data binding is a less confusing activity code and also avoiding Null pointer because of calling views which are not created or already destroyed by the framework.

    In this example you see how to prepare the XML of an activity, to get ready for a object and helper Method.

    
    <data>
    <!-- Import helper Method (static)-->
    <import type="com.applications.startroutineactivity.helper.classes.Helper_MillisToString"/>
    <!-- Prepare variable-->
    <variable
    
    name="Activity_Day"
    
    type="com.applications.startroutineactivity.data_types_and_adapter.Obj_ActivityDay"/>
    
     </data>
    
    
    <!-- this is an example how to use the object in xml-->
    <TextView
    
    android:id="@+id/tv_TakeTime_CurrentShownDateDateFormat"
    
    android:layout_width="wrap_content"
    
    android:layout_height="wrap_content"
    
    android:layout_marginStart="8dp"
    
    android:layout_alignTop="@+id/tv_TakeTime_UserHint"
    
    android:clickable="true"
    
    android:enabled="true"
    
    android:hint="12.01.2018"
    
    android:onClick="oc_tv_TakeTime_CurrentShownDateDateFormat"
    
    android:text="@{Helper_MillisToString.getStringOutMillis(Activity_Day.getActivity_Day())}"
    
    android:textColor="@color/Mercedes_WHITE"
    
    android:textColorHint="@color/Mercedes_GREY"
    
    android:textSize="@dimen/dim_TextSize_Normal_Big"
    
    app:layout_constraintBottom_toBottomOf="@+id/bt_TakeTime_nextDate"
    
    app:layout_constraintStart_toEndOf="@+id/bt_TakeTime_lastDate"
    
    app:layout_constraintTop_toTopOf="@+id/bt_TakeTime_nextDate"/>
    
    

    Use this in your activity class:

    ActivityTaketimeBinding mainBinding; //This always refers to the layout file name "_" turns into upperCase
    
    //Use this in onCreate()
    
    mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_taketime);
    
    //Use this to set an object
    mainBinding.setActivityDay();//.setXXX is referring to the name of the variable in the XML file.
    
    

    To enable Data binding add this to your build.gradle file.

    dataBinding {
    
    enabled =true
    
    }
    
    buildToolsVersion '28.0.3'
    
    

    Sync, clear and rebuild. Also make sure to have installed all support packages from Sdk manager.

    Use this tutorial which is really nice.