Things You May Not Know: Tools Attributes

You may have seen an extra namespace, xmlns:tools="http://schemas.android.com.tools", in layout files generated by Android Studio. Adding this namespace to your layout provides you access to attributes, the Tools Attributes, that add metadata to resource files, that facilitate manipulating, rendering, and testing layouts in Design view, and (while I do not condone excess of this kind of behavior) that suppress certain Lint warnings (but only if you have a good reason for ignoring them, she nags).

If these sound handy to you (they absolutely are) and you want to just get started, just go on ahead to the Android Tools Project Site and check out the documentation.



Still here? Awesome. To start we will talk about…

Designtime Layout Attributes

Designtime layout attributes are basically overrides of normal layout attributes that allow you to change the rendering of those layouts in Android Studio without changing them in the build. Here is an example:

 
 

The root FrameLayout adds the Tools namespace and the TextView has this attribute set: tools:text="Unable to load camera list." If you go to the Design view or Android Studio's Preview tab, you will see something this:

Okay, I know. Same thing happens when you just set the android:text attribute on TextView.

So what's the big deal? Well, since value of tools:text overrides the value of android:text (but only in Design view) you can feel free to put funny/weird/edge-case-esque/in-joke-y/whatever test text without worrying about it accidentally showing up at runtime. You are on your own for code reviews.

 
 

I sometimes use it to quickly test how a layout handles ridiculously long strings (that sometimes just pop up in test or production data when you least expect it).

Often the text in a TextView is not determined until runtime (e.g., text received from a service call). In your layout you might set android:text to placeholder text to help check styling or position, but you will have to remember to take it out before building or change the android:visibility or android:text at runtime. I am so not a big fan of this. Instead, you can just use tools:text for your placeholder.

Visibility is another great use of designtime attributes. If your layout has views that alternate visibility, you can quickly verify those changes in layout, again, without worrying about forgetting test code that shows up at runtime.

 
 

According to the documentation:

In general, you can set any Android framework attribute as a designtime attribute; just use the tools: namespace rather than the android: namespace.

The documentation for designtime attributes goes into more detail on usage and limitations.

Layout Pointers

Tools attributes can also assist Design view in rendering a layout that contains or is included by other layouts.

If you have a layout with a <merge> root tag, Design view is not much help. To be fair, without a proper root Design view has no idea how to layout the child views (neither would you). However, you can assist it by adding the tools:showIn tag and specifying a layout where you have included the <merge>. As an example here's a <merge> layout:

 
 

And here is a layout that includes it:

 
 

Without the tools:showIn tag, Design View would display something like this:

But with tools:showIn, Design view displays this:

I know. My layout is ugly and simple. But the point is that in the first Design View rendering, the title TextView is completely lost. In the second, we actually see how the <merge> layout would look in place. Also note that parts of the screen outside of the <merge> are grayed out. Neat.

Ignore Lint Warnings

Generally, I do not like ignoring Lint warnings, but (if you know that it does not apply and it is really bugging you) you can suppress a particular Lint warning by adding tools:ignore="NameOfLintWarning" to a tag in your layout. That Lint warning will be ignored for that tag and all of its descendants. You can add multiple Lint warnings as a comma-separated string:

 
 

Wrap Up

Navigate over to the tech docs for even more handy Tools attributes. I will finish up this round of blather with a few tips:

Caveat: Design view sometimes hiccups especially if you are on the canary channel (like me). I have not had much trouble with the designtime attributes but have had some with the layout pointers. For example, using tools:layout with a <fragment> tag has been a no-go.

Tip #1: If you want to use tools:ignore to silence Lint warnings, you may have to Google to figure out the exact text value for your exact warning.

Tip #2: A good person to include in your googling is Tor Norbye of the Android Tools team. You should also follow him on Google+ and/or Twitter for the low-down on this and other Android tools.


Edited 2015.06.19: Thanks, Douglas Drumond for proofreading. :)