181

Why is this line needed in xml layout file?

xmlns:android="http://schemas.android.com/apk/res/android" 
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Hitesh Dhamshaniya
  • 2,886
  • 3
  • 24
  • 31

13 Answers13

128

In XML, xmlns declares a Namespace. In fact, when you do:

<LinearLayout android:id>
</LinearLayout>

Instead of calling android:id, the xml will use http://schemas.android.com/apk/res/android:id to be unique. Generally this page doesn't exist (it's a URI, not a URL), but sometimes it is a URL that explains the used namespace.

The namespace has pretty much the same uses as the package name in a Java application.

Here is an explanation.

Uniform Resource Identifier (URI)

A Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource.

The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address. Another, not so common type of URI is the Universal Resource Name (URN).

In our examples we will only use URLs.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
NitroG42
  • 5,336
  • 2
  • 28
  • 32
  • 14
    Then why do we have `android:layout_width` instead of just `layout_width`? – Shashwat Mar 01 '13 at 20:14
  • 5
    Then how come we use "LinearLayout" instead of "android:LinearLayout" ? – android developer Apr 30 '13 at 12:21
  • Probably because LinearLayout are part of the root namespace, and android:xxx attributes are part of a subnamespace ("http://schemas.android.com/apk/res/android/android/id") – NitroG42 Apr 30 '13 at 13:55
  • Are you sure it actually fetches this URL? It does not even exist. I guess the namespace is _only_ to prevent naming collisions. – caw Jul 14 '13 at 00:50
  • 4
    Of course it doesn't fetches this url, it's an URI : http://www.w3schools.com/xml/xml_namespaces.asp – NitroG42 Jul 15 '13 at 09:42
  • When i tried to change the name-space Uri from "http://schemas.android.com/apk/res/android/android" to "http://schemas.android.com/apk/res/android/and" am getting a lint error saying "Unexpected namespace prefix "android" found for tag **Layout", this implies that 'android' is a prefix which is defined in the namespace and all prefixes are then followed up with an attribute and then a value for the attribute. The same is true in any XML Document. The ADK might be set to recognize the Schema for Android Layout XML Files. This needs further digging to find whether my hypothesis is true or false. – Sri Krishna Jan 05 '14 at 13:19
  • 1
    From https://danielmiessler.com/study/url_vs_uri/: "a URL is a type of URI. So if someone tells you that a URL is not a URI, he’s wrong. But that doesn’t mean all URIs are URLs. All butterflies fly, but not everything that flies is a butterfly. The part that makes a URI a URL is the inclusion of the “access mechanism”, or “network location”, e.g. http:/, ftp://, ldap://, telnet://, etc. – yonivav Dec 28 '15 at 08:03
  • answer seems to be more of URI vs URL whereas I feel it should focus more on why this 'xmlns' is even required. – eRaisedToX Mar 04 '17 at 06:16
46

To understand why xmlns:android=“http://schemas.android.com/apk/res/android” must be the first in the layout xml file We shall understand the components using an example

Sample::

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container" >    
</FrameLayout>

Uniform Resource Indicator(URI):

  • In computing, a uniform resource identifier (URI) is a string of characters used to identify a name of a resource.
  • Such identification enables interaction with representations of the resource over a network, typically the World Wide Web, using specific protocols.

Ex:http://schemas.android.com/apk/res/android:id is the URI here


XML Namespace:

  • XML namespaces are used for providing uniquely named elements and attributes in an XML document. xmlns:android describes the android namespace.
  • Its used like this because this is a design choice by google to handle the errors at compile time.
  • Also suppose we write our own textview widget with different features compared to android textview, android namespace helps to distinguish between our custom textview widget and android textview widget
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • 9
    this answer is really worth reading as compared to the above ones which did not clearly explain "WHY?" – eRaisedToX Mar 04 '17 at 06:19
  • You were telling like ,"Such identification enables interaction with representations of the resource over a network, typically the World Wide Web, using specific protocols. Ex:http://schemas.android.com/apk/res/android:id is the URI here" - But If i don't have internet connection ,How this android name space will interact with resources over a network? – kavie Jun 29 '17 at 14:07
  • Does Android allow developers to define their own schemas? For eg. I wrote a custom component that uses the XML parser to take menu.xml's item and build it's own custom UI. I would like to write a schema with my own set of declarations i.e. in menu folder, I want a schema where only the id, title, enabled, icon, iconTint, visible and enabled work and the IDE should not auto-complete to show other items such as actionLayout, menuCategory, etc. which my component doesn't support or require. How do I go about defining my own namespace/schema for a library? – Saifur Rahman Mohsin Sep 13 '20 at 11:46
30

xmlns refers to the XML namespace

When using prefixes in XML, a so-called namespace for the prefix must be defined. The namespace is defined by the xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".

