Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android XML layout files and namespace

Tags:

Android layouts are defined in XML with this namespace declared in the root element:

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

Example of an element:

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" />
  1. Why is the android prefix used instead of ommitting it like xmlns="http... ?
  2. Why is the prefix used only in attributes and not in elements?
like image 492
fhucho Avatar asked Mar 23 '11 11:03

fhucho


People also ask

What is namespace in Android XML?

An XML Namespace has the properties: Namespace URI: Namespace name expressed as a URI to which the prefix is bound. prefix: syntactically, this is the part of the attribute name following the XMLConstants. XMLNS_ATTRIBUTE ("xmlns") in the Namespace declaration.

In which file Android layout files are stored?

Specifically, Android considers XML-based layouts to be resources, and as such, layout files are stored in the reslayout directory inside your Android project. Each XML file contains a tree of elements specifying a layout of widgets and containers that make up one View.

What is Android XML layout?

Using Android's XML vocabulary, you can quickly design UI layouts and the screen elements they contain, in the same way you create web pages in HTML — with a series of nested elements. Each layout file must contain exactly one root element, which must be a View or ViewGroup object.

What is contained within the layout XML file?

What is contained within the Layout xml file? Orientations and layouts that specify what the display looks like.


1 Answers

Interesting question! it sure feel a bit weird.

  1. It was a design choice by Google to be as strict as possible on namespace to handle errors at compile time.
  2. The prefix is not used on elements because theses are representing Java classes: com.android.widget.TextView (com.android.widget.* can always be truncated). The java namespace of this class will be automagically resolved at compile time, so an xml namespace representing a fully qualified java namespace is not welcome here. But attributes can be mapped to any of the inherited java classes of the Element. Hence the namespace on attributes to allow inheritance.

This is done like this mainly because the layout describes Java objects and Google here is using the XML namespace mechanism to help in mapping your Layout with Java objects. So there are collisions between the Java namespace world and XML namespace world. It also allow us developers to subclass elements, add our own attributes without worrying that the next version of the platform will maybe add an attribute with the same name.

See the two replies to this blog post by Dianne Hackborn, a well-known android engineer working at google: http://www.elharo.com/blog/software-development/xml/2008/09/16/android-xml-weirdness/

like image 131
pcans Avatar answered Oct 31 '22 04:10

pcans