33

For example, in:

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

Do I need to put it?

R. Zagórski
  • 19,466
  • 5
  • 64
  • 89
Stokres
  • 655
  • 2
  • 7
  • 12
  • 2
    There's some good information along these lines in [What's “tools:context” in Android layout files?](http://stackoverflow.com/q/11078487/1267661) – Sam Mar 12 '13 at 17:52

3 Answers3

32

It defines the XML namespace of the document. You should put it, otherwise tags like <RelativeLayout> could be not recognied by the parser.

Namespaces are a way for XML documents to include tags from various vendors. By using xmlns attribute you declare, that, by default, you're using XML elements defined here: http://schemas.android.com/apk/res/android (note that this link is broken - this discussion explains why).

You also declare additional namespace, tools, which is not your default namespace, thus when referencing elements or attributes defined there, you must add tools prefix, on example:

tools:context=".SomeActivity"
kamituel
  • 32,634
  • 4
  • 77
  • 95
  • @Sam - That's because Google does not make it public. – kamituel Mar 12 '13 at 17:54
  • @Sam - see [this discussion](https://groups.google.com/forum/?fromgroups=#!topic/android-developers/2FtsLQ4wESY) to see why those schemas are not available. – kamituel Mar 12 '13 at 18:02
  • Did you do a ninja edit? I swear that was a different link attached to a different sentence... maybe I just zoned out. I agree, schemas are not required to be valid links, just consistent. – Sam Mar 12 '13 at 18:10
  • @Sam - I did, to update answer with link to the discussion (i also fixed a link) - but the exact link doesn't matter, because all xmlns's from Android aren't defined. – kamituel Mar 12 '13 at 18:14
  • Just update, you can find tool attributes explanations here http://tools.android.com/tech-docs/tools-attributes – peter_budo Aug 03 '14 at 13:55
  • This actually doesn't answer about `tools`. This answer is for `xmlns:android` – OneCricketeer Apr 15 '17 at 06:33
  • 2
    @kamituel The link to the discussion does not work anymore either. – user3738870 Jun 05 '19 at 11:56
  • [https://developer.android.com/studio/write/tool-attributes](https://developer.android.com/studio/write/tool-attributes) - Updated link – Sujay Feb 19 '20 at 10:00
5

Following is a useful link from Android dev portal: https://developer.android.com/studio/write/tool-attributes.html

It says

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.

i.e. tools namespace helps designing UI and all attributes with prefix 'tools' will be removed at build time.

Jose
  • 2,129
  • 1
  • 18
  • 16
1

In fact, when you do :

<RelativeLayout android:id> </RelativeLayout>

Instead of calling android:id, the xml will call http://schemas.android.com/apk/res/android:id . It just the page that declare all the attribute and views that you can use in your xml.

Here is an explanation. http://www.w3schools.com/xml/xml_namespaces.asp

Android
  • 415
  • 5
  • 14