XML In Android

Introduction

In this blog, I will explain about XML(eXtensible Markup Language) in an Android Application and how many XML files are available in an Android Application, using Android Studio.
 
Requirements      
  • Windows 10 (OS free available source in online (https//www.microsoft.com/en-in/software-download/windows10)). 
  • Android Studio 2.1.3 (free source available in online(https//developer.android.com/studio/index.html?gclid=CNP3qbH5htECFdiKaAodeFIKtA)). 
XML stands for an eXtensible MarkUp Language and it is like HTML (Hyper Text MarkUp Language). it should be mainly used for design purposes because XML is a lightweight layout.

User Interface

The user interface interacts with the user in an Application. It should be used for designing purposes.
The code given below is mentioned an Activity_main.xml. 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="xyz.rvconstructions.www.myapplication.MainActivity">  
  11.   
  12.     <Button  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="New Button"  
  16.         android:id="@+id/button"  
  17.         android:layout_centerVertical="true"  
  18.         android:layout_alignParentLeft="true"  
  19.         android:layout_alignParentStart="true" />  
  20.   
  21.     <EditText  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:id="@+id/editText"  
  25.         android:layout_alignParentLeft="true"  
  26.         android:layout_alignParentStart="true"  
  27.         android:layout_alignParentTop="true"  
  28.         android:layout_alignParentRight="true"  
  29.         android:layout_alignParentEnd="true" />  
  30.   
  31.     <RadioButton  
  32.         android:layout_width="wrap_content"  
  33.         android:layout_height="wrap_content"  
  34.         android:text="New RadioButton"  
  35.         android:id="@+id/radioButton"  
  36.         android:layout_above="@+id/button"  
  37.         android:layout_alignRight="@+id/editText"  
  38.         android:layout_alignEnd="@+id/editText" />  
  39. </RelativeLayout>  
Now, you will see the design page. It is like XML and I have mentioned parent and child view group.



Different XML files are available in our Application.

Layout XML file

The layout XML is used for actual User Interface. It is like views and elements, such as on TextView, Button, Spinner, RadioButton and Extra.

The code snippet is given below, which is mentioned in activity_main.xml. 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="xyz.rvconstructions.www.myapplication.MainActivity">  
  11.   
  12.     <Button  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="New Button"  
  16.         android:id="@+id/button"  
  17.         android:layout_centerVertical="true"  
  18.         android:layout_alignParentLeft="true"  
  19.         android:layout_alignParentStart="true" />  
  20.   
  21.     <EditText  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:id="@+id/editText"  
  25.         android:layout_alignParentLeft="true"  
  26.         android:layout_alignParentStart="true"  
  27.         android:layout_alignParentTop="true"  
  28.         android:layout_alignParentRight="true"  
  29.         android:layout_alignParentEnd="true" />  
  30.   
  31.     <RadioButton  
  32.         android:layout_width="wrap_content"  
  33.         android:layout_height="wrap_content"  
  34.         android:text="New RadioButton"  
  35.         android:id="@+id/radioButton"  
  36.         android:layout_above="@+id/button"  
  37.         android:layout_alignRight="@+id/editText"  
  38.         android:layout_alignEnd="@+id/editText" />  
  39. </RelativeLayout>  
Manifest XML file

Manifest XML file is used for all the components of our Application. It is like application activities, packages, services and receivers. Sometimes, you need internet permission in our application support to manifest the XML file, which is like WebView app.


Here, I will declare AndroidManifest.xml code. 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="xyz.rvconstructions.www.myapplication">  
  4.       
  5.     <uses-permission android:name="ANDROID.PERMISSION.INTERNET" />  
  6.       
  7.     <application  
  8.           
  9.         android:allowBackup="true"  
  10.         android:icon="@mipmap/ic_launcher"  
  11.         android:label="@string/app_name"  
  12.         android:supportsRtl="true"  
  13.         android:theme="@style/AppTheme">  
  14.         <activity android:name=".MainActivity">  
  15.             <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.     </application>  
  22.   
  23. </manifest>  
Colors XML file(colors.xml)

In this field, I will show how to set colors in our Application, which is background color, app color and extras.



Here, I will mention Colors.xml code, as shown below.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <color name="colorPrimary">#3F51B5</color>  
  4.     <color name="colorPrimaryDark">#303F9F</color>  
  5.     <color name="colorAccent">#FF4081</color>  
  6. </resources>  
Dimention XML file(dimention.xml)

In this, the files are used to define dimensions of the Views. It is like setting the button pixels and you will set to height and width density pixels (50dp,100dp).These are the two dimensions file available such as on dimens.xml(), dimens.xml (w820dp).



The code is given below of dimens.xml. 
  1. <resources>  
  2.     <!-- Default screen margins, per the Android Design guidelines. -->  
  3.     <dimen name="activity_horizontal_margin">16dp</dimen>  
  4.     <dimen name="activity_vertical_margin">16dp</dimen>  
  5. </resources>  
Drawable XML file(custom_drawable.xml)

In this Drawable XML, the file is used to provide the various graphics elements and view our Application. Now, go to Drawable and click New. Choose Drawable Source File and write your drawable XML file's name and finally click OK button. Here, we created Drawable.xml file. Drawable> File>Drawable Source File>" and you will write the drawable file name">OK.



Here, you will see the custom_drawable.xml .



The code of custom_dawable.xml is given below. 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4. <gradient  
  5. android:centerColor="#0f0"  
  6. android:endColor="#00f"  
  7. android:startColor="#f00" />  
  8. </shape>  
Strings XML file(strings.xml)

In this file, it is used to assign the string value and replace the string value. The files are meant to be used by your app.



The code for strings.xml is given below. 
  1. <resources>  
  2.     <string name="app_name">My Application</string>  
  3.     <string name="Muthuramalingam">Muthuramalingam</string>  
  4.     <string name="apple">apple</string>  
  5.     <string name="android">android</string>  
  6. </resources>  
Styles XML file(styles.xml)

In this file, the different styles and looks are used. It should be mainly used for the user interface.



Here, you will see the code of styles.xml. 
  1. <resources>  
  2.   
  3.     <!-- Base application theme. -->  
  4.     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  5.         <!-- Customize your theme here. -->  
  6.         <item name="colorPrimary">@color/colorPrimary</item>  
  7.         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>  
  8.         <item name="colorAccent">@color/colorAccent</item>  
  9.     </style>  
  10.   
  11. </resources>
Ebook Download
View all
Learn
View all