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

Hitesh Dhamshaniya picture Hitesh Dhamshaniya · Aug 19, 2011 · Viewed 201.2k times · Source

Why is this line needed in xml layout file?

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

Answer

NitroG42 picture NitroG42 · Aug 19, 2011

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.