attributes: xmlns:android. Defines the Android namespace. This attribute should always be set to " http://schemas.android.com/apk/res/android ". package. The value of the package attribute in the APK's manifest file represents your app's universally unique application ID.
The xmlns attribute specifies the xml namespace for a document. Note: The xmlns attribute is required in XHTML, invalid in HTML 4.01, and optional in HTML5.
Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources).
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):
Ex:http://schemas.android.com/apk/res/android:id
is the URI here
XML Namespace:
xmlns:android
describes the android
namespace.textview
widget with different
features compared to android textview
, android namespace helps to
distinguish between our custom textview
widget and android
textview
widgetxmlns 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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With