38

When we write a xml based layout in Android we use the string "xmlns:android="http://schemas.android.com/apk/res/android".

From the books I read it says that this is a namespace, but why do we use this? The link doesn't even exist. So what is the use of this?

Vinoth
  • 5,687
  • 11
  • 44
  • 56

4 Answers4

29

From developer.android.com

xmlns:android

Defines the Android namespace. This attribute should always be set to "http://schemas.android.com/apk/res/android".

xmlns:android is for identification that this xml is used for android, not for other function.

Namespaces uniquely identify code/libraries. If I write an api that uses all the same names and such as the android api the only way to distinguish between my api and android api is to use the android namespace, or mine.

You can further refer http://en.wikipedia.org/wiki/XML_namespace

Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104
  • 2
    i mean, this is the *definition* of what the namespace does, but it doesn't touch on the *why*. Put it like this, if i put the namespace as blank, nothing works. Why? – bharal Feb 28 '18 at 23:12
20

You may want to do some general reading on XML. w3.org's XML documentation may be your best best.

In general though, namespaces are used so that you don't have collisions between element names.

For example, if I created an XML schema that defined the element <person> and my friend created a separate XML schema that defined an entirely different element that also happened to be named <person>, we could have a collision. If I tried to reference both elements in an XML file, there would be no way to tell which one was which. If we use namespaces, however, myNamespace:person is clearly different than myFriendsNamespace:person.

The standard for defining a namespace is to use a URI, which is why you see that in the Android definition. It doesn't necessarily mean the schema exists at that location, it means that the URI is the unique name for that namespace.

Pang
  • 9,564
  • 146
  • 81
  • 122
tschaible
  • 7,635
  • 1
  • 31
  • 34
  • i mean, this is the definition of what the namespace does, but it doesn't touch on the why. Put it like this, if i put the namespace as blank, nothing works. Why? – bharal Feb 28 '18 at 23:13
  • You just dont understand why we dont understand why namespaces are defined like this in xml. xmlns clearly means xml namespace. ok. Then we see a colon and android follows. THAT SEEMS TO HAVE DEFINED ANDROID AS THE NAMESPACE. And future code shows me that android is really a namespace, eg: android:icon android:minSdkVersion. So my problem is, why is the url looking thing apart of the code? Seems like nonesense to me. So please disabuse my mind. –  Jul 08 '19 at 11:45
4

As you are going to refer android define keys in your XML. so, resources will identify the key with the namespace that's why we define namespace in every Android XML file like:- "xmlns:android="http://schemas.android.com/apk/res/android".

now in XML you can refer the keys using the android tag like:-

<FrameLayout
        android:id="@+id/id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color">

Using the android namespace you are able to find the keys id,layout_width and layout_height.

For more information please refer here.

Community
  • 1
  • 1
VikasGoyal
  • 3,308
  • 1
  • 22
  • 42
1

It is true that the link doesn't exists..But the string we are using is for declaring the Namespace... In XML,namespaces are used to avoid naming conflicts. You can refer to this link for more clarification http://www.w3schools.com/XML/xml_namespaces.asp

Jaydeep Khamar
  • 5,975
  • 3
  • 32
  • 30