Thursday, July 4, 2013

Android : Layouts

Back to you guys after a long time. From today we will go inside in depth of android features and try to learn some new stuff along the way . The next few days, we will feverishly try to cover a lot of topics pertaining to android.  So let's get cracking .

What's an android layout ?

Well, a lay out is what it actually means. It's the main UI of your app and will contain several UI controls of your app. You can define your lay out height, width and orientation , add text views , buttons etc and when you run the app , all of them will come up in your app UI . Layouts are presented in XML. When you first create a layout, the main.xml  is created. In case you are wondering that's your lay out .

This is how a typical layout is created :

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout >

I know, i know, you are very confused . Let's break it up then :

1> fill_parent --> means fill the parent window

2> vertical --> That's your typical orientation , you can specify 'horizontal' as well and no one will kill you.

3> wrap_content --> means wrap the content , kind of works like html wrap attribute, quite cool , huh !

Okay, kidding aside, these are very important attributes that define your layout, which means they tell your mobile / tablet that this is the way you want the app to be shown .

So let's run this one in eclipse, once we do that , this is what we will get in the emulator :


If  you look clearly up above , you will see the hello world string is coming twice and vertically one below the other. It's coming twice because we have two text views and vertically because the  orientation is vertical, duh......

So there you go....it's been laid out for you. Till next time ....

No comments:

Post a Comment