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

AndroidXmlAndroid ActivityLayoutXml Namespaces

Android Problem Overview


Why is this line needed in xml layout file?

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

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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

Solution 3 - Android

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.

Solution 4 - Android

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.

Solution 5 - Android

>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

Solution 6 - Android

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

Solution 7 - Android

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.

Solution 8 - Android

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

Solution 9 - Android

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"

Solution 10 - Android

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.

Solution 11 - Android

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 .

Solution 12 - Android

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.

Solution 13 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHitesh DhamshaniyaView Question on Stackoverflow
Solution 1 - AndroidNitroG42View Answer on Stackoverflow
Solution 2 - AndroidDevrathView Answer on Stackoverflow
Solution 3 - AndroidMartynView Answer on Stackoverflow
Solution 4 - AndroidHarsh DaniView Answer on Stackoverflow
Solution 5 - Androiddinesh sharmaView Answer on Stackoverflow
Solution 6 - AndroidSwapnil KadamView Answer on Stackoverflow
Solution 7 - AndroidvikselnView Answer on Stackoverflow
Solution 8 - AndroidstudentView Answer on Stackoverflow
Solution 9 - AndroidSaleem KalroView Answer on Stackoverflow
Solution 10 - AndroidLilacView Answer on Stackoverflow
Solution 11 - AndroidRashid KalhoroView Answer on Stackoverflow
Solution 12 - AndroidShankerView Answer on Stackoverflow
Solution 13 - AndroidjeffView Answer on Stackoverflow