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

Android LinearLayout in Kotlin

LinearLayout is a popular layout used in Android applications to arrange UI components in either a vertical or horizontal manner. The direction can be set using the android:orientation attribute.

Here's a simple guide to creating a LinearLayout programmatically in Kotlin and via XML.

1. Using XML:

This is the most common way of defining layouts in Android.

  • Vertical LinearLayout:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 2"/>

</LinearLayout>
  • Horizontal LinearLayout:

Replace android:orientation="vertical" with android:orientation="horizontal".

2. Programmatically using Kotlin:

To create a LinearLayout programmatically, you can do the following:

val linearLayout = LinearLayout(this)
linearLayout.orientation = LinearLayout.VERTICAL
linearLayout.layoutParams = LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.MATCH_PARENT
)
linearLayout.setPadding(16, 16, 16, 16)

val button1 = Button(this)
button1.layoutParams = LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT
)
button1.text = "Button 1"
linearLayout.addView(button1)

val button2 = Button(this)
button2.layoutParams = LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT
)
button2.text = "Button 2"
linearLayout.addView(button2)

setContentView(linearLayout)

This code should ideally be in the onCreate method of your Activity. It sets the activity's content to a LinearLayout containing two buttons.

When using LinearLayout, you can use the android:weight attribute (or layout_weight property in Kotlin) to specify how child views divide up the space. It's especially useful in cases where you want views to occupy a proportional amount of space within the LinearLayout.

Remember, while LinearLayout is versatile, excessive nesting can harm performance. If you find yourself deeply nesting LinearLayouts, consider using alternative layouts like ConstraintLayout to flatten your view hierarchy and improve performance.

  1. LinearLayout in Android using Kotlin example:

    To use LinearLayout in XML layout with Kotlin, you can define it as follows:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <!-- Add your UI elements here -->
    
    </LinearLayout>
    
  2. Creating LinearLayout programmatically in Kotlin:

    val linearLayout = LinearLayout(context)
    linearLayout.layoutParams = LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
    )
    linearLayout.orientation = LinearLayout.VERTICAL
    
    // Add your UI elements to linearLayout programmatically
    
    // Finally, add linearLayout to the parent view
    parentView.addView(linearLayout)
    
  3. Android Kotlin LinearLayout gravity and layout_weight:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical">
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Left"/>
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Right"/>
    
    </LinearLayout>
    

    Use android:gravity for the gravity within the LinearLayout and android:layout_weight to distribute space.

  4. Nested LinearLayout in Android with Kotlin:

    Nesting LinearLayouts involves placing one LinearLayout inside another. This allows for more complex and hierarchical UI designs.

    <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">
    
            <!-- Add views for the first row -->
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <!-- Add views for the second row -->
    
        </LinearLayout>
    
    </LinearLayout>
    
  5. LinearLayout orientation in Kotlin Android:

    The android:orientation attribute in LinearLayout determines whether the layout is horizontal or vertical.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <!-- Horizontal LinearLayout -->
    
    </LinearLayout>
    
  6. LinearLayout weightSum attribute in Kotlin:

    The android:weightSum attribute in LinearLayout allows you to specify a maximum weight sum. This is useful when using layout_weight to distribute space among child views.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Left"/>
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Right"/>
    
    </LinearLayout>
    

    In this example, each TextView gets half of the available space due to the equal weight and the weight sum being set to 2.