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

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:

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


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 ofcharacters used to identify a name of a resource.
  • Such identification enables interaction with representations of theresource over a network, typically the World Wide Web, using specificprotocols.

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


XML Namespace:

  • XML namespaces are used for providing uniquely named elements andattributes in an XML document. xmlns:android describes the androidnamespace.
  • Its used like this because this is a design choice by google tohandle the errors at compile time.
  • Also suppose we write our own textview widget with differentfeatures compared to android textview, android namespace helps todistinguish between our custom textview widget and androidtextview widget


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.