Introduction

We have already discussed in my previous article that the LinearLayout arranges views in a single column or single row. It displays child View elements in a linear direction, either vertically or horizontally. Like a box it contains controls inside; controls are drawn one after each other either horizontally or vertically according to the Orientation of the layout.
When dealing with Linear layout there are five properties that we can deal with:
  1. Orientation
  2. Fill model
  3. Weight
  4. Gravity
  5. padding
As you have already learned the first three categories, in this article, I am going to explain the remaining two categories Gravity and Padding.
  1. Gravity: By default, widgets are positioned in the top-left of the screen but if you want you can specify the "gravity" for each component that's in the LinearLayout. This determines whether the component is left or right-aligned, and top or bottom aligned, or a combination of these.
    Basically, we use six types of gravity properties with LinearLayout:
    --left
    --center
    --center_vertical
    --center_horizontal
    --right
    --bottom
    I know based on the above documentation it was not very clear how the above layout parameters work. So, I read and find out more details and finally understood how to use gravity with LinearLayout. I will explain these properties with examples:
    left gravity
    1. <TextView
    2. android:layout_gravity="left"
    3. android:background= "#aa0000"
    4. android:textColor="#ffffffff"
    5. android:text="TextView with Left Gravity"
    6. android:layout_width="wrap_content"
    7. android:layout_height= "wrap_content"/>
    Output
    Gravity Left property
    center gravity
    1. <TextView
    2. android:layout_gravity="center"
    3. android:background= "#00aa00"
    4. android:textColor="#ffffffff"
    5. android:text="TextView with center Gravity"
    6. android:layout_width="wrap_content"
    7. android:layout_height="wrap_content"/>
    Output:
    Gravity Center property
    center_vertical gravity
    1. <TextView
    2. android:layout_gravity="center_vertical"
    3. android:background= "#0000aa"
    4. android:textColor="#ffffffff"
    5. android:text="TextView with center vertical Gravity"
    6. android:layout_width= "wrap_content"
    7. android:layout_height= "wrap_content" />
    Output:
    Gravity Center Vertical property
    center_horozontal gravity
    1. <TextView
    2. android:layout_gravity="center_horizontal"
    3. android:background="#00FFFF"
    4. android:textColor="#ffffffff"
    5. android:text="TextView with center horizontal Gravity"
    6. android:layout_width= "wrap_content"
    7. android:layout_height="wrap_content" />
    Output:
    Gravity Center Horizontal property
    right gravity
    1. <TextView
    2. android:layout_gravity="right"
    3. android:background= "#FF00FF"
    4. android:textColor="#ffffffff"
    5. android:text="TextView with right Gravity"
    6. android:layout_width= "wrap_content"
    7. android:layout_height= "wrap_content" />
    Output:
    Gravity Right property
    bottom
    1. <TextView
    2. android:layout_gravity="bottom"
    3. android:background= "#0999ff"
    4. android:textColor="#ffffffff"
    5. android:text="TextView with bottom Gravity"
    6. android:layout_width= "wrap_content"
    7. android:layout_height= "wrap_content" />
    Output:
    Gravity Bottom property
    Here is the complete application to understand all properties of gravity in a single application:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:orientation="vertical"
    4. android:layout_width="fill_parent"
    5. android:layout_height="fill_parent">
    6. <TextView
    7. android:layout_gravity="left"
    8. android:background= "#aa0000"
    9. android:textColor="#ffffffff"
    10. android:text="TextView with Left Gravity"
    11. android:layout_width="wrap_content"
    12. android:layout_height= "wrap_content"/>
    13. <TextView
    14. android:layout_gravity="center"
    15. android:background= "#00aa00"
    16. android:textColor="#ffffffff"
    17. android:text="TextView with center Gravity"
    18. android:layout_width= "wrap_content"
    19. android:layout_height= "wrap_content"/>
    20. <TextView
    21. android:layout_gravity="center_vertical"
    22. android:background= "#0000aa"
    23. android:textColor="#ffffffff"
    24. android:text="TextView with center vertical Gravity"
    25. android:layout_width= "wrap_content"
    26. android:layout_height= "wrap_content" />
    27. <TextView
    28. android:layout_gravity="center_horizontal"
    29. android:background= "#00FFFF"
    30. android:textColor="#ffffffff"
    31. android:text="TextView with center horizontal Gravity"
    32. android:layout_width= "wrap_content"
    33. android:layout_height= "wrap_content" />
    34. <TextView
    35. android:layout_gravity="right"
    36. android:background= "#FF00FF"
    37. android:textColor="#ffffffff"
    38. android:text="TextView with right Gravity"
    39. android:layout_width= "wrap_content"
    40. android:layout_height= "wrap_content" />
    41. <TextView
    42. android:layout_gravity="bottom"
    43. android:background= "#0999ff"
    44. android:textColor="#ffffffff"
    45. android:text="TextView with bottom Gravity"
    46. android:layout_width= "wrap_content"
    47. android:layout_height= "wrap_content" />
    48. </LinearLayout >
    Output:
    textviewG.gif
  2. Padding: The "android:padding" property sets the padding between widgets. If you specify the padding property to the container then the container with all of its widgets would be shifted by the value, if you specify it to a single widget then the contents of that widget would be shifted by the specified value.
    I set the layout_height property to "wrap_content" which means the height of the TextView should be increased according to the size of text, but because I use the padding property the height of the TextView is increased according to the size of the text as well as the value of "android:paddingTop".
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:orientation="vertical"
    4. android:paddingTop="20px"
    5. android:layout_width="fill_parent"
    6. android:layout_height="fill_parent">
    7. <TextView
    8. android:text= "First Layout"
    9. android:background= "#aa0000"
    10. android:textColor="#ffffffff"
    11. android:layout_width= "fill_parent"
    12. android:paddingTop="20px"
    13. android:layout_height= "wrap_content"/>
    14. <TextView
    15. android:text= "Second Layout"
    16. android:background= "#00aa00"
    17. android:textColor="#ffffffff"
    18. android:layout_width= "fill_parent"
    19. android:paddingTop="20px"
    20. android:layout_height= "wrap_content"/>
    21. <TextView
    22. android:text= "Thired Layout"
    23. android:background= "#0000aa"
    24. android:textColor="#ffffffff"
    25. android:layout_width= "fill_parent"
    26. android:paddingTop="20px"
    27. android:layout_height= "wrap_content" />
    28. </LinearLayout>
    Output:
    Padding Property
I hope you enjoy learning how to work with LinearLayout in Android using Visual Studio 2010.