The Wayback Machine - https://web.archive.org/web/20240919125604/https://www.geeksforgeeks.org/a-complete-guide-to-learn-xml-for-android-app-development/
Open In App

A Complete Guide to Learn XML For Android App Development

Last Updated : 17 Oct, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

XML stands for Extensible Markup Language. XML is a markup language much like HTML used to describe data. It is derived from Standard Generalized Markup Language(SGML). Basically, the XML tags are not predefined in XML. We need to implement and define the tags in XML. XML tags define the data and used to store and organize data. It’s easily scalable and simple to develop. In Android, the XML is used to implement UI-related data, and it’s a lightweight markup language that doesn’t make layout heavy. XML only contains tags, while implementing they need to be just invoked. 

A-Complete-Guide-to-Learn-XML-for-Android-App-Development

The simple syntax of XML is 

<tag_name>Hello World!</tag_name>

So in this article, there is a deep discussion on how to learn and understand what XML is for Android Development.

Basics of User Interface(UI)

Basically in Android XML is used to implement the UI-related data. So understanding the core part of the UI interface with respect to XML is important. The User Interface for an Android App is built as the hierarchy of main layouts, widgets. The layouts are ViewGroup objects or containers that control how the child view should be positioned on the screen. Widgets here are view objects, such as Buttons and text boxes. Considering the following simple example of the activity_main.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<!--ViewGroup1-->
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:orientation="vertical"
    android:padding="24dp"
    tools:context=".MainActivity">
  
    <!--TextView1 widget inside ViewGroup1-->
    <TextView
        style="@style/TextAppearance.MaterialComponents.Headline5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GEEKSFORGEEKS"
        android:textColor="@color/green_500" />
  
    <!--TextView2 widget inside ViewGroup1-->
    <TextView
        style="@style/TextAppearance.MaterialComponents.Headline6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Enter the user information"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
    <!--ViewGroup2-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:clipToPadding="false">
  
        <!--EditText1 widget inside ViewGroup2-->
        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter first name" />
  
        <!--EditText2 widget inside ViewGroup2-->
        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/editText1"
            android:hint="Enter last name" />
  
        <!--Button widget inside ViewGroup2-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/editText2"
            android:layout_alignParentEnd="true"
            android:text="Submit" />
  
    </RelativeLayout><!--End of ViewGroup2-->
  
</LinearLayout><!--End of ViewGroup1-->


Output UI:

In the above example of XML file, There are 2 view groups one is LinearLayout and another is RelativeLayout and the TextView1 and TextView2 is child widgets under the ViewGroup1 that is LinearLayout. EditText1, EditText2, and Button are the child widgets under ViewGroup2 that is RelativeLayout. ViewGroup2(RelativeLayout) is nested under the ViewGroup1 which produces the following hierarchy.

A Complete Guide to Learn XML for Android App Development

From the hierarchy, it’s been observed that every widget like EdtText, TextView, or Button is one of the View. These Views are contained inside the ViewGroup, like RelativeLayout, LinearLayout, FrameLayout, etc.

Different Types of XML Files Used in Android Studio

Different XML files serve different purposes in Android Studio. The list of various XML files in Android Studio with their purposes is discussed below.

1. Layout XML files in android

The Layout XML files are responsible for the actual User Interface of the application. It holds all the widgets or views like Buttons, TextViews, EditTexts, etc. which are defined under the ViewGroups. The Location of the layout files in Android is:

app -> src -> main -> res -> layout

The folder contains the layout files for respective activity, fragments. The basic layout of the is as follows for activity_main.xml:

XML




<?xml version="1.0" encoding="utf-8"?>
<!--ViewGroup1-->
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:orientation="vertical"
    android:padding="24dp"
    tools:context=".MainActivity">
  
    <!--TextView1 widget inside ViewGroup1-->
    <TextView
        style="@style/TextAppearance.MaterialComponents.Headline5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GEEKSFORGEEKS"
        android:textColor="@color/green_500" />
  
    <!--TextView2 widget inside ViewGroup1-->
    <TextView
        style="@style/TextAppearance.MaterialComponents.Headline6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Enter the user information"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
    <!--ViewGroup2-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:clipToPadding="false">
  
        <!--EditText1 widget inside ViewGroup2-->
        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter first name" />
  
        <!--EditText2 widget inside ViewGroup2-->
        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/editText1"
            android:hint="Enter last name" />
  
        <!--Button widget inside ViewGroup2-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/editText2"
            android:layout_alignParentEnd="true"
            android:text="Submit" />
  
    </RelativeLayout><!--End of ViewGroup2-->
  
