Part 2. XML Basics

Part 2. XML Basics

xmlns attribute in detail

By looking at the default structure of XML file we'll be discussing attributes.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" />
</LinearLayout>

What is this xmlns ?

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

What is URI?

URI (Uniform Resource Identifier) is sequence of the characters used to identify resources uniquely over the internet.

  • XML namespace are used for providing uniquely named elements and attributes in an XML documents.
  • xmlns:android describes then android namespace.
  • Also suppose we write our own TextView widget with different features compared to android textview, android namespace helps to distinguish between our customer textview widget and android textview widget.
  • Without xmlns:android="http://schemas.android.com/apk/res/android" android related tags won't be recognised in the xml document of our layout.

Important Arributes

  • android id
  • android:layout_height (required)
  • android:layout_width (required)
  • android:text