Why this line xmlns:android="http://schemas.android.com/apk/res/android" must be the first in the layout xml file?
Asked Answered
K

13

181

Why is this line needed in xml layout file?

xmlns:android="http://schemas.android.com/apk/res/android" 
Knives answered 19/8, 2011 at 9:8 Comment(1)
It doesn't need to be on the first line ?Swain
F
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.

Finegrain answered 19/8, 2011 at 9:13 Comment(8)
Then why do we have android:layout_width instead of just layout_width?Supercargo
Then how come we use "LinearLayout" instead of "android:LinearLayout" ?Grivet
Probably because LinearLayout are part of the root namespace, and android:xxx attributes are part of a subnamespace ("schemas.android.com/apk/res/android/android/id")Finegrain
Are you sure it actually fetches this URL? It does not even exist. I guess the namespace is only to prevent naming collisions.Casebound
Of course it doesn't fetches this url, it's an URI : w3schools.com/xml/xml_namespaces.aspFinegrain
When i tried to change the name-space Uri from "schemas.android.com/apk/res/android/android" to "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.Theatrics
From 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.Contemplate
answer seems to be more of URI vs URL whereas I feel it should focus more on why this 'xmlns' is even required.Sloan
L
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
Loop answered 10/9, 2014 at 5:55 Comment(3)
this answer is really worth reading as compared to the above ones which did not clearly explain "WHY?"Sloan
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: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?Attaint
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?Poltroonery
P
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.

Pasquinade answered 19/8, 2011 at 9:15 Comment(0)
V
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.

Veteran answered 10/8, 2016 at 2:33 Comment(1)
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.Cloven
W
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

Wedlock answered 22/3, 2012 at 6:42 Comment(0)
A
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.

Archduchy answered 13/10, 2016 at 10:31 Comment(0)
C
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.

Clawson answered 7/9, 2014 at 8:38 Comment(0)
U
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".

Ungraceful answered 10/8, 2016 at 15:19 Comment(0)
V
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"
Venom answered 17/3, 2017 at 2:19 Comment(0)
O
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.

Officeholder answered 6/5, 2017 at 4:50 Comment(0)
N
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 .

Nagel answered 6/8, 2019 at 10:20 Comment(0)
F
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.

Felsite answered 2/5, 2021 at 10:23 Comment(0)
D
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.

Dormouse answered 20/1, 2017 at 6:28 Comment(1)
please elaborate it moreAidoneus

© 2022 - 2024 — McMap. All rights reserved.