Hitesh Dhamshaniya Hitesh Dhamshaniya - 3 months ago 32
Android Question

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

Why this line is needed in xml layout file ?

Can anybody give me an explanation?

I tried to understand, but not succeeded, so I post this question here.

Answer

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.