Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

LinearLayout and its Important Attributes with Examples in Android

LinearLayout is one of the most commonly used layout managers in Android. It lays out its children in a single direction, either vertically or horizontally, depending on the orientation attribute.

Important Attributes:

  1. android:orientation: Determines the direction in which child views are placed. Can be set to vertical or horizontal.

    android:orientation="vertical"
    
  2. android:gravity: Specifies how children should be aligned in terms of both the X and Y axes. Common values are center, left, right, top, bottom, center_vertical, and center_horizontal.

    android:gravity="center"
    
  3. android:layout_gravity: Used within child views of a LinearLayout to specify how that particular child should be aligned within the LinearLayout.

    android:layout_gravity="center_horizontal"
    
  4. android:layout_weight: Specifies the weight of the child view, allowing you to distribute the remaining space in the LinearLayout among its children. Child views with higher weight values will occupy more space. Note that setting weight requires setting the corresponding width (for horizontal) or height (for vertical) to 0dp.

    android:layout_weight="1"
    
  5. android:divider: Drawable used as a divider between items. When set, it needs the showDividers attribute to determine when to show it.

    android:divider="@drawable/divider"
    
  6. android:showDividers: Defines where dividers should appear in the list of children. Common values are none, beginning, end, middle.

    android:showDividers="middle"
    
  7. android:baselineAligned: If set to false, it will disable the alignment of children's baselines. By default, it's true.

    android:baselineAligned="false"
    

Example:

Here's an XML example demonstrating some of these attributes:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:divider="@drawable/divider"
    android:showDividers="middle">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Item 1"
        android:layout_gravity="center"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Item 2 with weight"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Item 3"
        android:layout_gravity="left"/>

</LinearLayout>

In this example, Item 1 and Item 3 are regular TextViews with different layout_gravity, and Item 2 expands to occupy all the remaining space because of its layout_weight attribute. The divider is shown between each item.

  1. Android LinearLayout gravity attribute example:

    • The gravity attribute in LinearLayout determines the alignment of child elements within the layout.

    • Example:

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:gravity="center">
      
          <Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Centered Button" />
      </LinearLayout>
      
  2. Orientation attribute in LinearLayout with examples:

    • The orientation attribute in LinearLayout defines the arrangement of child elements either horizontally or vertically.

    • Examples:

      <!-- Horizontal orientation -->
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
          <!-- Child elements go here -->
      </LinearLayout>
      
      <!-- Vertical orientation -->
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical">
          <!-- Child elements go here -->
      </LinearLayout>
      
  3. Weight attribute in LinearLayout for proportional sizing:

    • The layout_weight attribute in child elements of LinearLayout allows proportional sizing based on weights.

    • Example:

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
      
          <Button
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="Button 1" />
      
          <Button
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="2"
              android:text="Button 2" />
      </LinearLayout>
      
  4. Nested LinearLayout examples in Android:

    • LinearLayout can be nested within another LinearLayout for more complex UI designs.

    • Example:

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal">
              <!-- Nested horizontal layout -->
          </LinearLayout>
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
              <!-- Nested vertical layout -->
          </LinearLayout>
      </LinearLayout>
      
  5. Layout weightSum attribute in LinearLayout:

    • The weightSum attribute in LinearLayout specifies the sum of weights for proportional sizing.

    • Example:

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:weightSum="3">
      
          <Button
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:text="Button 1" />
      
          <Button
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="2"
              android:text="Button 2" />
      </LinearLayout>
      
  6. Android XML layout examples with LinearLayout:

    • Examples showcasing various attributes and configurations of LinearLayout.

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:gravity="center">
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, LinearLayout!" />
      
          <Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Click me" />
      </LinearLayout>