Share your knowledge

Friday 6 January 2017

Xml Basics to create a layout in Android

XML - eXtensible Markup Language:

Markup Language: Every opening tag will have it's corresponding closed tag.

Ex:
      <TextView>
  
      </TextView>

It can also be written as
  
 <TextView/> (Opening and closing tag are at one line)


XML Namespaces: XML Namespaces provide a method to avoid element name conflicts.

Ex:

     Below xml is used to store Suresh information

      <user>
     <name>Suresh</name>
     <city>Palasa</city>
     <phoneNumber>9898989898</address>
      </user>

      <user>
     <address>Srikakulam,Palasa</name>
     <age>23</age>
      </user>

Note: If these XML fragments were added together, there would be a name conflict. Both contain a <user> element, but the elements have different content and meaning. A user or an XML application will not know how to handle these differences.

Solving the name conflict using Prefix:

      <a:user>
     <a:name>Suresh</a:name>
     <a:city>Palasa</a:city>
     <a:phoneNumber>9898989898</a:phoneNumber>
      </a:user>

      <b:user>
     <b:address>Srikakulam,Palasa</b:name>
     <b:age>23</b:age>
      </b:user>

In the example above, there will be no conflict because the two <user> elements have different names.


XML Namespaces - The xmlns Attribute:

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"

     <root>
      <a:user xmlns:a="http://schemas.android.com/apk/res/android/">
     <a:name>Suresh</a:name>
     <a:city>Palasa</a:city>
     <a:phoneNumber>9898989898</a:phoneNumber>
      </a:user>

      <b:user xmlns:b="http://schemas.android.com/apk/res-auto">
     <b:address>Srikakulam,Palasa</b:name>
     <b:age>23</b:age>
     </b:user>
      </root>


In the example above:

The xmlns attribute in the first <user> element gives the a: prefix a qualified namespace.

The xmlns attribute in the second <user> element gives the b: prefix a qualified namespace.

When a namespace is defined for an element, all child elements with the same prefix are associated with the same namespace.

Namespaces can also be declared in the XML root element as below:

     <root
          xmlns:a="http://schemas.android.com/apk/res/android/"
            xmlns:b="http://schemas.android.com/apk/res-auto">


      <a:user xmlns:a="http://schemas.android.com/apk/res/android/">
     <a:name>Suresh</a:name>
     <a:city>Palasa</a:city>
     <a:phoneNumber>9898989898</a:phoneNumber>
      </a:user>

      <b:user xmlns:b="http://schemas.android.com/apk/res-auto">
     <b:address>Srikakulam,Palasa</b:name>
     <b:age>23</b:age>
     </b:user>
      </root>

The purpose of using an URI is to give the namespace a unique name. However, companies often use the namespace as a pointer to a web page containing namespace information.


User Interface In Android





No comments:

Post a Comment