Android LayoutInflator example - Without View Binding

Published September 24, 2021


LayoutInflator which is instantiate a xml layout into respected View object. Which will takes xml file all elements and create a view for the screen. So in this layoutInflate example we will add other layout to main layout by inflating the view.

 

So let's create

Step 1: Create android application

Step 2: To inflate view inside the screen we will use the Inflator class.

val layoutInflater: LayoutInflater = LayoutInflater.from(applicationContext)

 

In the xml file we will add below widgets

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cedeca"
    android:orientation="vertical"
    android:padding="16dp"
    >
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello Android!"
        android:textSize="25sp"
        android:layout_margin="25dp"
        />
</LinearLayout>

 

 

Step 3: Now let's create other layout which we will add inside main layout

child_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center"

    android:padding="10dp"
    >
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="350dp"
        android:scaleType="center"
        android:src="@drawable/bg"
        />
    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:layout_gravity="center"
        android:padding="10dp"
        />
</LinearLayout>

 

 

Step 4: Now update MainActivity.kt

package com.rrtutors.kotlinexample2021

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var main_container=findViewById<LinearLayout>(R.id.main_container);
        // Get the LayoutInflater from Context
        val layoutInflater: LayoutInflater = LayoutInflater.from(applicationContext)

        // Inflate the layout using LayoutInflater
        val view: View = layoutInflater.inflate(
            R.layout.child_layout, // Custom view/ layout
            main_container, // Root layout to attach the view
            false // Attach with root layout or not
        )

        // Find the text view from custom layout
        val label = view.findViewById<TextView>(R.id.txt)

        // Get the ImageView widget reference from custom view
        val imageView = view.findViewById<ImageView>(R.id.image_view)

        // Set the text of custom view's text view widget
        label.text = "Andorid development"



        // Finally, add the view/custom layout to the activity root layout
        main_container.addView(view,0)
    }
}

 

 

Step 5: Now run the application it will add second layout view to the main layout dynamically.

Android Layoutinflator example

 

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

15 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions