Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android "tools" namespace in layout xml documentation

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-

like image 846
J Webb Avatar asked Jul 25 '12 01:07

J Webb


People also ask

WHAT IS tools in Android XML?

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).

What is namespace Android Studio?

Namespace URI: Namespace name expressed as a URI to which the prefix is bound. prefix: syntactically, this is the part of the attribute name following the XMLConstants. XMLNS_ATTRIBUTE ("xmlns") in the Namespace declaration. All get*(*) methods operate in the current scope for Namespace URI and prefix resolution.

What is Android XML layout?

Using Android's XML vocabulary, you can quickly design UI layouts and the screen elements they contain, in the same way you create web pages in HTML — with a series of nested elements. Each layout file must contain exactly one root element, which must be a View or ViewGroup object.

What are layout attributes?

Attributes of Layout in Androidandroid:id: It uniquely identifies the Android Layout. android:hint: It shows the hint of what to fill inside the EditText. android:layout_height: It sets the height of the layout. android:layout_width: It sets the width of the layout.


2 Answers

We've just added support for designtime attributes like this in Android Studio 0.2.11. See http://tools.android.com/tips/layout-designtime-attributes for more.

like image 123
Tor Norbye Avatar answered Sep 22 '22 23:09

Tor Norbye


Think of them as design time helpers only.They do not get processed in actual view rendering at run time.

For example you want to set background of some view in your layout design when working on android studio so that you can make clear distinction where that particular view is.So you would normally do that with

android:background="@color/<some-color>" 

Now risk is that sometimes we forget to remove that color and it gets shipped in apk. instead you can do as follows:

tools:background="@color/<some-color>" 

These changes will be local to android studio and will never get transferred to apk.

And also check out http://tools.android.com/tech-docs/tools-attributes for more options.

like image 21
Gagandeep Singh Avatar answered Sep 22 '22 23:09

Gagandeep Singh