Thursday, December 6, 2007

LinearLayout Example

So if you want to create a UI using a XML file, you will need to put the XML file into your project's res/layouts folder. Here is a simple UI using the LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#000044"
android:orientation="vertical">
<TextView
id="@+id/textName"
android:text="Name:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"/>
<EditText
id="@+id/editName"
android:text=""
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
id="@+id/textPasswd"
android:text="Password:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"/>
<EditText
id="@+id/editPasswd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"/>
<Button
id="@+id/buttonSignIn"
android:text="Sign In"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
If you start your "Hello world!!" app having this file in a proper place (i.e. res/layouts/main.xml) you shall still see the same "Hello world!!" text on the emulator's screen. To tell the Android to use the XML file you have to change one line in the Java code. Concretely this line:
setContentView(tv);
To this:
setContentView(R.layout.main);
Now, after the compilation and running the app you should see our new screen presence of the app. It should be a simple "Sign In" form.

Try to make some changes to the XML file to get yourself more familiar with a XML defined UI. If you need some help, check the Android's API reference page or leave me a comment.

7 comments:

Anonymous said...

Hi, thanks for the tutorials. I have not clear how to put code for certain events like a command button "OnClick" (as many programmers are used to)... how do I program an "OnClick" event, or something like this?

Greetings

Francisco said...

Man, not that great. Nothing that you can't find on the official guides. Then there is a guy asking something really simple (at least in every used language) and you can't give a minimal feedback.
Not good for Android.
Yahooid will rule!!!

Anonymous said...

android:id

Anonymous said...

thanks for tutorial, but I tried it and there is a problem like that ( Activity class {com.hello/com.hello.hello} does not exist.
)
Before trying I run it correctly, I changed only the main.xml I got the mistake.
Do you have any idea?

joel said...

From operating systems to multimedia, PC & mobile games to anti-virus, from drivers to registry cleaners and internet tools our website features all the latest soft wares for safe and free downloading enjoy.

andry yudha said...

Nice blog. I will keep visiting this blog very often.
Thanks , for much information,Keep sharing such a wonderful information..!

Meghna said...

Great example, thanks... Even this http://www.compiletimeerror.com/2013/06/android-linearlayout-example.html might help.. Have a look..