Basic XML for Android Devs

Sanjay S
2 min readJan 14, 2021

--

Hey Android Devs đź‘‹, here I am going to explain the basic syntax of XML as a Android Developer to know.

Let’s start….

Introduction:

XML stands for eXtensible Markup Language… What is this mean? It is a markup (Designing or skeleton) language that define the data structure…

XML is also used in data transfer in APIs.

Tags:

In XML, there is two types of tags.

  1. Normal tag
  2. Self closing tag

Normal tag:

<MyTag> …children… </MyTag>

It has both opening tag and closing tag

Self closing tag:

<MySelfTag />

Since the name suggests it contains no closing tag… It is useful when there is no children.

Attributes:

Tags can have multiple attributes..

Attributes consists of two components:

  • Attribute key
  • Attribute value (it must be wrapped in double quotes)
  • Namespace (optional in xml, but required in android layouts)
<MyTag
namespace:key_one="value1"
namespace:key_two="value2">
</MyTag>

Note: Each key must be unique

These are the basic things in XML.. Now let’s dive in to The Android World.

In Android, each tag represents a View or ViewGroup class.
If the attribute is provided by Android, it must have namespace android: .
Each Tag must have two specific attributes layout_width and layout_height .

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />

Note: Value of layout_width and layout_height may be match_parent | wrap_content | values in units .

If the Tag is a View, write that tag in self closing tag, else in normal tag

Refer this Video:

Thank you reading my post… See you in next story…

Original Post: https://sanjaydev.tech/blog/basic-xml-for-android-devs-91b00f7713d0

--

--

Sanjay S
Sanjay S

Written by Sanjay S

Google Certified Associate Android Developer

No responses yet