How to work with LayoutInflater in Kotlin

How to work with LayoutInflater in Kotlin
December 19, 2018 12 Comments Development Pushpendra Kumar

Typically, you don’t ever need to directly use a LayoutInflater. Android does most of the layout inflation for you when you call setContentView() in the onCreate() method of your activity. So you, as the programmer, are responsible for making sure the views are inflated. Now you want to inflate views in the context of a ListView. The Adapter class can do the inflation for you if you do not want to customize each item. But if you want to customize the views shown in a list, you will have to manually inflate each view with the LayoutInflater, since there is no other existing method you can use. But if you are here then in very simple way we can use like below code.

fun inflaterData() {
        val inflater: LayoutInflater = this@LocationInfoActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val viewGroup : ViewGroup = findViewById (
        for (i in 0 until list.size) {
            val model = list[i]
            val view : ViewGroup = inflater.inflate(R.layout.item_for_view, viewGroup, false) as ViewGroup
            val tvTitle : TextView = view.findViewById (

            tvTitle.text = model.label
            view.tag = i
            view.setOnClickListener { v ->
                val index = v.tag as Int
                Toast.makeText(applicationContext, index.toString(), Toast.LENGTH_LONG).show()
            viewGroup.addView(view, i)

Wish you good luck 🙂 Happy Coding

About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd ( This website I have made so that I can meet with new challenges and can share here.
Leave Comment
  1. 1


    Ԛuaⅼitʏ artiⅽles is the key to attract the users to pаy a visit
    the site, that’s what this site iѕ providing.

  2. 1


    The short answer is the lack of lambdas and functional operations. But Kotlin can work with them, so sorting, transforming, mapping or filtering are just a function call away.

  3. 1

    rooms anaheim

    This is my first time pay a visit at here and i am actually impressed to read all
    at alone place.

  4. 1

    n95 respirator

    Educative article, learned a lot. So glad I discovered
    your blog, and was able to learn new things. Keep posting articles, it’s really
    Best regards,
    Harrell Dencker

  5. 1


    Like!! Really appreciate you sharing this blog post.Really thank you! Keep writing.

  6. 1

    small business accounting

    Good post. I learn something totally new and challenging on blogs I stumbleupon on a daily basis. It will always be useful to read through content from other authors and use something from other web sites.

  7. 1

    best web hosting 2020

    This information is worth everyone’s attention. When can I find
    out more?

  8. 1

    best web hosting company

    My relatives always say that I am killing my time here at net, however I know I am getting knowledge
    everyday by reading such pleasant articles.

  9. 1

    adreamoftrains webhosting

    Thank you for sharing your info. I truly appreciate your efforts and I
    am waiting for your further post thank you once again. adreamoftrains web hosting services

  10. 1


    example requires a bit more work when we manually add the View to a ViewGroup. Adding our Button to our LinearLayout was more convenient with one line of code when

  11. 1

    El Reloj Antiguo XVI

    Wow, great blog. Want more.

  12. 1

    tantric massage london

    Thanks-a-mundo for the post.Thanks Again. Keep writing.


Leave a reply

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