You are on page 1of 35

Lecture 2

XML in Android

by
Zana Azeez Kakarash

Email: zana.azeez@kti.edu.krd

2021 – 2022

Kurdistan Technical Institute


(www.kti.edu.krd/ku)
CS Departement
XML in Android

Two
XML in Android

• XML stands for Extensible Markup Language.

• XML is a markup language much like HTML used to describe data.

• XML tags are not predefined in XML , we must define 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’t
make our layout heavy.

Kurdistan Technical Institute 3 Lecturer : Zana Azeez Kakarash


Advantage of XML

• XML-based layout have the following advantages:

1. XML is a very popular and widely-used format. Hence, a lot of developers are quite comfortable with it.

2. It helps to provide separation of the UI from the code logic. This provided flexibility to change one

without affecting much the other.

3. Generating XML output is easier than writing direct code, making it easier to have drag-and-drop UI

tools to generate interfaces for android apps.

Kurdistan Technical Institute 4 Lecturer : Zana Azeez Kakarash


How XML different from Programming language

• A markup language is used to control the presentation of data.

• A markup language prepare a structure for the data or prepare the design of a page.

• XML is not a programming language.

• A markup language doesn’t include any kind of logic or algorithm

Kurdistan Technical Institute 5 Lecturer : Zana Azeez Kakarash


Basics Of User Interface

• The whole concept of Android User Interface 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 .

Kurdistan Technical Institute 6 Lecturer : Zana Azeez Kakarash


Basics Of User Interface

• Here in above Diagram ViewGroup (Linear Layout) contains one ViewGroup (i.e. Relative Layout) and
two View (Button and TextView)

Kurdistan Technical Institute 7 Lecturer : Zana Azeez Kakarash


Example : activity_main.xml

Kurdistan Technical Institute 8 Lecturer : Zana Azeez Kakarash


Different XML Files used in Android

9
Different XML Files Used in Android

• In Android there are several xml files used for several different purposes.

1 - Layout XML Files :

• 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’s, Button’s and other UI elements.

Kurdistan Technical Institute 10 Lecturer : Zana Azeez Kakarash


Different XML Files Used in Android

• Layout XML Files :


• You will find out this file inside the res folder and inside it there is another folder named layout where
you will get all the layout files for their respective activities or fragments.

Kurdistan Technical Institute 11 Lecturer : Zana Azeez Kakarash


Different XML Files Used in Android

2 - Manifest xml File(Mainfest.xml):

• This xml is used to define all the components of our application.

• It includes the names of our application packages, our Activities, receivers, services and the

permissions that our application needs.

• Example Suppose we need to use internet in our app then we need to define Internet permission in

this file.

Kurdistan Technical Institute 12 Lecturer : Zana Azeez Kakarash


Different XML Files Used in Android

• Manifest xml File(Mainfest.xml) :

• It is located inside app > manifests folder

Kurdistan Technical Institute 13 Lecturer : Zana Azeez Kakarash


Example

AndroidManifest.xml file and


define the Internet Permission in
that file.

Kurdistan Technical Institute 14 Lecturer : Zana Azeez Kakarash


Different XML Files Used in Android

3 - Strings xml File(strings.xml):

• 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 Layout XML files)

from this file.

• This file enhance the reusability of the code.

Kurdistan Technical Institute 15 Lecturer : Zana Azeez Kakarash


Example

- Location in Android Studio :

• show strings.xml file and define a string in


the file.

Kurdistan Technical Institute 16 Lecturer : Zana Azeez Kakarash


Different XML Files Used in Android

4 - Styles xml File(styles.xml): 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.

Kurdistan Technical Institute 17 Lecturer : Zana Azeez Kakarash


Different XML Files Used in Android

5 - Drawable xml Files: These are those xml files that are used to provide various graphics to the
elements or views of application.

Kurdistan Technical Institute 18 Lecturer : Zana Azeez Kakarash


Different XML Files Used in Android

6 - Color xml File (colors.xml):


• This file is used to define the color codes that we used in our app.
• We simply define the color’s in this file and used them in our app from this file.

Kurdistan Technical Institute 19 Lecturer : Zana Azeez Kakarash


Where to Run Android App.

20
Where to Run Android App

• Android app can run on :

1. Real phone (or device)

2. Emulator (software version of phone)

Kurdistan Technical Institute 21 Lecturer : Zana Azeez Kakarash


