Android UI Components – Core components of screen

Introduction to Android UI Components

In this post, we will understand the core Android UI components. Before building an Application, we have to know about the core android UI components of the screen which create the User Interface in Android.

Activity is a means by which users can interact easily with an application. Activity by itself does not have a presence on the screen. Instead, it has to draw the screen using Views and ViewGroups.

Here, we will learn the details about creating user interfaces in Android, how users interact with them.

Views and ViewGroups

A view is a widget that shows on screen. Examples of views are buttons, labels, and text boxes.

A view comes from the base class android.view.View.

We can see one or more than one views are grouped together into a single View Group.

A ViewGroup provides the layout in which you can order the appearance and sequence of views.

Examples of ViewGroups are LinearLayout and FrameLayout. A ViewGroup comes from the base class android.view.ViewGroup.

Android supports the following ViewGroups:

  • LinearLayout
  • AbsoluteLayout
  • TableLayout
  • RelativeLayout
  • FrameLayout
  • ScrollView

Let’s see some of them in detail.

LinearLayout

The LinearLayout arranges views in a single column or a single row. Child views can be arranged either vertically or horizontally.

To see how LinearLayout works, consider the following elements typically contained in the main.xml file:

In the main.xml file, observe that the root element is <LinearLayout> and it has a <TextView> element contained within it. The <LinearLayout> element controls the order in which the views contained within it appears.

Each View and ViewGroup has a set of common attributes:

layout_widthIt Specifies the width of the View or ViewGroup.
layout_heightIt Specifies the height of the View or ViewGroup
layout_marginTopIt Specifies extra space on the top side of the View or ViewGroup
layout_marginBottomIt Specifies extra space on the bottom side of the View or ViewGroup
layout_marginLeftIt Specifies extra space on the left side of the View or ViewGroup
layout_marginRightIt Specifies extra space on the right side of the View or ViewGroup
layout_gravityIt Specifies how child Views are positioned
layout_weightIt Specifies how much of the extra space in the layout should be allocated to the View
layout_xIt Specifies the x-coordinate of the View or ViewGroup
layout_yIt Specifies the y-coordinate of the View or ViewGroup

RelativeLayout

The RelativeLayout enables you to specify how child views are positioned relative to each other. Consider the following main.xml file:

Notice that each view embedded within the RelativeLayout has attributes that enable it to align with another view.

These attributes are as follows:

layout_alignParentTop

layout_alignParentLeft

layout_alignLeft

layout_alignRight

layout_below

layout_centerHorizontal

Framelayout

The FrameLayout is a placeholder on the screen that we can use to display a single view. Views that you add to a FrameLayout are always anchored to the top left of the layout.

Consider the following content in main.xml:

Here, we have a FrameLayout within a RelativeLayout. Within the FrameLayout, we embed an ImageView.

ScrollView

A ScrollView is a special type of FrameLayout in which it enables users to scroll through a list of views that occupy more space than the physical display.

The ScrollView can contain only one child view or ViewGroup, which normally is a LinearLayout.

The following main.xml content shows a ScrollView containing a LinearLayout, which in turn contains some Button and EditText views:

Team RCV Academy

About Team RCV Academy

RCV Academy Team is a group of professionals working in various industries and contributing to tutorials on the website and other channels.

View all posts by Team RCV Academy