Data binding attributes and event handler

Pankaj Rai 🇮🇳
AndroidPub
Published in
3 min readApr 2, 2017

Well databinding not only have a capability to avoid findViewById and setting directly from model class indeed it can help a lot in order to create custom attributes also. Think of some task like setting text font now it’s possible to set font directly using xml itself with the help of custom attribute. Not just this calling a method using method reference or by lambda expression is also very helpful.

How to set fonts?
First and foremost create a new class say util class and add a static method named setFont also annotate it with @bindingAdapter. This method will have two attributes first one is the type of view and second is the value to be set on that view.

@BindingAdapter("app:font")
public static void setFont(TextView textView, String fontName) {
Typeface typeface = Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/"+fontName);
textView.setTypeface(typeface);
}

In order to call this method in xml simply use app:font as an attribute and pass only the second argument that is name of the font, no need to pass the type of view as its automatically handled by data binding.

<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="This is my text."
app:font="@{`Macondo-Regular.ttf`}"
/>

Note: Do remember to use special symbol while using direct string its not a single quote. Best is to put the name of font in string.xml and then use it like @string/font_name.

Now the final step is set the contentView in your activity class using DataBindingUtil and you will get your desired font on screen.

DataBindingUtil.setContentView(this, R.layout.activity_main);

Event Handler
One the great advantages of data binding is event handler. You can create your own handler which gets trigger when an event occurs like onClickListener. If you do some particular work on number of views when an event occurs then its best to create a custom method and call that method using method reference or by lambda expression directly from xml.
In order to do that first create a new class say Presenter and write your own custom public method with first paramater as type of view or view directly its your choice then call that method in android:onClick or with the attributes you want that methods to get called.

<data>
<variable
name="presenter"
type="com.example.databinding.Presenter"
/>
</data>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:onClick="@{(view)->presenter.showToast(view)}"
android:text="Checking this font style"
app:font="@{@string/font_name}"
/>
public class Presenter {
public void showToast(View view) {
Toast.makeText(view.getContext(), "My toast message",Toast.LENGTH_SHORT).show();
}
}

Note: Here I used lambda expression to call the method, same thing can be achieved using method reference also.

Most important point to be remember is that just like setting a model in your activity class you do have to set your presenter class also otherwise none of the method in the presenter class will get called.

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setPresenter(new Presenter());

Just like that you can create any method and fabricate it based on your usage. It’s always fun to create our own attributes and methods which acts like predefined attributes and methods.

--

--

Pankaj Rai 🇮🇳
AndroidPub

Software Engineer | GDE Android & Firebase | YouTuber — All Techies