Home > Software design >  Having difficulty understanding layoutinflater Android Studio (Kotlin)
Having difficulty understanding layoutinflater Android Studio (Kotlin)

Time:06-30

I am aware of a post that was made before this pertaining to the same topic, however it was in java.

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
    // create a new view
    val adapterLayout = LayoutInflater.from(parent.context)
        .inflate(R.layout.list_item, parent, false)

    return ItemViewHolder(adapterLayout)
}

This was the definition the docs gave me, "Instantiates a layout XML file into its corresponding android.view.View objects." What does this mean?

Then there is the inflate method which the docs state "Inflate a new view hierarchy from the specified XML node."

Could someone explain to me in simpler terms what Layoutinflater and it's method inflater do?

CodePudding user response:

TL;DR The "inflater" reads the XML layout file, which defined what View objects to create and where to put them, and actually does the View creation.

Some helpful terms:

  • View - Anything that inherits from the View class, e.g. TextView or Button
  • Layout - an arrangement of different View objects on the screen (this is what you see looking at the app)
  • XML Layout File - An XML text file that describes a layout - specifically what Views go in that layout and how to position them (e.g. activity_main.xml). The XML file is not the layout, it is a description of how to build the layout.
  • Inflater - A routine/object that takes an XML layout file, reads it, and actually creates the Layout (arrangement of View objects). Calling inflate on the inflater returns a View object that contains everything you defined in the XML file.

More Details

A screen you see in an Android app is a collection of "Views". These may be things like TextView, ConstraintLayout, EditText, Button, etc... - all different types of views (they all inherit from the View class).

When you build up a layout of those views you typically use an XML file to define what views to create and where to position those views. The XML file is not the view itself, it is just a description of how the views should be constructed and positioned.

The layout inflater takes that XML file and actually goes about building the views as you see them on the screen in the app. When you call inflate it reads all the data about the views from the XML file, creates instances of those views, and positions them in the parent view container based on what you told it to do in the XML file.

In the example code you showed (from a RecyclerView adapter) the XML file it is referring to is the one that describes how to arrange the views in a given row in the RecyclerView. Once the adapter "inflates" the view for that row, then the actual view objects (e.g. TextViews) have been created and positioned within that row.

The RecyclerView adapter will call this method multiple times, to "inflate" a new unique view instance for each displayed row. The "recycler" part of the RecyclerView means that it will try not to call this more than necessary, and will re-use the views on new rows where it can as you scroll.

Additional Reading

  • Related