What is data binding in android ?
DataBinding
It is a technique or pattern to connect a layout to an activity or fragment at compile time.Usually we use the findViewById to get a reference to Views every time we search for a view in this way after it has been created or recreated Android has to traverse the view hierarchy to find it for us at runtime. For a large and deep view hierarchy this can take a time and slow down app. To avoid this case we use this DataBinding technique.
The compiler generates the helper class, or binding class when the activity is generated. So, it's the instance of this binding class. Then it can access the view through this generated binding class without any extra overhead.
Data Binding - The Idea
- The big idea about data binding is to create an object that connects/maps/binds two pieces of distant information together at compile time, so that you don't have to look for it at runtime.
- The object that surfaces these bindings to you is called the Binding object. It is created by the compiler, and while understanding how it works under the hood is interesting, it is not necessary to know for basic uses of data binding.
How do we do this ?
We enable data binding by adding this code to the build.gradle(Module:app) section// Enables data binding.dataBinding { enabled = true }
Then, add the layout tag to your layout xml file
<?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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:orientation="vertical" android:paddingStart="@dimen/padding_general" android:paddingEnd="@dimen/padding_general"> <TextView android:id="@+id/name_text" style="@style/NameStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/name" android:textAlignment="center" /> </LinearLayout> </layout>
Then,
In your activity create a binding object that gets instantiated in onCreate()
private lateinit var binding : ActivityYourBinding
This binding object is like a layer of glue between a layout and its views and the data.
The type of binding, ActivityYourBinding class is created by the compiler specifically for this YourActivity. The name is actually derived from the layout file i.e.
activity_your.xml ------> ActivityYourBinding
class YourActivity : AppCompatActivity() { private lateinit var binding : ActivityYourBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // setContentView(R.layout.activity_your) binding = DataBindingUtil.setContentView(this, R.layout.activity_your) /* findViewById<Button>(R.id.done_btn).setOnClickListener { addNickname(it) } */ binding.doneBtn.setOnClickListener { addNickname(it) } } private fun addNickname(view: View) { /* val editText = findViewById<EditText>(R.id.nickname_edit) val nicknameTextView = findViewById<TextView>(R.id.nickname_text) */ binding.apply { nicknameText.text = binding.nicknameEdit.text invalidateAll() // in order to refresh UI with the new data we need to first invalidate all binding expressions nicknameEdit.visibility = View.GONE doneBtn.visibility = View.GONE nicknameText.visibility = View.VISIBLE } // Hide the keyboard. val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(view.windowToken, 0) } }
Note* : invalidateAll() // in order to refresh UI with the new data we need to first invalidate all binding expressions
DataBinding for Data
The real power of DataBinding is on Binding data. DataBinding is done in Data class so the data is directly available to the Views. This will make code simpler and sets things up to handle more complex cases later in App Development Process.
Let's do Data Binding in Data Class.
Steps:
1. We create a Data Class for names.
data class Person(
var name: String = "",
var nickName: String = ""
)
Instead of storing them as string resources, we make them proper data and then we'll bind that data to our views.
2. To let our layout know about the data, we create a data block in the XML file, then we add a variable block for our variable and then declare the variable inside variable.
- the variable needs a name : which is similar to name of Data class
- the type : a fully qualified name of the data class we just created.
<data>
<variable
name="Person"
type="com.niranjan.mycoolapp.Person" />
</data>
3. Now in the Layout file instead of using the string resources, we can reference the variable
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={Person.name}"
android:textSize="@dimen/textSize_30sp"
android:inputType="textPersonName"
android:hint="@string/what_is_your_name"
android:textAlignment="center"/>
This gives us the reference to the data but we also need to create the actual data.
4. Finally we have to update the binding object in your Fragment.
Comments
Post a Comment