Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Design Time Data Binding Fallback/Default value

I am using Android data binding which works great:

<TextView
android:text="@{ee.Name}"
...

But if I do that the Android Studio designer doesn't show any text. Without text I can't see the TextView at all. Which is understandable because I haven't bound the data yet. Is there something like a fallback value or a default value which can be displayed until there is real data?

like image 396
ehmunnehm Avatar asked Dec 13 '15 15:12

ehmunnehm


2 Answers

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}"
like image 115
Bandreid Avatar answered Oct 16 '22 23:10

Bandreid


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}"/>
like image 15
qinmiao Avatar answered Oct 16 '22 22:10

qinmiao