Tips ‎ > ‎ Designtime Layout Attributes This page is obsolete. Redirecting to https://developer.android.com/studio/write/tool-attributes.html









As of Android Studio 0.2.11, the layout rendering (used in both the layout editor as well as the XML editor layout preview window), supports designtime layout attributes.

These are attributes which are used when the layout is rendered in the tool, but have no impact on the runtime. This is useful if you for example want to put sample data in your textfields for when you are editing the layout, but you don't want those attributes to affect your running app.

To use designtime attributes, first make sure you have the tools namespace defined in your layout:

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

The tools namespace is a specially recognized namespace by the Android tools, so all the attributes you define on view elements in the tools-namespace will be automatically stripped when the application is packaged and there is no runtime overhead.

Then, to for example set the text of a text field, use the same attribute as you would from the Android framework, but use the tools: namespace rather than the android: namespace:

<TextView android:text="Name:" android:layout_width="wrap_content" android:layout_height="wrap_content" />

<EditText tools:text="John Doe" android:layout_width="wrap_content" android:layout_height="wrap_content" />

In the above, the Name label is using the normal text attribute, and will be shown at runtime. However, the text field is using a designtime attribute, so it will appear in the tool, but not at runtime.

In general, you can set any Android framework attribute as a designtime attribute; just use the tools: namespace rather than the android: namespace. Note also that you don't have to choose either/or; you can set both the Android namespace attribute (which will be used at runtime) and a tools attribute (which will override the runtime attribute at designtime only).

You can also use designtime attributes to unset an attribute while in the tools. For example, there is a bug ( http://b.android.com/58448) that you cannot use the fastScrollAlwaysVisible attribute on ListViews in the layout editor. However, you may still want that attribute set at runtime. With designtime attributes you can work around it like this: <ListView android:id="@+id/listView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:fastScrollAlwaysVisible="true" tools:fastScrollAlwaysVisible=""/>

Here's another example; we have a FrameLayout with multiple children, and at designtime we only want to see one of them, let's say the second child: We can use the tools:visibility attribute: Here's another example; we have a FrameLayout with multiple children, and at designtime we only want to see one of them, let's say the second child: We can use the tools:visibility attribute:

<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="First" tools:visibility="invisible" />

<Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Second" tools:visibility="visible" /> (Instead of visibility="invisible" you may want to use visibility="gone" , depending on your needs.) Limitations Currently only overriding existing attributes is supported. We may want to define some additional convenience attributes to make it simple to for example choose which child in a ViewFlipper to show etc.

You have to manually edit in your designtime attributes at this time

They do not appear as an option in for example the layout editor property sheet.



Editor code completion does not help you enter these attributes; the easiest way to use them is to first enter them in the Android namespace, and when done replacing the prefix.

Note that designtime attributes are supported only for layout files themselves. You cannot use them anywhere else -- in menu XML files, in string resource definitions, etc.

Designtime attributes can only be used for framework resources, not custom attributes at this point.

See https://code.google.com/p/android/issues/detail?id=46186 for background or additional requests or comments. tools: namespace, see For information about other attributes in thenamespace, see Tools Attributes The tools namespace is a specially recognized namespace by the Android tools, so all the attributes you define on view elements in the tools-namespace will be automatically stripped when the application is packaged and there is no runtime overhead. As of Android Studio 0.2.11, the layout rendering (used in both the layout editor as well as the XML editor layout preview window), supports