sparkonhdfs
sparkonhdfs

Reputation: 1343

How does this XML code work?

I started learning Android development today, and I need help understanding how some XML code works. The book I am using does not explain the code, however, using it gives me the desired result.

<?xml version="1.0" encoding="utf-8"?>
<inearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New To Do Item"
/*The above lines are the only things I understand. the code below does not create an 
actual list, instead just initiates the layout of height and width. */
/>
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

Using this code generates a To-do list of items, that are all labeled "Item 1", "Item 2", etc..
I don't understand how the list gets created when no actual code is used to make

-Also, I want to know how relevant XML coding is to programming in Android. Should I learn the language if it is going to be useful?
thanks.

Upvotes: 0

Views: 105

Answers (2)

Martin Cazares
Martin Cazares

Reputation: 13705

Ok, first of all, Let me explain what exactly android does for you when you use xml resource.

Android uses the "Inflate" technique, which consist in creating Java Objects based on XML Tags, basically what it does its creating a class and properties(java) based on tags and attributes(xml), the prior code is asking to the OS to:

First:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

Create a ViewGroup, in this case a LinearLayout which is a Views Holder that arranges elements "vertically" or "horizontally" depending on the orientation, "fill_parent" attributes ask for all the space that remain on the parents view, if this elements parent is the root view, then it will take the whole screen space.

Second:

<EditText
     android:id="@+id/myEditText"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="New To Do Item"/>

Create a EditText component within this ViewGroup that matches the parent's width(in this case the LinearLayout) and keep the height of the element as long as the content within( this is why it starts as 1 single line, and as you are writing it might grow vertically...)

Third:

<ListView
   android:id="@+id/myListView"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"/>

Create a listView to show contents in a vertical scrollable way, this element might not been shown if no adapter is being use to populate some content, since we are using a LinearLayout, this element will be show below the EditText created below...

Regards!

Upvotes: 5

jimmithy
jimmithy

Reputation: 6380

The android graphical layout designer is pre-populating the listview with example data to give you an idea of how it will look. If you were to run this code in the emulator or on your phone, nothing would show.

XML is somewhat useful if you want to really delve into android programming, luckily you can do most things in the graphical drag and drop designer to begin with.

Upvotes: 1

Related Questions