Placing the components of a LinearLayout

The purpose of layout is to arrange the various components within itself according to a particular scheme and the LinearLayout is the simplest of layout which is proposed by default when you create an Android project.

  1. Create a new Android project
  2. Edit the file res/layout/main.xml:
  3. Edit the file res/values/strings.xml:
  4. Create the file res/values/colors.xml:
  5. Verify that the main class has the method:

The end result seen by the emulator is as follows:

The Linearlayout arrange the components in or portrait or landscape orientation, you can verify this aspect replacing android: orientation = “vertical” with android: orientation = “vertical”in the file main.xml.
The dimensions of the components are mainly determined by android:layout_width and layout_height whose values can be set to:

  • fill_parent: the component fills all available space (deprecated)
  • match_parent: the component fills all available space
  • wrap_content: the component gets enough space to show their contents
  • [a number]: specify an exact number in px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters)

The dimensions of the components can be increased or decreased value to android:layout_weight, and the components for which a value is specified for android:layout_weight have relative size proportionate to that value.
android:layout_gravity e android:gravity set similar characteristics but on different objects:

  • android:layout_gravity: sets the positioning of the component relative to the layout
  • android:gravity: sets the positioning of the body of the component relative to the same component.

You can specify multiple values separated by the character “|”, for example: android:gravity=”bottom|center”.
android:padding sets the distance between the body of the component and the edge of the component.
android:layout_margin sets the distance between the component from the edge of the layout or other elements.
android:padding e android:layout_margin can also be set for layout.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.