8. Views and layout manager

8.1. View – The user interface widgets in Android

A view in Android represents a widget, e.g., a button, or a layout manager. The Android SDK provides standard views (widgets), e.g., via the Button, TextView, EditText classes. Additional libraries provide more complex widgets, for example, RecyclerView.

All views in Android extend the android.view.View class, this class provides a lot of base functionality for subclasses. For example, it provides the base support for touch.

The main packages for views are part of the android.view namespace for all the base classes and android.widget for the default widgets of the Android platform.

8.2. Using a layout manager

A layout manager is responsible for layouting itself and its child Views It is a subclass of ViewGroup.

Android supports different default layout managers.

The most relevant layout managers in Android are:

  • ConstraintLayout – provided by an extra library
  • LinearLayout
  • FrameLayout
  • RelativeLayout
  • GridLayout

8.3. Layout attributes

All layout manager can be configured via attributes. Children can also define attributes which may be evaluated by their parent layout.

Children can specify their desired width and height via the following attributes.

  • android:layout_width – Defines the width of the widget.
  • android:layout_height – Defines the height of the widget.

Views can define their size. This can be done in units of measurement or via pre-defined layout values. For example, as 100dp.

The match_parent value tells the application to maximize the widget in its parent. The wrap_content value tells the layout to allocate the minimum amount so that the widget is rendered correctly. The effect of these elements is demonstrated in the following graphics.

8.4. Constraint layout

Constraint layout is provided by an external library. It allows you to use a flat view hierarchy and has great performance. Also the design tools support constraint layout very well. New projects should prefer the usage of constraint layout.

8.5. FrameLayout

FrameLayout is a layout manager which draws all child elements on top of each other. This allows to create nice visual effects.

The following screenshot shows the Gmail application which uses FrameLayout to display several button on top of another layout.

8.6. LinearLayout

LinearLayout puts all its child elements into a single column or row depending on the android:orientation attribute. Possible values for this attribute are horizontal and vertical. horizontal is the default value.

If horizontal is used, the child elements are layouted as indicated by the following picture.

LinearLayout can be nested to achieve more complex layouts.

LinearLayout supports assigning a weight to individual children via the android:layout_weight layout parameter. This value specifies how much of the extra space in the layout is allocated to the corresponding view. If, for example, you have two widgets and the first one defines a layout_weight of 1 and the second of 2, the first will get 1/3 of the available space and the other one 2/3. You can also set the layout_width to zero to always have a certain ratio.

8.7. ScrollView

The ScrollView or the HorizontalScrollView class is not a layout manager. It is useful to make views available, even if they do not fit onto the screen. A scroll view can contain one view, e.g., a layout manager containing more views. If the child view is too large, scroll view allows scrolling the content.


8.a Android Buttons


      In Android, just use “android.widget.Button” class to display a normal button.

open you main_content.xml file and add a button to it

Edit the Corresponding Java File and add the following code:



8.b Android TextView


Creating a TextView inside an Android layout XML file is done by inserting a TextView element into the layout file at the place where you want the TextView to be displayed. Here is an example layout file declaring a TextView:


Once the layout file is used as the content view of an Activity subclass you can obtain a reference to the TextView instance like this:

8.c Android Edit Text