Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
A Complete Guide to Learn Kotlin For Android App Development
Next article icon

A Complete Guide to Learn XML For Android App Development

Last Updated : 17 Oct, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     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:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     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"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     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"?> <shape xmlns:android="http://schemas.android.com/apk/res/android">     <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> 

Next Article
A Complete Guide to Learn Kotlin For Android App Development
author
adityamshidlyali
Improve
Article Tags :
  • Android

Similar Reads

  • 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 A
    10 min read
  • 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
    8 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 t
    9 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
  • 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 Androi
    7 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
    7 min read
  • App Development for Android in 2017: Challenges and Solutions
    Since Android OS took over the world, it’s hard to imagine the outsourcing niche without a mobile app development. Usually, the launching of the app is a quite time-consuming process. When a company decides to make an enterprise application, it is needed here and now. That’s why the mechanism of bui
    10 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
    3 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 n
    15+ min read
  • Best Way to Become Android Developer – A Complete Roadmap
    Android is an open-source operating system, based on the Linux kernel and used in mobile devices like smartphones, tablets, etc. Further, it was developed for smartwatches and Android TV. Each of them has a specialized interface. Android has been one of the best-selling OS for smartphones. Android O
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences