Android + Data Binding @style

Although @bwhite is correct, there may be workarounds you can do. It depends what you need to conditionally change. For instance, if you want to change the font based on the condition (which I needed to do), you can do it by making a custom binding adapter.

In other words, doing something like this:

public class FontBindingAdapter {

    @BindingAdapter({"bind:font"})
    public static void setFont(TextView textView, String typefaceName){
        Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
        // You'd probably want to actually use `typefaceName` to determine the font to use 
        textView.setTypeface(typeface);
    }

Then in your layout, like this:

<TextView
   app:font="@{some_condition ? @string/typeface_string_name_bold: @string/typeface_string_name_bold_light}"

I used this in my code, based on a great post: https://plus.google.com/+LisaWrayZeitouni/posts/LTr5tX5M9mb


The data binding unfortunately is not supported for styles: https://code.google.com/p/android-developer-preview/issues/detail?id=2613

Tags:

Android