juztcode
juztcode

Reputation: 1345

understanding the xml file for android programming

I'm not very experience with xml but, according to w3Schools, the keywork before the colon(:) is actually an xml way of creating a namespace. E.g.:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    tools:context="com.hbad.mymessenget.CreateMessageActivity">  

So, my question is, where and how are these variables stored, and how do these get accessed in our android programs? Specifically in the case of android studio(currently version 3.4)

Upvotes: 2

Views: 301

Answers (2)

Giddy Naya
Giddy Naya

Reputation: 4658

XML is nothing but a structured text file.

I will break it down as simple as possible in other to aid your understanding of how it works.

For example: You can't create a .txt file on your device with the content as int abc = 12000; and expect your device to specifically store the value of abc into its memory simply because it looks like a variable. :/ Simply No. Same goes for an XML file with content, the content remains inside the xml file until it is needed.

When android UI launches an app it checks for the activity's xml file in other to read it, interprete it, and determine how to draw a reasonable display to the user.

Reading the file line-after-line

If android UI sees:

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"

It tells the OS: Create a linear section that will fill the app screen both in width and in height

If it sees this:

android:background="@color/white"

It tells the OS: Look for a file named color.xml, read the content, bring back the value of an item named white and then make this layout background that color.

As you can see the OS is not going to any specific memory allocation to retrieve its values because already it resides in the file (either internal or external).

What is the work of namespace in all senario?

Android basically has three predefined XML namespaces: app, android, tools

When android UI sees the line

xmlns:android

It simply tells the OS: Yes, this file is meant for us and we can go on to interpret it like we always do. Hence anywhere in the file it sees a line with android: it would get the attribute of that line say e.g background then look up the definition in xmlns:android namespace dictionary if it exist and how it should be handled/processed. Same goes for other namespaces; i.e. xmlns:tools, xmlns:app, and even custom namespaces xmlns:myNameSpace.

As you can see; namespaces enables android UI to distinguish its file from others, and also allow similar definition for attributes with different interpretaion making the xml content more robust and structured.

For further references. See Android Layouts Vocabulary, Different XML Files Used in Android, XML Namespace Wiki

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1006829

They are not variables.

xmlns: defines an XML namespace for use by other attributes.

tools: is used for attributes aimed at Android Studio and other development tools; these are not included in your app.

android: and app: are for attributes that have a runtime effect on your app. Details about those attributes can be found in the documentation, such as the documentation for LinearLayout. The use of these attributes is also covered in books and courses on Android app development.

Upvotes: 2

Related Questions