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 (R.id.linear_layout)
        viewGroup!!.removeAllViews()
        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 (R.id.tvTitle)

            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

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

    gallant

    Ԛ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.

    Reply
  2. 1

    jahrastafara

    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.

    Reply
  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.

    Reply
  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
    valuable.
    Best regards,
    Harrell Dencker

    Reply
  5. 1

    ปั้มไลค์

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

    Reply
  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.

    Reply
  7. 1

    best web hosting 2020

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

    Reply
  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.

    Reply
  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

    Reply
  10. 1

    Androidena

    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

    Reply
  11. 1

    El Reloj Antiguo XVI

    Wow, great blog. Want more.

    Reply
  12. 1

    tantric massage london

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

    Reply

Leave a reply

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