Search code examples
androidlayoutlayout-inflaterandroid-inflatefindviewbyid

What is the LayoutInflater in Android? How does it differs from findViewById?


An example LayoutInflater code:

View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.,parent,false);

Can't understand the work of LayoutInflater in Android. Tried to read the official documentation of the android but, couldn't get the concept.


Solution

  • The main difference between findViewById and LayoutInflater is that findViewById is used to access existing views in a layout, while LayoutInflater is used to create a new view hierarchy from an XML layout file.

    FindViewById is a method of the Activity class, while LayoutInflater is a separate class on it's own.

    FindViewById is used to access a view in the current layout, this means lets us find Views from layouts written in XML and returns a reference to their Java objects, while LayoutInflater is used to create a new view hierarchy from an XML layout file, so this class is the responsible for “inflating” the layouts.

    Hope it helps.