Running Android App on Real Phone

1) Connect your device to your development machine with a USB cable.

2) Enable USB debugging in the Developer options window :

a. Open the Settings app.

b. If your device uses Android v8.0 or higher, select System.

c. Scroll to the bottom and select About phone.

d. Scroll to the bottom and tap Build number seven times.

e. Return to the previous screen, scroll to the bottom, and tap Developer options.

f. In the Developer options window, scroll down to find and enable USB debugging.

Kurdistan Technical Institute 22 Lecturer : Zana Azeez Kakarash


Running Android App on Emulator

• The Android Emulator simulates Android devices on your computer so that you can test your
application on a variety of devices and Android API levels without needing to have each physical
device.

Kurdistan Technical Institute 23 Lecturer : Zana Azeez Kakarash


Running Android App on Emulator

• Step 1: Firstly, open the AVD Manager directly by the AVD icon in the Toolbar.

Kurdistan Technical Institute 24 Lecturer : Zana Azeez Kakarash


Running Android App on Emulator

• Step 2: Your Virtual Devices list will be opened on your Computer screen. After that you Select that
AVD on which you want to run your App.

Kurdistan Technical Institute 25 Lecturer : Zana Azeez Kakarash


What is in the Android App. ?

• Most apps written in Java or C#

• Android SDK tools compile code, data and resource files into Android PacKage (filename.apk).

• Apps download from Google Play, or copied to device as filename.apk

• Installation = installing apk file which is include :

a) App elements
b) User Interface
c) Other code designed to run in background

Kurdistan Technical Institute 26 Lecturer : Zana Azeez Kakarash


Android Compilation Process/Steps

27
Android Compilers

• Your source code is compiled by Standard Java Compiler which uses the JIT- 'Just-In-

Time' compilation model.

• Once the code is compiled, it becomes bytecode (with ‘.class’ extension).

• The bytecode gets executed by target-specific-VM , which is a runtime environment for Java.

• But for Android, we have two different virtual machines: - DALVIK

- ART

Kurdistan Technical Institute 28 Lecturer : Zana Azeez Kakarash


1- DALVIK

1) The Java Compiler (javac) converts the Java Source Code into Java Byte-Code (.class).

2) Then DEX Compiler converts this (.class) file into in Dalvik Byte Code i.e. “.dex” file.

3) Dalvik Virtual Machine uses its own byte-code and runs “.dex” (Dalvik Executable File) file.

Kurdistan Technical Institute 29 Lecturer : Zana Azeez Kakarash


2- Android runtime (ART)

• ART was introduced in Android 5.0 (Lollipop), ART completely replaced Dalvik.

• ART as the runtime executes the Dalvik Executable format and Dex bytecode specification.

• Uses the ahead-of-time compilation model which compiles the apps to machine code upon installation.

• Apps run a bit FASTER! under ART, so the startup time of apps gets reduced.

• It also has improved garbage collection.

Kurdistan Technical Institute 30 Lecturer : Zana Azeez Kakarash


Process of Compilation

1- From source code to bytecode :

• You will need SDK (Software Developer Kit) to compile the Java code of your application.

• Your code and precompiled classes from runtime and custom libraries are compiled.

• The Javac outputs a set of Java bytecode files.

Kurdistan Technical Institute 31 Lecturer : Zana Azeez Kakarash


Process of Compilation

2- From minimized bytecode to dex code :

• The next step is to take the output files from step number 2 and to turn those files into dex bytecode -

Dalvik EXecutable(.DEX)

• The DEX bytecode is a format that is optimized for Android and which can be executed by the older

DALVIK runtime or the newer ART runtime.

• Note : It generate a .apk file from a DEX file + non-Java libraries + zipped resources.

Kurdistan Technical Institute 32 Lecturer : Zana Azeez Kakarash


Process of Compilation

3- Put classes.dex and resources into a package file :

• The classes.dex file and the resources from your application, such as images and layouts, are then
compressed into a zip-like file called an Android Package or .apk file.

• This is done with the Android Asset Packaging Tool or aapt:

Kurdistan Technical Institute 33 Lecturer : Zana Azeez Kakarash


Build Process Explained :

Kurdistan Technical Institute 34 Lecturer : Zana Azeez Kakarash


Thanks!
Any questions ?
You can find me at
◉ zana.azeez@kti.edu.krd
◉ zana.azeez.k@gmail.com

35

You might also like