Skip to content Skip to sidebar Skip to footer

Android Studio Design Time Data Binding Fallback/Default Value

I am using Android data binding which works great:

Solution 1:

You should read the Data Binding Guide posted on the Android developers website. The last section of the document, Android Studio Support for Data Binding explain how you can use a placeholder that can help you during the design phase. It's very simple:

<TextView android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@{user.firstName, default=Placeholder}"/>

If you want to have text with spaces as placeholder you can use single quotes ('), back quotes (`) or &quot;

android:text='@{user.firstName, default="Placeholder text"}'
android:text="@{user.firstName, default=`Placeholder text`}"
android:text="@{user.firstName, default=&quot;Placeholder text&quot;}"
android:text="@{user.firstName, default=@string/placeholder_text}"

Solution 2:

The Preview pane displays default values for data binding expressions.

android:text="@{user.firstName, default=PLACEHOLDER}"

This can set default value .

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="@{defaultString ?? @string/hello_world}"/>

Solution 3:

If you want to see default value only at designtime, consider using Designtime Layout Attributes. This is useful if you for example want to put incorrect values (e.g. too long values) and see how it rendering, but you don't want to see it at runtime.

Of course, you can use both techniques - default placeholders (mentioned by @Bandreid) and designtime attributes. In this case you will see designtime attributes at designtime and placeholders at runtime.


Post a Comment for "Android Studio Design Time Data Binding Fallback/Default Value"