A Brief Guide To Knowing Layouts in Android

Posted By : Kishan Gupta | 13-Apr-2018

In Android, there are many types of layouts are available but Linear layout, Relative layout, and Frame Layouts are most commonly used the layout in android.

 

So today, I am gonna discuss these very famous layouts of android but first, let's understand what layout is in android

 

A layout defines the structure body for a user interface in our application such as in fragment, or in an activity. All elements are created using a hierarchy of ViewGroup and View objects in the layout. A ViewGroup is layout container which contains an invisible layout that defines the layout structure for View and ViewGroup objects whereas a View usually built something that user can see and deal with.

 

The View objects are usually called "widgets" and can be one of many subclasses, such as Button or TextView. The ViewGroup objects are commonly known as "layouts" can be of many types that provide a various layout structure like Frame Layout or Relative Layout.

 

Now, let's start with layouts:-

 

Linear Layout

 

It is a view group that aligns all of its children in a single particular direction, either vertically or horizontally. It also facilitates assigning a weight to individual children (android: layout_weight ). 

 

The followings are important attributes of Linear Layout:-

 

  • Orientation: Align direction either horizontally or vertically which is an attribute of node Linear Layout, set android: orientation ="vertical" for vertical orientation and android: orientation="horizontal" for horizontal orientation to specify child view display
  • Weight: It specifies how much space is allocated to each view in relative to other. For Example, in an e-mail application, we can give less weight to 'To' and 'Subject', and more weight to 'Message'.
  • Layout Gravity: it defines the placement of the created view itself.
  • Gravity: it defines placement of view's contents. For example, if a view spans entire screen, ut has only one line of text, then we can decide whether it should be displayed on top, center or bottom.

 

It can't be best fit to all pages as the screen of mobile devices are smaller and different dimensions are available, for such requirements "Linear Layout" is not the best fit.

 

Relative Layout

 

It is a view group that specifies how views of the child are positioned in relation to each other. Then, positions of each view can be specified as relative to sibling elements. we can arrange components below, top, right, left in relation to one another.

 

It is a very powerful utility for designing a user interface because it can eliminate nested view groups and keeps our hierarchy flat, which improves performance. if we find ourselves using several nested Linear Layout groups, we may be able to replace them with a single Relative Layout.

 

The followings are the important attribute of Relative Layout:-

 

  • Position relative to screen: we can align a view relative to a screen using various attributes.
  • Position relative to other views: we can align a view relative to another view using above, below,toLeftOf,toRightOf etc.
  • Margins: we can provide margins as well in relative layout

 

Frame Layout

 

It is a view group that is designed to display a single item at a time. In Frame Layout, we can have multiple elements but the positioning of each element will be based on the top left of the screen. View elements that overlap will be displayed overlapping on one another elements.

 

The followings are the important attribute of Frame Layout:-

 

  • ForegroundGravity: This attribute defines the gravity to apply in the foreground of the layout. Possible values are right, left, bottom, top, center, center_horizontal, center_vertical etc. we can set it with android:foregroundGravity.
  • MeasureAllChildren: This attribute determines whether to measure all children of layout or just those which are in the VISIBLE or INVISIBLE state. By default it is false. we can set it with android:measure all children.

About Author

Author Image
Kishan Gupta

Young, Enthusiastic, Never give up Attitude are the perfect words to define Kishan. He is committed to learn and dedicated to work. He believes in "Think Big Get Big"

Request for Proposal

Name is required

Comment is required

Sending message..