Tuesday, 17 January 2017

Android Linear Layout

Android Linear Layout is an view group that arranges its views either in horizontal or vertical direction.The direction of views can be set with help of orientation property of linear layout in xml file or programmatically.

We can also set weightsum to linear layout and then can divide this weight to views so that views can occupy that much space in parent linear layout.We can also set gravity to views.

Here are some android linear layout attributes -


Attributes & Description
1
android:id
This is the ID which uniquely identifies the layout.
2
android:baselineAligned
This must be a boolean value, either "true" or "false" and prevents the layout from aligning its children's baselines.
3
android:baselineAlignedChildIndex
When a linear layout is part of another layout that is baseline aligned, it can specify which of its children to baseline align.
4
android:divider
This is drawable to use as a vertical divider between buttons. You use a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
5
android:gravity
This specifies how an object should position its content, on both the X and Y axes. Possible values are top, bottom, left, right, center, center_vertical, center_horizontal etc.
6
android:orientation
This specifies the direction of arrangement and you will use "horizontal" for a row, "vertical" for a column. The default is horizontal.
7
android:weightSum
Sum up of child weight
Here is an example of android linear layout we are creating an login screen in which we will have two edit text to enter user name ,password and two button one is to login and other is to get forgotten password.

Here is the code to create an linear layout using an xml file-


In the above code we have created one linear layout as root view its property gravity is set to center so that its contain can be in center of screen. We have given the orientation as vertical hence views will lie in vertical direction to each other.

Then there are two edit text views one of them is getting user name and other is taking password as input. Then we have created one linear layout to contain two button i.e. login and forgot password button.This linear layout has weightsum property having a value of 2 and this weightsum is divided into its views .

As you can see in the image that both button has width property value as 0 because when we define the weight property if its parent layout orientation is horizontal then its child view will have width property value as 0 and if linear layout orientation is vertical then child views height value will be zero.Here we have divided linear layout in two equal parts to provide equal space to buttons on screen.

And we have set the other properties like margin and hint text etc. so that layout looks better on screen otherwise there will be no space between views.It's output preview is shown in below image -


This is all about how to create Android Linear Layout in xml if you have any question regarding this please leave it in to comments.





Monday, 16 January 2017

Android UI Layouts(Android ViewGroup)

Android mobile application's building blocks are views, objects of View class and it acquire some rectangular space on the android mobile screen.

ViewGroups are collection of views and ViewGroup is an subclass of View class. ViewGroup provides an invisible area to hold the views like buttons,textview etc. that are used to create an layout of either an android activity or android fragment.

We can declare android layout in two different ways-

1. Defining Layouts in XML file
2. Creating Layouts at runtime or programmatically

An android viewgroup can contain other type of viewgroup or layout.Now we will see how to create an layout using XML file.

Creating Android Layout file Using XML File


<?xml version="1.0" encoding="utf-8"?>
<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/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" />
</LinearLayout>
It is really easy to create an layout in any xml file because android provide very straight forward and awesome library to provide suggestion while you are writing code.

In the above example we have create one layout of type linear which contains two views as Button and TextView. 

We will come to know about every type of layout in details in other blogs.

Loading the XML Resource

After creating an xml file for activity layout we have to load this xml file in our class file or java file to attach this with our activity or fragment using setContentView method for activity or  onCreateView for fragment in these methods we have to provide the resource file name as id.

here is an example to load an xml file named as main_layout.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
}
onCreate is an callback method for activity class and setcontentView is other method which takes parameter as layout or xml file id which are stored in R file under layout.

Attributes

Every view or viewgroup has its own attributes and there are some attributes that are common to every view or viewgroup that are explained below -

ID

We need to provide id to every view or view group to identify it and to use this in our java or class file where we defines the action of views.

Below is an example of creating an button and fetching in our activity class file-

android:id="@+id/my_button"
  1. Define a view/widget in the layout file and assign it a unique ID:
    <Button android:id="@+id/my_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/my_button_text"/>
  2. Then create an instance of the view object and capture it from the layout (typically in the onCreate() method):
    Button myButton = (Button) findViewById(R.id.my_button);

Android Layout Parameters/Attributes

Every layout has its own parameters but there are few parameters that are common to each and every layout.Every xml layout attribute starting with layout_something define layout parameters for the view are appropriate for the ViewGroup in which it is contained.


This image explain the hierarchy of views and viewgroups that can be combined together.Below is 
the list of some layout attributes -

Sr.NoAttribute & Description
1
android:id
This is the ID which uniquely identifies the view.
2
android:layout_width
This is the width of the layout.
3
android:layout_height
This is the height of the layout
4
android:layout_marginTop
This is the extra space on the top side of the layout.
5
android:layout_marginBottom
This is the extra space on the bottom side of the layout.
6
android:layout_marginLeft
This is the extra space on the left side of the layout.
7
android:layout_marginRight
This is the extra space on the right side of the layout.
8
android:layout_gravity
This specifies how child Views are positioned.
9
android:layout_weight
This specifies how much of the extra space in the layout should be allocated to the View.
10
android:layout_x
This specifies the x-coordinate of the layout.
11
android:layout_y
This specifies the y-coordinate of the layout.
12
android:layout_width
This is the width of the layout.
13
android:layout_width
This is the width of the layout.
14
android:paddingLeft
This is the left padding filled for the layout.
15
android:paddingRight
This is the right padding filled for the layout.
16
android:paddingTop
This is the top padding filled for the layout.
17
android:paddingBottom
This is the bottom padding filled for the layout.

Android Create Layout Programmatically 

Here is an example that show how to create an linear layout programmatically in an android application.You won't need often to create any layout programmatically but some time you may need this.using this example you will come to know basics of creating an linear layout programmatically in the same manner you can create other layouts as well.


You can click on the image to see in full size.

First of all we have created an object of linear layout class then specified its orientation.In the next line linear layout parameters are defined to match it's parent border or in other words we can say it is going to fill its parent.

In the next line we set this create layout as content in our activity and then we have created some views that will be contained by linear layout as child views.We have also defined their margin and other some required parameters this all be explained in upcoming blogs.