Tools attributes reference

Android Studio supports a variety of XML attributes in the tools namespace
that enable design-time features (such as which layout to show in a fragment) or
compile-time behaviors (such as which shrinking mode to apply to your XML
resources). When you build your app, the build tools remove these attributes so
there is no effect on your APK size or runtime behavior.

To use these attributes, add the tools namespace to the root element of each
XML file where you'd like to use them, as shown here:

tools:targetApi

This attribute works the same as the
@TargetApi annotation in Java
code: it lets you specify the API level (either as an integer or as a code name)
that supports this element.

This tells the tools that you believe this element (and any children) will be
used only on the specified API level or higher. This stops lint from warning you
if that element or its attributes are not available on the API level you specify
as your minSdkVersion.

For example, you might use this because
GridLayout is available only on
API level 14 and higher, but you know this layout is not used for any lower
versions:

tools:locale

Intended for: <resources>

Used by: Lint, Android Studio editor

This tells the tools what the default language/locale is for the resources in
the given <resources> element (because the tools otherwise
assume English) in order to avoid warnings from the spell checker.
The value must be a valid
locale qualifier.

For example, you can add this to your values/strings.xml file (the default
string values) to indicate that the language used for the default strings is
Spanish rather than English:

Design-time view attributes

The following attributes define layout characteristics that are visible
only in the Android Studio layout preview.

tools: instead of android:

Intended for: <View>

Used by: Android Studio layout editor

You can insert sample data in your layout preview by using the tools: prefix
instead of android: with any <View> attribute from the Android framework.
This is useful when the attribute's value isn't populated until runtime but you
want to see the effect beforehand, in the layout preview.

For example, if the android:text attribute value is set at runtime or you want
to see the layout with a value different than the default, you can add
tools:text to specify some text for the layout preview only.

Figure 1. The tools:text attribute
sets "Google Voice" as the value for the layout preview

You can add both the android: namespace attribute (which is used at
runtime) and the matching tools: attribute (which overrides the runtime
attribute in the layout preview only).

You can also use a tools: attribute to unset an attribute only for the
layout preview. For example, if you have a FrameLayout with multiple children
but you want to see only one child in the layout preview, you can set one of
them to be invisible in the layout preview, as shown here:

When using the Layout Editor in
design view, the Properties window also allows you to edit some design-time
view attributes. Each design-time attribute is indicated with
a wrench icon next to the attribute name to distinguish it from
the real attribute of the same name.

tools:context

Intended for: Any root <View>

Used by: Lint, Android Studio layout editor

This attribute declares which activity this layout is associated with by
default. This enables features in the editor or layout preview that require
knowledge of the activity, such as what the layout theme should be in the
preview and where to insert onClick handlers when you make
those from a quickfix (figure 2).

Figure 2. Quickfix for the onClick attribute
works only if you've set tools:context

You can specify the activity class name using the same dot prefix as in
the manifest file (excluding the full package name). For example:

tools:listitem / tools:listheader / tools:listfooter

Intended for: <AdapterView> (and subclasses like <ListView>)

Used by: Android Studio layout editor

These attributes specify which layout to show in the layout preview for a list's
items, header, and footer. Any data fields in the layout are filled with
numeric contents such as "Item 1" so that the list items are not repetitive.

Now the layout preview shows this TextView layout as it appears inside the
activity_main layout.

tools:menu

Intended for: Any root <View>

Used by: Android Studio layout editor

This attribute specifies which menu the layout preview should show in the
app bar. The value can be one or more menu IDs,
separated by commas (without @menu/ or any such ID prefix and without
the .xml extension). For example:

"@tools:sample/*" resources

Intended for: Any view that supports UI text or images.

Used by: Android Studio layout editor

This attribute allows you to inject placeholder data or images into your view.
For example, if you want to test how your layout behaves with text, but you
don't yet have finalized UI text for your app, you can use placeholder text as
follows:

tools:shrinkMode

Intended for: <resources>

Used by: Build tools with resource shrinking

This attribute allows you to specify whether the build tools should use
"safe mode" (play it safe and keep all resources that are explicitly cited and
that might be referenced dynamically with a call to
Resources.getIdentifier())
or "strict mode" (keep only the resources
that are explicitly cited in code or in other resources).

The default is to use safe mode (shrinkMode="safe"). To instead use
strict mode, add shrinkMode="strict" to the <resources> tag as shown here:

tools:keep

Intended for: <resources>

Used by: Build tools with resource shrinking

When using resource shrinking to remove unused resources, this
attribute allows you to specify resources to keep (typically because they are
referenced in an indirect way at runtime, such as by passing a dynamically
generated resource name to
Resources.getIdentifier()).

To use, create an XML file in your resources directory (for example, at
res/raw/keep.xml) with a <resources> tag
and specify each resource to keep in the tools:keep attribute as a
comma-separated list. You can use the asterisk character as a wild card.
For example:

tools:discard

Intended for: <resources>

Used by: Build tools with resource shrinking

When using resource shrinking to strip out unused resources, this attribute
allows you to specify resources you want to manually discard (typically because
the resource is referenced but in a way that does not affect your app, or
because the Gradle plugin has incorrectly deduced that the resource is
referenced).

To use, create an XML file in your resources directory (for example, at
res/raw/keep.xml) with a <resources> tag
and specify each resource to keep in the tools:discard attribute as a
comma-separated list. You can use the asterisk character as a wild card.
For example: