Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A xmlns:android, how does that actually work?

Tags:

android

xml

Consider the following perfectly well formed layout file on android:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, PurpleActivity"/>
</LinearLayout>

I looked at this and got curious about that ULR: http://schemas.android.com/apk/res/android. So, I tried to enter it into my web browser's address bar and also tried to fetch it via CURL, but both methods showed that it doesn't actually exist. I looked for something akin to an apk/res/android directory in the android-sdk folder on my local machine, but that searched turned up nothing as well. So, I figured since this resource didn't actually exist, maybe that I could change the xmlns:android line to whatever I wanted. I went ahead and changed it to

http://schemas.android.com/apk/res/androidASDFASDFASDFASDFLKJSDFLEIE

and then tried to recompile my project. I got this result:

-resource-src:
     [echo] Generating R.java / Manifest.java from the resources...
     [aapt]     (skipping hidden file '/Users/rutski/Desktop/purple/res/layout/.#main.xml')
     [aapt] /Users/rutski/Desktop/purple/res/layout/main.xml:2: error: No resource identifier found for attribute 'orientation' in package 'androidASDFASDFASDFASDFLKJSDFLEIE'
     [aapt] /Users/rutski/Desktop/purple/res/layout/main.xml:2: error: No resource identifier found for attribute 'layout_width' in package 'androidASDFASDFASDFASDFLKJSDFLEIE'
     [aapt] /Users/rutski/Desktop/purple/res/layout/main.xml:2: error: No resource identifier found for attribute 'layout_height' in package 'androidASDFASDFASDFASDFLKJSDFLEIE'
     [aapt] /Users/rutski/Desktop/purple/res/layout/main.xml:7: error: No resource identifier found for attribute 'layout_width' in package 'androidASDFASDFASDFASDFLKJSDFLEIE'
     [aapt] /Users/rutski/Desktop/purple/res/layout/main.xml:7: error: No resource identifier found for attribute 'layout_height' in package 'androidASDFASDFASDFASDFLKJSDFLEIE'
     [aapt] /Users/rutski/Desktop/purple/res/layout/main.xml:7: error: No resource identifier found for attribute 'text' in package 'androidASDFASDFASDFASDFLKJSDFLEIE'

This leaves me confused. If the resource doesn't exist anyway, then what does it matter if I change the xmlns:android value to some other nonexistent resource? And if it does exist, then where is it?

like image 274
fieldtensor Avatar asked Oct 09 '11 16:10

fieldtensor


People also ask

What does Xmlns android do?

xmlns: Android namespace is used in order to access and use the attributes which are provided by Android platform. xmlns:app( or xmlns:'customname') namespace is used to access the custom attributes which are defined in the application scope.

What is xmlns in xml android?

In an XML file, namespaces are used to provide elements and attributes with distinctive names. Names for elements or attributes in an XML instance may come from different XML vocabularies.

What are namespaces in android?

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. i mean, this is the definition of what the namespace does, but it doesn't touch on the why.

What is the purpose of the Activity_main xml file in the project you created?

Choose as many answers as you see fit. What is the purpose of the activity_main. xml file in the project you created? It provides the theme settings for your app.


1 Answers

xmlns:android="http://schemas.android.com/apk/res/android" declares an XML namespace prefix. xmlns indicates it's about a namespace declaration. android is the prefix. http://schemas.android.com/apk/res/android is a URI - a string that uniquely identifies which namespace is referred to. The URI can be something abstract (as in this case); it is not necessarily a URL that physically locates something.

In general, you are free to choose the actual prefix. You can most likely replace android with something else and it will still work.

So what that attribute actually means is "We need to work with nodes from the namespace http://schemas.android.com/apk/res/android here, and in this file, we're going to refer to that namespace as android.

If, in several files, you define different namespace prefixes using the same URI, all those files will be referring to the same namespace - because the URI is the same. The prefix can be seen a a shorthand notation for referring to the actual namespace. The prefix can only be used inside the file that defines it.

like image 155
Dabbler Avatar answered Oct 05 '22 17:10

Dabbler