Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Android Data Binding, how to specify a dummy (or "default") text to be displayed during layout preview in Android Studio?

Can I specify a string to be shown in the layout preview in Android studio, e.g. for a TextView? ( Lorem ipsum ...)

It would be very helpful to be able to do that to see some text in the layout editor, e.g. to see if the textsize is correct etc.

like image 687
treesAreEverywhere Avatar asked Apr 05 '16 13:04

treesAreEverywhere


2 Answers

Found the answer myself just now:

Option 1:

android:text="@{user.name, default=JOHN_DOE}"

As explained at the very bottom of this page:

http://developer.android.com/intl/es/tools/data-binding/guide.html

Drawback: Not sure how to use strings with spaces.

Option 2

http://tools.android.com/tips/layout-designtime-attributes

1.) Add tools namespace in your root node:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

2.) Add a tools:text attribute:

 <TextView
        tools:text="John Doe"

Drawback: You need to add the tools namespace to your root node.

like image 195
treesAreEverywhere Avatar answered Oct 25 '22 04:10

treesAreEverywhere


You need to use tools:text

Add the tools namespace to your root element.

xmlns:tools="http://schemas.android.com/tools"

Then the tools:text attribute as below.

enter image description here

Whether you are using DataBinding or not it doesn't matter, tools:text will work to see text during layout preview.

For more info see https://developer.android.com/studio/write/tool-attributes

like image 45
Ravi Avatar answered Oct 25 '22 04:10

Ravi