Saturday, 21 January 2017

Android Frame Layout

A Frame means an area which can hold view like an photoframe which can hold a photo and can arrange other photos as a stack in frame means on top of each other.

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.
Child views are drawn in a stack, with the most recently added child on top. The size of the FrameLayout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits). Views that are GONE are used for sizing only if setConsiderGoneChildrenWhenMeasuring() is set to true.
Here is the the image that shows an layout of an android frame layout -

Android FrameLayout Attributes are as below -

Attribute & Description
1
android:id
This is the ID which uniquely identifies the layout.
2
android:foreground
This defines the drawable to draw over the content and possible values may be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
3
android:foregroundGravity
Defines the gravity to apply to the foreground drawable. The gravity defaults to fill. Possible values are top, bottom, left, right, center, center_vertical, center_horizontal etc.
4
android:measureAllChildren
Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. Defaults to false.
Below is an simple example of Android Frame Layout -

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   
   <ImageView 
      android:src="@drawable/ic_launcher"
      android:scaleType="fitCenter"
      android:layout_height="250px"
      android:layout_width="250px"/>
   
   <TextView
      android:text="Frame Demo"
      android:textSize="30px"
      android:textStyle="bold"
      android:layout_height="fill_parent"
      android:layout_width="fill_parent"
      android:gravity="center"/>
</FrameLayout>

Here is an output of this screen -

Android Frame Layout is mostly used with Navigation Drawer to show the main content of navigation drawer controls after click.It will be discussed in upcoming blogs.


Friday, 20 January 2017

Android Table Layout

A Table is a collection of data which is arranged in Rows and Column format. Rows and columns can have some title and there can be table header, Which can be table name or something else that can describe which data we are going to show in table.

In the same way we can create table layout in which views are arranged in rows and columns.In Android table layout is not used so much as linear and relative layouts are used because it does not provide that much functionality as provided by these. 

We arrange the views in table layout using TableRow element.When we define a table layout then the parent tag in xml file is TableLayout then we define an element TableRow then we define the views that we want to arrange to create layout for any activity,as much we define view it will define the columns respectively.

A design of Android Table Layout is shown in below image -



Here are some table layout attributes -


Attribute & Description
1
android:id
This is the ID which uniquely identifies the layout.
2
android:collapseColumns
This specifies the zero-based index of the columns to collapse. The column indices must be separated by a comma: 1, 2, 5.
3
android:collapseColumns
The zero-based index of the columns to shrink. The column indices must be separated by a comma: 1, 2, 5.
4
android:stretchColumns
The zero-based index of the columns to stretch. The column indices must be separated by a comma: 1, 2, 5.

Below is the example of an Android Table Layout xml file -

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   
   <TableRow
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
  
      <TextView
         android:text="Time"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_column="1" />
   
      <TextClock
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/textClock"
         android:layout_column="2" />
   
   </TableRow>
   
   <TableRow>
 
      <TextView
         android:text="First Name"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_column="1" />
   
      <EditText
         android:width="200px"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
   </TableRow>
   
   <TableRow>
 
      <TextView
         android:text="Last Name"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_column="1" />
   
      <EditText
         android:width="100px"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
   </TableRow>
   
   <TableRow
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
  
      <RatingBar
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/ratingBar"
         android:layout_column="2" />
   </TableRow>
   
   <TableRow
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"/>
  
   <TableRow
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
  
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Submit"
         android:id="@+id/button"
         android:layout_column="2" />
   </TableRow>

</TableLayout>

Here is its output -





Wednesday, 18 January 2017

Android Relative Layout

Relative layout as its name suggest that in this type of layout we can specify the position of child views relative to each other. This position either can be related to its parent or other views in the layout.

Relative layout is very powerful layout because it can remove some unnecessary nesting of layouts and will keep your hierarchy in a flat manner that will improve performance of our application.It can help to replace some nesting of linear layouts.Nesting of layouts decrease performance of our application and make hierarchy complex.

