{"id":2049,"date":"2016-06-07T06:30:02","date_gmt":"2016-06-07T06:30:02","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=2049"},"modified":"2018-06-05T12:08:29","modified_gmt":"2018-06-05T12:08:29","slug":"xml","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/xml","title":{"rendered":"XML in Android: Basics And Different XML Files Used In Android"},"content":{"rendered":"<p>XML stands for Extensible\u00a0Markup\u00a0Language. XML is a markup language much like HTML used to describe data. \u00a0XML tags are not predefined in XML. We must\u00a0define our own Tags. Xml as itself is well readable both by human and machine. Also, it is scalable and simple to develop. In Android we use xml for designing our layouts because xml is lightweight language so it doesn\u2019t make our layout heavy.<\/p>\n<p>In this article we will go through the basic concepts of xml in Android and different XML files used for different purpose in Android. This will help you in writing a UI code\u00a0to design your desired user interface.<\/p>\n<hr \/>\n<h4><strong>Basics Of User Interface:<\/strong><\/h4>\n<p>The whole concept of <a href=\"\/ui\/\">Android User Interface<\/a> is defined using the hierarchy of View and ViewGroup objects. A ViewGroup is an invisible container that organizes child views. These child views are other widgets which are used to make the different parts of UI. One ViewGroup can have another ViewGroup as an child element as shown in the figure given below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2037\" src=\"\/ui\/wp-content\/uploads\/2016\/06\/Basic-UI-user-interface-ViewGroup-and-View-Diagram.jpg\" alt=\"Basic UI user interface ViewGroup and View Diagram\" width=\"542\" height=\"341\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/Basic-UI-user-interface-ViewGroup-and-View-Diagram.jpg 542w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/Basic-UI-user-interface-ViewGroup-and-View-Diagram-300x189.jpg 300w\" sizes=\"auto, (max-width: 542px) 100vw, 542px\" \/><\/p>\n<p>Here in above Diagram ViewGroup (<a href=\"\/ui\/linear-layout\">Linear Layout<\/a>) contains one ViewGroup (i.e. <a href=\"\/ui\/relative-layout\">Relative Layout<\/a>)and two View(<a href=\"\/ui\/button\">Button<\/a> and <a href=\"\/ui\/textview\">TextView<\/a>). Further two more View (i.e. 2 <a href=\"\/ui\/edittext\">EditText<\/a> ) are nested inside Relative Layout ViewGroup. It is important to note that one layout can be nested in another layout.<\/p>\n<p>The below code snippet will explain the above image in better way. Paste it in <strong>activity_main.xml<\/strong>:<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n android:orientation=\"vertical\" android:layout_width=\"match_parent\"\r\n android:layout_height=\"match_parent\"&gt;\r\n\r\n\r\n &lt;Button\r\n android:id=\"@+id\/buton1\"\r\n android:layout_width=\"wrap_content\"\r\n android:layout_height=\"wrap_content\"\r\n android:text=\"Button\"\/&gt;\r\n\r\n &lt;TextView\r\n android:id=\"@+id\/textView1\"\r\n android:layout_width=\"wrap_content\"\r\n android:layout_height=\"wrap_content\"\r\n android:text=\"sample Text\"\r\n android:layout_marginTop=\"15dp\"\r\n android:textSize=\"30dp\"\/&gt;\r\n\r\n &lt;RelativeLayout\r\n android:layout_width=\"match_parent\"\r\n android:layout_height=\"match_parent\"&gt;\r\n\r\n &lt;EditText\r\n android:id=\"@+id\/editTextName\"\r\n android:layout_width=\"wrap_content\"\r\n android:layout_height=\"wrap_content\"\r\n android:hint=\"First Name\"\r\n \/&gt;\r\n\r\n &lt;EditText\r\n android:id=\"@+id\/editTextLastName\"\r\n android:layout_width=\"wrap_content\"\r\n android:layout_height=\"wrap_content\"\r\n android:hint=\"Last Name\"\/&gt;\r\n\r\n &lt;\/RelativeLayout&gt;\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2038\" src=\"\/ui\/wp-content\/uploads\/2016\/06\/XML-UI-in-Android-with-example.jpg\" alt=\"XML UI in Android with example\" width=\"512\" height=\"486\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/XML-UI-in-Android-with-example.jpg 512w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/XML-UI-in-Android-with-example-300x285.jpg 300w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/p>\n<p>Every Android application screen has some components like button, Text or images. These are contained inside the ViewGroup. Layouts are the best examples for ViewGroups. The different types of layout in android are\u00a0<a href=\"\/ui\/linear-layout\">Linear Layout<\/a>,\u00a0<a href=\"\/ui\/relative-layout\">Relative Layout<\/a>,\u00a0<a href=\"\/ui\/absolute-layout\">Absolute Layout<\/a>, <a href=\"\/ui\/tablelayout\">Table Layout<\/a> and\u00a0<a href=\"\/ui\/framelayout\">Frame Layout<\/a>.<\/p>\n<hr \/>\n<h4><strong>Different XML Files Used in Android:<\/strong><\/h4>\n<p>In Android there are several xml files used for several different purposes. Below we define each and every one.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. Layout XML Files:\u00a0<\/strong><\/span>Layout xml files are used to define the actual UI(User interface) of our application. It holds all the elements(views) or the tools that we want to use in our application. Like the TextView\u2019s, Button\u2019s and other UI elements.<\/p>\n<p><strong>Location in Android Studio:<\/strong><\/p>\n<p>You will find out this file inside the <strong>res<\/strong> folder and inside it there is another folder named <strong>layout<\/strong> where you will get all the layout files for their respective activities or fragments.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2041\" src=\"\/ui\/wp-content\/uploads\/2016\/06\/Layout-XML-in-Android-Studio.jpg\" alt=\"Layout XML in Android Studio\" width=\"496\" height=\"299\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/Layout-XML-in-Android-Studio.jpg 496w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/Layout-XML-in-Android-Studio-300x181.jpg 300w\" sizes=\"auto, (max-width: 496px) 100vw, 496px\" \/><\/p>\n<p><strong>Basic Layout XML Code:<\/strong><\/p>\n<p>Below we show <strong>activity_main.xml<\/strong> file in which we have two TextView\u2019s.<\/p>\n<pre>&lt;!--\u00a0 RelativeLayout in which we set green color for the background --&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nxmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nandroid:background=\"@color\/greenColor\"\r\ntools:context=\".MainActivity\"&gt;\r\n\r\n&lt;TextView\r\nandroid:id=\"@+id\/firstTextView\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:layout_margin=\"20dp\"\r\nandroid:padding=\"10dp\"\r\nandroid:text=\"First Text View\"\r\nandroid:textColor=\"@color\/white\"\r\nandroid:textSize=\"20sp\"\r\nandroid:textStyle=\"bold\" \/&gt;\r\n&lt;!-- second TextView --&gt;\r\n&lt;TextView\r\nandroid:id=\"@+id\/secondTextView\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_below=\"@+id\/firstTextView\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:layout_margin=\"20dp\"\r\nandroid:padding=\"10dp\"\r\nandroid:text=\"Second Text View\"\r\nandroid:textColor=\"@color\/white\"\r\nandroid:textSize=\"20sp\"\r\nandroid:textStyle=\"bold\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. Manifest xml File(Mainfest.xml):\u00a0<\/strong><\/span>This xml is used to define all the components of our application. It includes the names of our application packages, our Activities, receivers, services \u00a0and the permissions that our application needs. For Example &#8211; Suppose we need to use internet in our app then we need to define Internet permission in this file.<\/p>\n<p><strong>Location in Android Studio:<\/strong><\/p>\n<p>It is located inside app &gt; manifests folder<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2042\" src=\"\/ui\/wp-content\/uploads\/2016\/06\/AndroidManifest-xml-location-in-Android-Studio.jpg\" alt=\"AndroidManifest xml location in Android Studio\" width=\"352\" height=\"157\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/AndroidManifest-xml-location-in-Android-Studio.jpg 352w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/AndroidManifest-xml-location-in-Android-Studio-300x134.jpg 300w\" sizes=\"auto, (max-width: 352px) 100vw, 352px\" \/><\/p>\n<p><strong>Defining Internet Permission in AndroidManifest.xml<\/strong><\/p>\n<p>Below we show the <strong>AndroidManifest.xml<\/strong> file and define the Internet Permission in that file.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\npackage=\"example.abhiandroid.MyApplication\"&gt;\u00a0\u00a0\u00a0\u00a0 &lt;!-- application package name --&gt;\r\n\r\n&lt;uses-permission android:name=\"ANDROID.PERMISSION.INTERNET\" \/&gt;\r\n&lt;!-- define Internet Permission --&gt;\r\n&lt;application\r\nandroid:allowBackup=\"true\"\r\nandroid:icon=\"@mipmap\/ic_launcher\"\r\nandroid:label=\"@string\/app_name\"\r\nandroid:theme=\"@style\/AppTheme\"&gt;\r\n\r\n&lt;!-- add your Activities, Receivers, Services Names Here --&gt;\r\n&lt;activity\r\nandroid:name=\".MainActivity\"\r\nandroid:label=\"@string\/app_name\"&gt;\r\n&lt;intent-filter&gt;\r\n&lt;action android:name=\"android.intent.action.MAIN\" \/&gt;\r\n\r\n&lt;category android:name=\"android.intent.category.LAUNCHER\" \/&gt;\r\n&lt;\/intent-filter&gt;\r\n&lt;\/activity&gt;\r\n&lt;\/application&gt;\r\n\r\n&lt;\/manifest&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. Strings xml File(strings.xml):\u00a0<\/strong><\/span>This xml file is used to replace the Hard-coded strings with a single string. We define all the strings in this xml file and then access them in our app(Activity or in\u00a0 Layout XML files) from this file. This file enhance the reusability of the code.<\/p>\n<p><strong>Location in Android Studio:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2043\" src=\"\/ui\/wp-content\/uploads\/2016\/06\/strings-xml-location-android-studio.jpg\" alt=\"strings xml location android studio\" width=\"264\" height=\"342\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/strings-xml-location-android-studio.jpg 264w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/strings-xml-location-android-studio-232x300.jpg 232w\" sizes=\"auto, (max-width: 264px) 100vw, 264px\" \/><\/p>\n<p>Below we show <strong>strings.xml<\/strong> file and define a string in the file.<\/p>\n<pre>&lt;resources&gt;\r\n&lt;string name=\"app_name\"&gt;My Application&lt;\/string&gt;\r\n\r\n&lt;string name=\"hello_world\"&gt;Hello world!&lt;\/string&gt;\r\n&lt;string name=\"action_settings\"&gt;Settings&lt;\/string&gt;\r\n&lt;string name=\"login\"&gt;User Login&lt;\/string&gt;\r\n&lt;!-- define your strings here --&gt;\r\n&lt;\/resources&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. Styles xml File(styles.xml):\u00a0<\/strong><\/span>This xml is used to define different styles and looks for the UI(User Interface) of application. We define our custom themes and styles in this file.<\/p>\n<p><strong>Location in Android Studio:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2045\" src=\"\/ui\/wp-content\/uploads\/2016\/06\/styles-location-xml-Android-Studio-1.jpg\" alt=\"styles location xml Android Studio\" width=\"264\" height=\"342\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/styles-location-xml-Android-Studio-1.jpg 264w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/styles-location-xml-Android-Studio-1-232x300.jpg 232w\" sizes=\"auto, (max-width: 264px) 100vw, 264px\" \/><\/p>\n<p>Below we show the style.xml file.<\/p>\n<pre>&lt;resources&gt;\r\n\r\n&lt;!-- Base application theme. --&gt;\r\n&lt;style name=\"AppTheme\" parent=\"Theme.AppCompat.Light.DarkActionBar\"&gt;\r\n&lt;!-- Customize your theme here. --&gt;\r\n&lt;\/style&gt;\r\n\r\n&lt;\/resources&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. Drawable xml Files:\u00a0<\/strong><\/span>These are those xml files that are used to provide various graphics to the elements or views of application. When we need to create a custom UI we use drawable xml files. Suppose if we need to define a gradient color in the background of Button or any custom shape for a view then we create a Drawable xml file and set it in the background of View.<\/p>\n<p><strong>Do Read:\u00a0<a href=\"\/androidstudio\/how-to-create-drawable-resource-xml-file-in-android-studio\">How To Create Drawable Resource XML File in Android Studio<\/a><\/strong><\/p>\n<p>Below we show <strong>custom_drawable.xml<\/strong> file and create a gradient background color using style attribute.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;shape xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"&gt;\r\n&lt;!-- define start, center and end color for gradient --&gt;\r\n&lt;gradient\r\nandroid:centerColor=\"#0f0\"\r\nandroid:endColor=\"#00f\"\r\nandroid:startColor=\"#f00\" \/&gt;\r\n&lt;\/shape&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. Color xml File (colors.xml):\u00a0<\/strong><\/span>This file is used to define the color codes that we used in our app. We simply define the color\u2019s in this file and used them in our app from this file.<\/p>\n<p><strong>Location in Android Studio<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2046\" src=\"\/ui\/wp-content\/uploads\/2016\/06\/colors-xml-location-Android-Studio.jpg\" alt=\"colors xml location Android Studio\" width=\"264\" height=\"342\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/colors-xml-location-Android-Studio.jpg 264w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/colors-xml-location-Android-Studio-232x300.jpg 232w\" sizes=\"auto, (max-width: 264px) 100vw, 264px\" \/><\/p>\n<p>Below we show the <strong>colors.xml<\/strong> file in which we define green and white color.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;resources&gt;\r\n&lt;!-- define your colors Here --&gt;\r\n&lt;color name=\"greenColor\"&gt;#0f0&lt;\/color&gt;\r\n&lt;color name=\"white\"&gt;#fff&lt;\/color&gt;\r\n&lt;\/resources&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. Dimension xml File(dimens.xml):\u00a0<\/strong><\/span>This xml file is used to define the dimensions of the View\u2019s. Suppose we need a Button with 50dp(density pixel) height then we define the value 50dp in dimens.xml file and then use it in our app from this file.<\/p>\n<p><strong>Location in Android Studio:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2047\" src=\"\/ui\/wp-content\/uploads\/2016\/06\/Dimension-xml-File-in-Android-Studio.jpg\" alt=\"Dimension xml File in Android Studio\" width=\"339\" height=\"385\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/Dimension-xml-File-in-Android-Studio.jpg 339w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/06\/Dimension-xml-File-in-Android-Studio-264x300.jpg 264w\" sizes=\"auto, (max-width: 339px) 100vw, 339px\" \/><\/p>\n<p>Below we show the dimens.xml file in which we define 50dp \u00a0dimension for Button height.<\/p>\n<pre>&lt;resources&gt;\r\n&lt;!-- Default screen margins, per the Android Design guidelines. --&gt;\r\n&lt;dimen name=\"activity_horizontal_margin\"&gt;16dp&lt;\/dimen&gt;\r\n&lt;dimen name=\"activity_vertical_margin\"&gt;16dp&lt;\/dimen&gt;&lt;dimen name=\"btnheight\"&gt;50dp&lt;\/dimen&gt;\r\n&lt;\/resources&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>XML stands for Extensible\u00a0Markup\u00a0Language. XML is a markup language much like HTML used to describe data. \u00a0XML tags are not predefined in XML. We must\u00a0define our own Tags. Xml as itself is well readable both by human and machine. Also, it is scalable and simple to develop. In Android we use xml for designing our &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/xml\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">XML in Android: Basics And Different XML Files Used In Android<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-2049","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>XML in Android: Basics And Different XML Files Used In Android \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"In this article we will go through the basic concepts of xml in Android and different XML files used for different purpose in Android. This will help you in writing a UI code to design your desired user interface.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/xml\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/2049","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=2049"}],"version-history":[{"count":1,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/2049\/revisions"}],"predecessor-version":[{"id":2684,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/2049\/revisions\/2684"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=2049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}