</LinearLayout><!--End of ViewGroup1-->


2. AndroidManifest.xml file

This file describes the essential information about the application’s, like the application’s package names which matches code’s namespaces, a component of the application like activities, services, broadcast receivers, and content providers. Permission required by the user for the application features also mentioned in this XML file. Location of the AndroidManifest.xml file:

app -> src -> main

A typical code inside AndroidManifest.xml with internet permission looks like this:

XML




<?xml version="1.0" encoding="utf-8"?>
    package="com.adityamshidlyali.gfgarticle">
  
    <!--internet permission from the user-->
    <uses-permission android:name="ANDROID.PERMISSION.INTERNET" />
  
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.GFGArticle">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
  
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
  
</manifest>


3. strings.xml file

This file contains texts for all the TextViews widgets. This enables reusability of code, and also helps in the localization of the application with different languages. The strings defined in these files can be used to replace all hardcoded text in the entire application. Location of the strings.xml file

app -> src -> main -> res -> values

A typical code inside strings.xml looks like this:

XML




<resources>
    <string name="app_name">GFG Article</string>
    <string name="gfg_heading">GEEKSFORGEEKS</string>
    <string name="settings_label">settings</string>
</resources>


4. themes.xml file

This file defines the base theme and customized themes of the application. It also used to define styles and looks for the UI(User Interface) of the application. By defining styles we can customize how the views or widgets look on the User Interface. Location of styles.xml file

app -> src -> main -> res -> values

A typical code inside themes.xml looks like this:

XML




<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.GFGArticle" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/green_500</item>
        <item name="colorPrimaryVariant">@color/green_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>


5. Drawable XML files

These are the XML files that provide graphics to elements like custom background for the buttons and its ripple effects, also various gradients can be created. This also holds the vector graphics like icons. Using these files custom layouts can be constructed for EditTexts. Location for the Drawable files are:

app -> src -> main -> res -> drawable

A typical code inside my_custom_gradient.xml looks like this:

XML




<?xml version="1.0" encoding="utf-8"?>
    <gradient
        android:centerColor="@color/green_500"
        android:endColor="@color/green_700"
        android:startColor="@color/green_200" />
</shape>


which produces the following gradient effect:

6. colors.xml file

The colors.xml file is responsible to hold all the types of colors required for the application. It may be primary brand color and its variants and secondary brand color and its variants. The colors help uphold the brand of the applications. So the colors need to be decided cautiously as they are responsible for the User Experience. The colors need to be defined in the hex code format. Location of colors.xml file:

app -> src -> main -> res -> values

A typical code inside custom.xml looks like this:

XML




<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
  
    <!--custom colors-->
    <color name="green_200">#55cf86</color>
    <color name="green_500">#0f9d58</color>
    <color name="green_700">#006d2d</color>
</resources>


7. dimens.xml file

As the file name itself suggests that the file is responsible to hold the entire dimensions for the views. it may be the height of the Button, padding of the views, the margin for the views, etc. The dimensions need to in the format of pixel density(dp) values. Which replaces all the hard-coded dp values for the views. This file needs to be created separately in the values folder. Location to create dimens.xml file:

app -> src -> main -> res -> values

A typical code inside dimens.xml looks like this:

XML




<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="button_height">58dp</dimen>
    <dimen name="default_start_margin">16dp</dimen>
    <dimen name="default_end_margin">16dp</dimen>
    <dimen name="card_view_elevation">12dp</dimen>
</resources>




Similar Reads

A Complete Guide to Learn Kotlin For Android App Development
Kotlin is a statically typed, cross-platform, general-purpose programming language for JVM developed by JetBrains. This is a language with type inference and fully interoperable with Java. Kotlin is concise and expressive programming as it reduces the boilerplate code. Since Google I/O 2019, Android development has been Kotlin-first. Kotlin is seam
8 min read
A Complete Guide to Learn Android Studio For App Development
Before diving into the vast field of Android Development, there is a need to understand the tool required for it. The name of the tool is Android Studio, used for developing Android Applications. Android Studio is developed by Google and Jetbrains. It's the most widely used software for developing Android Applications. So in this article, there is
10 min read
Learn Java For App Development - A Complete Guide
Java is one of the powerful general-purpose programming languages, created in 1995 by Sun Microsystems (now owned by Oracle). Java is Object-Oriented. However, it is not considered as pure object-oriented as it provides support for primitive data types (like int, char, etc). Java syntax is similar to C/C++. But Java does not provide low-level progr
9 min read
Why Should You Learn Android App Development?
Nowadays, we have one thing which is always close to us, and it also tells about our interests, behavior, etc. It also makes our life easier in education, health, banking, and many other fields. But what is that? I hope you must have found the answer, yes, it is a smartphone. Earlier, mobile phones were mostly used to make calls, sometimes sending
6 min read
Guide to Install and Setup IntelliJ IDEA for Android App Development
To start developing Android applications, one has to set up a proper development environment. It facilitates developers to use the tools needed in creating an app and ensure that all operations/processes carried out in a smooth manner. An IDE(Integrated Development Environment) is a complete application suite that provides services like writing and
5 min read
Guide to Install and Setup Visual Studio for Android App Development
To start developing Android applications, one has to set up a proper development environment. It facilitates developers to use the tools needed in creating an app and ensure that all operations/processes carried out in a smooth manner. An IDE(Integrated Development Environment) is a complete application suite that provides services like writing and
4 min read
100 Days of Android Development: A Complete Guide For Beginners
In the dynamic world of Mobile application development mastering in developing applications is not just a skill but rather a journey. Welcome to the 100 days of Android Development journey where we will dive into different Android development important components day by day. The definition of Android Development of Android to the advanced Android c
7 min read
Master Android Development With Kotlin: A Complete Guide
We regret to inform you that the Android App Development with Kotlin – Live Course by GeeksforGeeks is currently unavailable. For information on related courses and opportunities, please click here. Thank you for your interest.
1 min read
How to draw Horizontal and Vertical lines in an Android App using XML
This article demonstrates how to draw a horizontal and a vertical line in an Android app. Although we have countless XML tags to meet almost all of our needs, unfortunately, we don't have any tag such as &lt;line&gt; tag to draw the line. However, &lt;view&gt; tag can be used in XML as a workaround. NOTE: The "view" tag is different than the View c
2 min read
How to Use Stack Overflow to Fix Errors During Android App Development in Android Studio?
Stack Overflow, the life savior of developers, feels like Stack Overflow is built for the developers, by the developers, of the developers. According to Wikipedia: "Stack Overflow is a question-and-answer site for professional and enthusiast programmers". So being a developer or programmer can you imagine your life without Stack Overflow…. ? Most p
4 min read
Top 10 Books For Android App Development
Do you have an interest in Android app development but don't know where to begin? What can be better than starting with man's best friend i.e. books, undoubtedly, friendship with good books can help you to become a good developer. The best thing about books is you can learn at your own pace. Before going to talk about these books, the first thing y
7 min read
Basic Prerequisites of Learning Android App Development
This day's craze of learning any technology is very high among students and Android development is one of the technologies. Nowadays almost everyone has a smartphone so the percentage of people using apps is high these days. So Android app development is an OnDemand technology. Students are also showing very interest to learning this technology. Bu
7 min read
6 Weeks of Android App Development - FREE Project Based Learning
Are you among the ones who want to learn Android App Development from the beginning? Are you someone who doesn't have any idea about Android App Development? Or you have just started your journey, but you don't know whether you are on the right path or not. So, during your initial journey, all you need is proper guidance. If you get your guide, the
15+ min read
How to Install and Setup Eclipse IDE For Android App Development?
Android Application Development can be done using Android Studio as well as Eclipse IDE. We can create android applications in Eclipse IDE using the ADT plugin. Eclipse is preferred for creating small android applications. Eclipse IDE is an open-source software used by developers, it contains a variety of plugins to develop software in different pr
3 min read
Android App Development with Kotlin: A Technical Overview
Android Kotlin app development is a popular way to create mobile applications for the Android platform. Kotlin is a modern programming language that is designed to be easy to use and understand, making it a great choice for developers who want to create apps quickly and efficiently. In this article, we will discuss the key concepts and best practic
7 min read
Welcome to The Modern Android App Development
With the spread of smartphones, smartphone technology improved exponentially and thus smartphones occupied a huge part of the market. To meet the requirement of the user and seeing the wide-open market there came different operating systems like Android, iOS, etc but for the sake of this article, we gonna talk specifically about how Android Develop
3 min read
10 Best Android App Development Courses For Beginners
All thanks to Google's ubiquitous Android platform, we're able to get everything done with just a click on our Android apps. Everyone today is investing in Android app development for the growth of their organization. The salaries of Android developers have increased by 10% in the last 5 years and it'll keep growing in recent years as well. And fro
11 min read
How to Integrate In-App Review API into Android App?
Once we publish our app over the PlayStore and as it became live there, app rating and reviews become very crucial to driving audience and downloads to your app. In order to increase this, we ask our users to rate the app through a popup window and redirecting them to the PlayStore. But this nowadays creates little problems, once users went to Play
3 min read
How to Share Text of Your App with Another App and Vice Versa in Android?
Most of the time while using an app we want to share text from the app to another app. While using Many Social Media Platforms we find this feature to be very useful when we want to share information from one app to another. The Android intent resolver is used when sending data to another app as part of a well-defined task flow. To use the Android
5 min read
How to Share Image of Your App with Another App in Android?
Most of the time while using an app we want to share images from the app to another app. While using Many Social Media Platforms we find this feature to be very useful when we want to share information from one app to another. The Android Intent resolver is used when sending data to another app as part of a well-defined task flow. To use the Androi
4 min read
Complete guide on How to build a Video Player in Android
This article explains the stepwise process as to how to build a Video Player using Android Studio. For viewing videos in android, there is a special class called "MediaPlayer". In this article, we will be having 2 videos which are connected by the "Dialog box", i.e a dialog box will come after completion of the first video which will ask the user w
3 min read
XML Parsing in Android using XmlPullParser
In android, the XMLPullParser interface provides the functionality to parse the XML files in android applications. The XMLPullParser is a simple and efficient parser method to parse the XML data when compared to other parser methods such as DOM Parser and SAX Parser. The XMLPullParser has a method next() that provides access to high-level parsing e
5 min read
XML Parsing in Android using DOM Parser
Android DOM(Document Object Model) parser is a program that parses an XML document and extracts the required information from it. This parser uses an object-based approach for creating and parsing the XML files. In General, a DOM parser loads the XML file into the Android memory to parse the XML document. This results in more consumption of memory.
6 min read
How to Create Drawable Resource XML File in Android Studio?
Prerequisite: Android Project folder Structure A drawable resource is a common concept for a graphic that can be drawn to the screen and which one can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon. There are several different types of drawable resources files a
3 min read
XML Parsing in Android using SAX Parser
Generally, XML (Extensible Mark-up Language) is a commonly used data exchange format to interchange servers' data. In Android, SAX stands for Simple API for XML and is a widely used API for XML parsing. Like the DOM parser, the SAX parser is also used to perform in-memory operations to parse the XML document, but it consumes less memory than the DO
6 min read
Processing and Parsing XML in Android
In this blog, we will learn about one of Android's most fascinating topics. Even most of us are unaware of it, despite the fact that it is one of the most essential ideas in Android development. But, before we get into our topic, take out your phones and count the number of apps you have on your smartphone. On average there must be more than 30 app
6 min read
XML Namespaces in Android
In an XML file, namespaces are used to provide elements and attributes with distinctive names. Names for elements or attributes in an XML instance may come from different XML vocabularies. The ambiguity between similar elements or attributes can be eliminated if each vocabulary is given its own namespace. An identifier for a uniform resource is a n
2 min read
Android Jetpack Compose - Interoperability Using Compose in XML Layouts
Writing UI in jetpack compose can be fun, but can be difficult to migrate your whole project into Compose at one time. Fortunately, Jetpack Compose provides Interoperability api to use compose in existing XML views so that you can migrate to compose slowly. Prerequisites: Knowledge of Jetpack Compose.Knowledge of KotlinKnowledge of Android’s View.
3 min read
Android | Running your first Android app
After successfully Setting up an Android project, all of the default files are created with default code in them. Let us look at this default code and files and try to run the default app created. The panel on the left side of the android studio window has all the files that the app includes. Under the java folder, observe the first folder containi
3 min read
How to Add Audio Files to Android App in Android Studio?
The audio file format is a file format for saving digital audio data on a computer system and all are aware of audio files. So in this article, we are going to discuss how can we add audio files to the android app. There are three major groups of audio file formats: Format Name Description Examples Uncompressed audio formatsUncompressed audio forma
3 min read
Article Tags :