A layout defines the structure for a user interface in your app, such as in an activity. All elements in the layout are built using a hierarchy of View and ViewGroup objects. A View usually draws something the user can see and interact with. Whereas a ViewGroup is an invisible container that defines the layout structure for View and other ViewGroup objects, as shown in figure.


The View objects are usually called “widgets” and can be one of many subclasses, such as Button or TextView. The ViewGroup objects are usually called “layouts” can be one of many types that provide a different layout structure, such as LinearLayout or ConstraintLayout .



You can declare a layout in two ways:

  • Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts.

    You can also use Android Studio’s Layout Editor to build your XML layout using a drag-and-drop interface.

  • Instantiate layout elements at runtime. Your app can create View and ViewGroup objects (and manipulate their properties) programmatically.


Declaring your UI in XML allows you to separate the presentation of your app from the code that controls its behavior. Using XML files also makes it easy to provide different layouts for different screen sizes and orientations

Android Layout Types





Linear Layout

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally.


Relative Layout

RelativeLayout is a view group that displays child views in relative positions.


Table Layout

TableLayout is a view that groups views into rows and columns.


Absolute Layout

AbsoluteLayout enables you to specify the exact location of its children.


Frame Layout

The FrameLayout is a placeholder on screen that you can use to display a single view.


List View

ListView is a view group that displays a list of scrollable items.


Grid View

GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.


Layout Attributes



Each layout has a set of attributes which define the visual properties of that layout. There are few common attributes among all the layouts and their are other attributes which are specific to that layout. Following are common attributes and will be applied to all the layouts:

Sr.No Attribute & Description


This is the ID which uniquely identifies the view.



This is the width of the layout.



This is the height of the layout



This is the extra space on the top side of the layout.



This is the extra space on the bottom side of the layout.



This is the extra space on the left side of the layout.



This is the extra space on the right side of the layout.



This specifies how child Views are positioned.



This specifies how much of the extra space in the layout should be allocated to the View.



This specifies the x-coordinate of the layout.



This specifies the y-coordinate of the layout.



This is the width of the layout.



This is the width of the layout.



This is the left padding filled for the layout.



This is the right padding filled for the layout.



This is the top padding filled for the layout.



This is the bottom padding filled for the layout.

Android Widgets

 1)Overview about AppWidgets
 2) Home screen widgets



Home screen widgets are broadcast receivers which provide interactive components. They are primarily used on the Android home screen. They typically display some kind of data and allow the user to perform actions with them. For example, a widget can display a short summary of new emails and if the user selects an email, it could start the email application with the selected email.

To avoid confusion with views (which are also called widgets), this text uses the term home screen widgets, if it speaks about widgets.

A widget runs as part of the process of its host. This requires that the widget preserves the permissions of their application.

Widget use RemoteViews to create their user interface. A RemoteView can be executed by another process with the same permissions as the original application. This way the widget runs with the permissions of its defining application.

The user interface for a Widget is defined by a broadcast receiver. This receiver inflates its layout into an object of type RemoteViews. This object is delivered to Android, which hands it over the home screen application.

3 Steps to create a Widget

To create a widget, you:

  • Define a layout file

  • Create an XML file (AppWidgetProviderInfo) which describes the properties of the widget, e.g. size or the fixed update frequency.

  • Create a BroadcastReceiver which is used to build the user interface of the widget.

  • Enter the Widget configuration in the AndroidManifest.xml file.

  • Optional you can specify a configuration activity which is called once a new instance of the widget is added to the widget host.


4. Widget size


Before Android 3.1 a widget always took a fixed amount of cells on the home screen. A cell is usually used to display the icon of one application. As a calculation rule you should define the size of the widget with the formula: ((Number of columns / rows) * 74) - 2. These are device independent pixels and the -2 is used to avoid rounding errors.

As of Android 3.1 a widget can be flexible in size, e.g., the user can make it larger or smaller. To enable this for widget, you can use the android:resizeMode="horizontal|vertical" attribute in the XML configuration file for the widget