Sunday, April 8, 2018

What does LayoutInflater class do?



java.lang.Object
   ↳android.view.LayoutInflater

layout inflater is a java class that extends object


There are two ways in which we can make a view (textView, imageView, ...,),

First we create the view in Xml and then call the same in Java by findViewById method.


Second using the LayoutInflater here we get the layout and define the views.

The first one is simple and is used only to display few textView,imageView....,

If the list of item is large, we need to use the inflater method to reduce the code and also to reduce
 the memory consumed.


Instantiates a layout XML file into its corresponding View objects. It is never used directly. Instead, use getLayoutInflater() orgetSystemService(Class) to retrieve a standard LayoutInflater instance
 that is already hooked up to the current context and correctly configured for the device you are
running on. when you run setContentView(layout file name), you can run findViewById(id of the widget).
          You dont need to do something like xyz.findViewById. The context of your app is set to that

layout file and all findBiewById call will refer to that layout file.There are cases when you need to pick up one more layout file, like a CustomDialog, ListElement  or a Custom Toast. At this time you wont want to create a Activity just for these small UI
components, that that time you programmatically need to get a programmatic reference to your
layout file, so that you can run findViewById on it.



No comments:

Post a Comment