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

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

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.

117k 73 73 gold badges 321 321 silver badges 424 424 bronze badges answered Aug 19, 2011 at 9:13 5,356 2 2 gold badges 29 29 silver badges 32 32 bronze badges Then why do we have android:layout_width instead of just layout_width ? Commented Mar 1, 2013 at 20:14 Then how come we use "LinearLayout" instead of "android:LinearLayout" ? Commented Apr 30, 2013 at 12:21

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

Commented Apr 30, 2013 at 13:55 Of course it doesn't fetches this url, it's an URI : w3schools.com/xml/xml_namespaces.asp Commented Jul 15, 2013 at 9:42

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.

Commented Dec 28, 2015 at 8:03

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 ::

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

answered Sep 10, 2014 at 5:55 42.8k 55 55 gold badges 202 202 silver badges 309 309 bronze badges

this answer is really worth reading as compared to the above ones which did not clearly explain "WHY?"

Commented Mar 4, 2017 at 6: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: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?

Commented Jun 29, 2017 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?