26 Desember 2012

4.Learning XML Code for Android LinearLayout | Android XML Tutorial

Learning XML Code for Android LinearLayout | Android XML Tutorial


Learning about XML Code in Android Applications

In this Android tutorial we’ll learn about XML code. By learning XML, you will be able to create an Android LinearLayout. And lastly, I will show you how easily you can change background image of your app in XML Code.

Keyword variation Video Walk-through


Android Linear Layout Description

“A layout defines the visual structure for a user interface, such as the UI for an activity or app widget.”
Linear Layout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute.
All children of a Linear Layout are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are, and a horizontal list will only be one row high (the height of the tallest child, plus padding). A Linear Layout respects margins between children and the gravity (right, center, or left alignment) of each child.
You could declare your application’s default layouts in XML, including the screen elements that will appear in them and their properties. You could then add code in your application that would modify the state of the screen objects, including those declared in XML, at run time. The advantage to declaring your UI in XML is that it enables you to better separate the presentation of your application from the code that controls its behavior. Your UI descriptions are external to your application code, which means that you can modify or adapt it without having to modify your source code and recompile.
For example, you can create XML layouts for different screen orientations, different device screen sizes, and different languages. Additionally, declaring the layout in XML makes it easier to visualize the structure of your UI, so it’s easier to debug problems.
Using Android’s XML vocabulary, you can quickly design UI layouts and the screen elements they contain, in the same way you create web pages in HTML — with a series of nested elements. Each layout file must contain exactly one root element, which must be a View or ViewGroup object. Once you’ve defined the root element, you can add additional layout objects or widgets as child elements to gradually build a View hierarchy that defines your layout.
Every View and ViewGroup object supports their own variety of XML attributes. Some attributes are specific to a View object (for example, TextView supports the textSize attribute), but these attributes are also inherited by any View objects that may extend this class. Any View object may have an integer ID associated with it, to uniquely identify the View within the tree. When the application is compiled, this ID is referenced as an integer, but the ID is typically assigned in the layout XML file as a string, in the id attribute. This is an XML attribute common to all View objects (defined by the View class) and you will use it very often. The syntax for an ID, inside an XML tag is: android:id=”@+id/my_button”(Reference:http://developer.android.com/guide/topics/ui/declaring-layout.html
And lastly, how do you change background of your app? Simply, type the attribute android:background into Layout element of the XML code as follows:
1android:background="@drawable/<wbr>background_picture"
Before this will work, you need to add the picture into your drawable folder of your workspace.

Source Code

main.java
1package com.example.thebasics;
2 
3import android.app.Activity;
4import android.os.Bundle;
5 
6public class main extends Activity {
7 
8    @Override
9    protected void onCreate(Bundle savedInstanceState) {
10        // TODO Auto-generated method stub
11        super.onCreate(<wbr>savedInstanceState); setContentView(R.layout.main); } } </wbr>
main.xml
1 

Navigation

In this lesson you will learn:
-A better understanding of XML
-Adding a Button
-Find coding that even when we don’t remember the exact coding
-Basics of the Linear Layout
-New XML functions
-Adding our picture to the background of our app
-Running our program on the emulator

Tidak ada komentar:

Posting Komentar