Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of xmlns:tools in Android XML layout?

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?

like image 452
Stokres Avatar asked Mar 12 '13 17:03

Stokres


People also ask

What is xmlns tools in Android?

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 Tools targetApi?

By adding tools:targetApi="m" to an element you tell the lint that the element won't be used on API level below 23 (M). See the attribute documentation. This tells the tools that you believe this element (and any children) will be used only on the specified API level or higher.

What is main XML in Android?

main. xml is not a magic name; it is just the arbitrary name of a layout, which is used by the program to describe how the presentation looks. You might just as well have called it my_great_layout.


3 Answers

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"
like image 89
kamituel Avatar answered Oct 02 '22 20:10

kamituel


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.

like image 35
Jose Avatar answered Sep 29 '22 20:09

Jose


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

like image 33
Android Avatar answered Oct 01 '22 20:10

Android