Apps development for Android step by step

Appendix A: Android XML syntax – structure of XML tags and attributes

XML files are used to define layout of Android apps and store data. Tags decide what elements would be used for app interface (like strings, images and buttons), how they would look like and what values would they have.

The standard structure of XML tag looks like this:

If there isn’t any content (for instance other tags) you could finish tag in a simpler way just by adding “/” before “>”:

The number of attributes depends on the tag – some tags don’t have attributes at all, others could have many of them. Moreover, some attributes are necessary, other are optional. An attribute is a pair of its name and its value (but value could be empty  – then you just write attribute_name=“”). Almost all attributes in Android XML files has “android:” prefix (or namespace explained later).

What’s really helpful, Android Studio editor checks if you have used proper tags, proper attributes and that you closed tags. Common mistake is skipping signs: “”, =, <,> or /. For people tag without these signs is still easily readable, but our computers are really strict about rules (syntax).

On the other hand some characters are not allowed as XML values – this is < and & instead you should use &lt; and &amp;.

You could use some tags inside others, but it’s like… an onion. You have always close the inside tags before outside tag.

This correct:

This is wrong:

The structure could have many levels, but you always have to remember about an onion rule. The first (main) tag is called root element. This is a parent for all other child and subchild elements.

At the top of XML file you often find such a line:

This is a declaration that this data file is built according to XML standard version 1 and characters are encoded in UTF-8 format so you could use any international letter in data you could imagine.

The XML standard doesn’t define list of tags nor attributes. It’s because XML is universal standard for keeping all types of data. You could think of your own tag names for instance: <sizes>, <cars>, <sportdisciplines>… as well as your own attributes like <cars speed=”fast” color=”red”>. By the way XML tags are case sensitive so <cars> isn’t equal to <Cars>.

In some XML files at the top you could see another beginning:

Symbol xmlns stands for XML Namespace. This xmlns:android means that XML file is built according to tag names and rules established by Google for Android apps. The rules are defined in so called XML schema . In this case you can’t use your own tags or attributes, but only ones allowed by Google. If you try to add your own tag, you would see an error that it’s not allowed. You could not save nor run apps with errors. But in one XML file you could use various namespace. The xmlns:tools is another namespace used by Google. Mind that and are not real websites – these are just URIs (Uniform Resource Identifiers) needed by namespace declaration.

Check for list of XML tags and attributes allowed by Android schema.

If you are more deeply interested in XML, read XML Tutorial by

XML stands for eXtensible Markup Language. It’s used to store data (Photo credit by

XML stands for eXtensible Markup Language. It’s used to store data (Photo credit by