• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Help XML code format question

Hello all. I am new to coding. Except for many years ago, designing two games in BASIC on a Vic 20 during a summer break while in high school! Regaining a passion for creating programs now.

So, I am currently studying JavaScript, CSS and HTML along with Android app development.

The question I have is why does the XML code for Android need to include "android" in the code?

e.g.

<TextView
android:text="Happy Birthday Dad!"
android:background="@android:color/darker_gray"
android:layout_width="150dp"
android:layout_height="150dp"
android:gravity="center"
/>

I tried googling for an answer but, couldn't find anything. If the XML code is self contained in the APK why the need to specify android? Potentially only makes sense if there some communication outside of the app and this is required to differentiate between XML code used for non-android applications, databases, etc.

Explanation would be appreciated.

Thank you! Best.
 
Hello all. I am new to coding. Except for many years ago, designing two games in BASIC on a Vic 20 during a summer break while in high school! Regaining a passion for creating programs now.

So, I am currently studying JavaScript, CSS and HTML along with Android app development.

The question I have is why does the XML code for Android need to include "android" in the code?

e.g.

<TextView
android:text="Happy Birthday Dad!"
android:background="@android:color/darker_gray"
android:layout_width="150dp"
android:layout_height="150dp"
android:gravity="center"
/>

I tried googling for an answer but, couldn't find anything. If the XML code is self contained in the APK why the need to specify android? Potentially only makes sense if there some communication outside of the app and this is required to differentiate between XML code used for non-android applications, databases, etc.

Explanation would be appreciated.

Thank you! Best.

There's probably a really technical answer for this, but I'll just tell you what I think.

I believe this is done mostly for ease of use and to differentiate between different element options.

When you type "android:" you will get a list menu with all the options you can choose from therefore eliminating the need to memorize all the various options.

This also helps keep things concise between other element options. For example CardView items begin with "card_view:" instead of "android:" and would look something like this...

XML:
card_view:cardBackgroundColor="?attr/colorPrimary"

Another element you'll come across "app:"...

XML:
app:popupTheme="@style/ThemeOverlay.AppCompat"

Same goes for all these elements. When you type the name followed by a colon you'll get a list of options. Seems like a simple way of grouping options to different elements.

I've never concerned myself with this and probably why I don't have a geeky technical answer. Hope this helps somewhat.
 
I appreciate your answer. Shows how new I am to XML! I had assumed that all attributes were prefaced with android. Hence, the question.

Thank you for clarifying this.
 
@GameTheory nailed it, and the technical answer is that the prefix "android" is called a namespace. In XML, namespaces are used as references to other XML elements or schema, which are defined elsewhere, but used within the current file. It's a kind of shorthand notation.
So in the layout XML, you'll see this, where "xmlns" means "XML namespace"

Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

After this is defined, anywhere we use the format android:id, it's actually a reference to that other resource.
 
Last edited by a moderator:
Back
Top Bottom