Tuesday 11 June 2013

Linear Layout



What are Layouts:


Before start building a Layout we must know what the Layout is? Layouts are nothing but a container which can contain your views. We can put many Views inside Layout and can create GUI(Graphical user interface)  for users, so that they can interact with the Application.



Types of Layout 

There are different types of Layouts available in Android. We use these Layouts according to our requirement. Well I will tell the scenarios also to you so that you can understand which Layout, where you should use .Different types of layouts are as follows 

  • Linear Layout (Vertical )
  • Linear Layout (Horizontal )
  • Relative Layout
  • Grid Layout
  • Frame Layout
  • Table Layout

Let’s start with Linear Layout
Linear Layout supports two types of orientation vertical and horizontal. In both types of orientation views will arrange one after another.
I want to throw some light on vertical orientation of Linear Layout. In this orientation when you put views inside this layout they will arrange vertically means one row can contain one view only.
If we talk about  horizontal  orientation of Linear Layout  so one row can have multiple views .
Now come to the implementation part. Don’t worry we will discuss all important tags of Linear Layout.

Now create a Linear Layout in your application and name it "firstlayout". 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   
 <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Akanksha's"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Androids" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hunt" />
</LinearLayout>


Bind this layout with your "MainActivity" by 

 setContentView(R.layout.firstlayout).
Like this:-

@Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_first);
              }

Make sure your Activity is declared in "manifest.xml" file.






Now Run your Application you will get output like this in vertical orientation.
In horizontal orientation like this:-



Let’s  discuss  some tags of Linear Layout in brief.
Setting width and height of layout is very essential part of any layout and Views.
android:layout_width="match_parent" android:layout_height="match_parent"


There are three types of width and height options are available in Android match_parent, fill_parent and wrap_content. you can use any one of these according to your need.
If you want to set width and height according to your content so use wrap_content.
If you want to set width and height according to its parent so use match_parent.
If you want to that width and height of the layout to fill on the screen so use fill_parent.

other then all these you can also set fixed width and height  .
Like android:layout_width="100dp"
But it is advisable to use one of these three match_parent, fill_parent , wrap_content
So that you can get the same look in aspect of width and height on different resolution’s tablets or Android Mobile.


 

5 comments:

  1. nice tutorial....
    so helpful. thanks

    ReplyDelete
  2. this nice one tutorial for all beginners of android developer its in a simple language
    thanks

    ReplyDelete
  3. Thanks akanksha this tutorial is very helpful, I liked it because it is very simple and easy to understand, you have explained everything step by step. I hope you will post next tutorial very soon.

    ReplyDelete
  4. thanks mam,
    this is very easy and understandable language.
    its very helpful.

    ReplyDelete
  5. hi........mam good morning
    i had read very carefully this post really its very importantant for new learner of android developer ,
    thanks mam i hope next tutorial will post soon i am waiting
    once agian thanks

    ReplyDelete