Android "tools" namespace in layout xml documentation

Viewed 17411

Per the question here,

What's "tools:context" in Android layout files?

The 'tools' namespace reference (xmlns:tools="http://schemas.android.com/tools") has begun to appear in my layouts recently, and I want to know more. The original post only described the 'tools:context' attribute, but I have also noticed usage of the "tools:listitem" attribute appearing when I have designated a preview layout item for a listview, i.e.

<ListView
    android:id="@+id/lvCustomer"
    tools:listitem="@layout/customer_list_item" >
</ListView>

Are there more elements?

What brought me to this 'tools' namespace is that I want to be able to have 'preview-only' text (i.e. in a TextView or EditText) when using the layout designer in eclipse.

Currently, I assign the 'text' or 'hint' property for previewing text when arranging my layouts... but then I always have to remember to clear the preview value from within the code.

Ideally, instead of

<string name="preview_customer_name">Billy Bob's Roadhouse Pub</string>

...

<TextView
    android:id="@+id/tvCustomerName"
    android:text="@string/preview_customer_name"
</TextView>

have a something like:

<TextView
    android:id="@+id/tvCustomerName"
    tools:previewText="@string/preview_customer_name"
</TextView>

Thanks-

4 Answers

See in layout XML below. There are 2 namespace in use "xmlns:android" and "xmlns:tools".

Tools namespace is used when the developer wants to define placeholder content that is only used in preview or in design time. Tools namespace is removed when we compiled the app.

So in the code below, I want to show the placeholder image (image_empty) that will only be visible at design time, and image1 will the actual image that will be shown when the application launch

enter image description here

Related