Here are some important attributes of android relative layout -


Attribute & Description
1
android:id
This is the ID which uniquely identifies the layout.
2
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.
3
android:ignoreGravity
This indicates what view should not be affected by gravity.
Below are some more important attributes -

Attribute & Description
1
android:layout_above
Positions the bottom edge of this view above the given anchor view ID and must be a reference to another resource, in the form "@[+][package:]type:name"
2
android:layout_alignBottom
Makes the bottom edge of this view match the bottom edge of the given anchor view ID and must be a reference to another resource, in the form "@[+][package:]type:name".
3
android:layout_alignLeft
Makes the left edge of this view match the left edge of the given anchor view ID and must be a reference to another resource, in the form "@[+][package:]type:name".
4
android:layout_alignParentBottom
If true, makes the bottom edge of this view match the bottom edge of the parent. Must be a boolean value, either "true" or "false".
5
android:layout_alignParentEnd
If true, makes the end edge of this view match the end edge of the parent. Must be a boolean value, either "true" or "false".
6
android:layout_alignParentLeft
If true, makes the left edge of this view match the left edge of the parent. Must be a boolean value, either "true" or "false".
7
android:layout_alignParentRight
If true, makes the right edge of this view match the right edge of the parent. Must be a boolean value, either "true" or "false".
8
android:layout_alignParentStart
If true, makes the start edge of this view match the start edge of the parent. Must be a boolean value, either "true" or "false".
9
android:layout_alignParentTop
If true, makes the top edge of this view match the top edge of the parent. Must be a boolean value, either "true" or "false".
10
android:layout_alignRight
Makes the right edge of this view match the right edge of the given anchor view ID and must be a reference to another resource, in the form "@[+][package:]type:name".
11
android:layout_alignStart
Makes the start edge of this view match the start edge of the given anchor view ID and must be a reference to another resource, in the form "@[+][package:]type:name".
12
android:layout_alignTop
Makes the top edge of this view match the top edge of the given anchor view ID and must be a reference to another resource, in the form "@[+][package:]type:name".
13
android:layout_below
Positions the top edge of this view below the given anchor view ID and must be a reference to another resource, in the form "@[+][package:]type:name".
14
android:layout_centerHorizontal
If true, centers this child horizontally within its parent. Must be a boolean value, either "true" or "false".
15
android:layout_centerInParent
If true, centers this child horizontally and vertically within its parent. Must be a boolean value, either "true" or "false".
16
android:layout_centerVertical
If true, centers this child vertically within its parent. Must be a boolean value, either "true" or "false".
17
android:layout_toEndOf
Positions the start edge of this view to the end of the given anchor view ID and must be a reference to another resource, in the form "@[+][package:]type:name".
18
android:layout_toLeftOf
Positions the right edge of this view to the left of the given anchor view ID and must be a reference to another resource, in the form "@[+][package:]type:name".
19
android:layout_toRightOf
Positions the left edge of this view to the right of the given anchor view ID and must be a reference to another resource, in the form "@[+][package:]type:name".
20
android:layout_toStartOf
Positions the end edge of this view to the start of the given anchor view ID and must be a reference to another resource, in the form "@[+][package:]type:name".
Below is an example of relative layout -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:paddingLeft="16dp"
   android:paddingRight="16dp" >
   
   <EditText
      android:id="@+id/name"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:hint="@string/reminder" />
      
   <LinearLayout
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_alignParentStart="true"
      android:layout_below="@+id/name">
      
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="New Button"
         android:id="@+id/button" />
      
      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="New Button"
         android:id="@+id/button2" />
      
   </LinearLayout>

</RelativeLayout>
In above example we have created a relative layout with three child and one nested linear layout and we have arranged them relative to each other like linear layout is below to edit text. Here we can also remove linear layout and can arrange button to below edit text and second button to below of button.It will remove the use of linear layout.You should try it yourself.

Below is an output screen of this layout -



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.