A Quick Introduction to View, ViewGroups and Layout Managers in Xamarin Android

nilan avtar

Written by:

Nilanchala,  8 min read,  updated on September 17, 2023

Views and ViewGroups

Everything that you see in an Android app is a View; buttons, labels, text boxes, and radio buttons are all examples of Views. Views are organized in a hierarchy using various types of ViewGroups. A ViewGroup is a special kind of View which is used to arrange (layout) other Views on the screen.

User Interface Widgets

Android provides a comprehensive set of user interface widgets that can be used to build a rich user experience. All of these widgets are subtypes of  View and can be organized into sophisticated layouts using various types of ViewGroups. All of the user interface widgets can be found in the  android.widget package within the Application Framework.

Some of the common UI widgets includes;

  • TextView
  • EditText
  • Radio Buttons and Radio button group
  • CheckBox
  • Adapter views such as List View, GridView
  • WebView
  • Spinners, etc.

Declarative Versus Programmatic View Creation

Views and ViewGroups can be created using two different methods, programmatically or declaratively. When using a programmatic approach,  a developer makes API calls to create and position each individual View on screen. When using a declarative approach, a developer creates XML layout files that specify how Views should be arranged. Visual layout designer embedded to Xamarin Studio and Visual Studio IDE provides extensive support for creating the layout using drag and drop approach. In case of XML declaration, you need to remember each element and property to control the view/layout behavior.

The declarative method enjoys several advantages stated as follows:

  • Provides better separation of the visual design of an application from the processing logic
  • Allows multiple layouts to be created to support multiple devices or device configurations with a single code base
  • Development tools, such as Android Studio, Android plugin for Eclipse and Xamarin Studio Android designer, allow you to view the user interface as you build it, without needing to compile and execute your application after each change

While most developers prefer the declarative method of view creation, in practice, some combination of programmatic and declarative methods are often required.

Xamarin Android Layout Managers

Xamarin.Android offers a collection of view group classes that acts as container for views. These containers are refereed as layout managers. Each of the layout managers can contain one more child views and provides a specific strategy to manage the size and position of its children. For example, the Linear Layout class places its children either horizontally or vertically, one view adjacent to the other.

Following table lists the different types of layout managers available in Xamarin android.

Layout ManagerDescription
Linear LayoutPosition its child views either horizontally or vertically
Relative LayoutPosition its child view relative to others child or parent.
Table LayoutPosition its child on tabular form
Frame LayoutAllows to change content dynamically
Grid LayoutPosition child views in a grid format
Absolute Layout (Deprecated)Allows absolute positioning of child. This layout manager is deprecated and strictly not recommended to use for your app.

All the above layout managers are derived from the View class; therefore all the layout managers can be nested one inside of one another. You can also create a custom layout by extending ViewGroup classes for specific requirements for your project.