Note: The namespace URI is not used by the parser to look up information.

The purpose is to give the namespace a unique name. However, often companies use the namespace as a pointer to a web page containing namespace information.

Martyn
  • 16,432
  • 24
  • 71
  • 104
9

This is just the XML Name Space declaration. We use this Name Space in order to specify that the attributes listed below, belongs to Android. Thus they starts with "android:"

You can actually create your own custom attributes. So to prevent the name conflicts where 2 attributes are named the same thing, but behave differently, we add the prefix "android:" to signify that these are Android attributes.

Thus, this Name Space declaration must be included in the opening tag of the root view of your XML file.

Harsh Dani
  • 91
  • 1
  • 2
  • Simple and clear. I don't know about other answers but your answer works like magnet to my confusion and my concept is clear now. – Stack Overflow Jun 21 '19 at 17:18
6
  • Xmlns means xml namespace.
  • It is created to avoid naming conflicts in the xml’s.
  • In order to avoid naming conflicts by any other way we need to provide each element with a prefix.
  • to avoid repetitive usage of the prefix in each xml tag we use xmlns at the root of the xml. Hence we have the tag xmlns:android=”http://schemas.android.com/apk/res/android
  • Now android here simply means we are assigning the namespace “http://schemas.android.com/apk/res/android” to it.
  • This namespace is not a URL but a URI also known as URN(universal resource name) which is rarely used in place of URI.
  • Due to this android will be responsible to identify the android related elements in the xml document which would be android:xxxxxxx etc. Without this namespace android:xxxxxxx will be not recognized.

To put in layman’s term :

without xmlns:android=”http://schemas.android.com/apk/res/android” android related tags wont be recognized in the xml document of our layout.

Swapnil Kadam
  • 4,075
  • 5
  • 29
  • 35
6

xmlns:android

Defines the Android namespace. This attribute should always be set to "http://schemas.android.com/apk/res/android".

refer https://developer.android.com/guide/topics/manifest/manifest-element#nspace

qtmfld
  • 2,916
  • 2
  • 21
  • 36
dinesh sharma
  • 3,312
  • 1
  • 22
  • 32
2

xmlns:android This is start tag for define android namespace in Android. This is standerd convention define by android google developer. when you are using and layout default or custom, then must use this namespace.

Defines the Android namespace. This attribute should always be set to "http://schemas.android.com/apk/res/android".

From the <manifest> element documentation.

qtmfld
  • 2,916
  • 2
  • 21
  • 36
vikseln
  • 458
  • 6
  • 8
2

In XML, element names are defined by the developer. This often results in a conflict when trying to mix XML documents from different XML applications. A user or an XML application will not know how to handle these differences. Name conflicts in XML can easily be avoided using a name prefix. When using prefixes in XML, a namespace for the prefix must be defined.The namespace can be defined by an xmlns attribute in the start tag of an element.The namespace declaration has the following syntax. xmlns:prefix="URI".

student
  • 21
  • 2
2
xmlns:android="http://schemas.android.com/apk/res/android"

This is form of xmlns:android ="@+/id". Now to refernce it we use for example

android:layout_width="wrap_content"
android:text="Hello World!"

Another xmlns is

 xmlns:app="http://schemas.android.com/apk/res-auto"

which is in form of xmlns:app = "@+/id" and its use is given below

 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintLeft_toLeftOf="parent"
Saleem Kalro
  • 1,046
  • 9
  • 12
2

I think it makes clear with the namespace, as we can create our own attributes and if the user specified attribute is the same as the Android one it avoid the conflict of the namespace.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
Lilac
  • 580
  • 1
  • 7
  • 25
1
xmlns:android="http://schemas.android.com/apk/res/android" 

xmlns : is xml name space and the URL : "http://schemas.android.com/apk/res/android" is nothing but

XSD which is [XML schema definition] : which is used define rules for XML file .

Example :

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_marginBottom="4dp"
   android:hint="User Name"
  />
</LinearLayout> 

Let me explain What Kind of Rules ? .

  1. In above XML file we already define layout_width for our layout now IF you will define same attribute second time you will get an error .
  2. EditText is there but if you want add another EditText no problem .

Such Kind of Rules are define in XML XSD : "http://schemas.android.com/apk/res/android"

little bit late but I hope this helps you .

Rashid Kalhoro
  • 340
  • 4
  • 14
1

Below important point is missing in above answers,

When we declare xmlns:android="http://schemas.android.com/apk/res/android” in the root of xml file, then all the attributes and tags that are already attached to this namespace will be imported .

So next time when we give android: then autocomplete list occurs.

Shanker
  • 864
  • 6
  • 24
0

It is a XML name space declaration in order to specify that the attributes that are within the view group that it's decelerated in are android related.

jeff
  • 11