What is the real difference between :
return context.getLayoutInflater().inflate(R.layout.my_layout, null);
Inflate a new view hierarchy from the specified xml resource.
and
return View.inflate(context, R.layout.my_layout, null);
Inflate a view from an XML resource. This convenience method wraps the LayoutInflater class, which provides a full range of options for view inflation.
Inflate (One Pane) The inflate method inflates the View hierarchy and binds to it all it one step.
"Inflating" a view means taking the layout XML and parsing it to create the view and viewgroup objects from the elements and their attributes specified within, and then adding the hierarchy of those views and viewgroups to the parent ViewGroup.
The inflate() function of the Inflater class is used to uncompress the input data and fill the given buffer with the uncompressed data.
inflater.inflate will -Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error. In simple terms inflater. inflate is required to create view from XML .
Both are the same. The 2nd version is just a convenient and short method to do the task. If you see the source code of View.inflate()
method, you find :
/**
* Inflate a view from an XML resource. This convenience method wraps the {@link
* LayoutInflater} class, which provides a full range of options for view inflation.
*
* @param context The Context object for your activity or application.
* @param resource The resource ID to inflate
* @param root A view group that will be the parent. Used to properly inflate the
* layout_* parameters.
* @see LayoutInflater
*/
public static View inflate(Context context, int resource, ViewGroup root) {
LayoutInflater factory = LayoutInflater.from(context);
return factory.inflate(resource, root);
}
Which actually does the same job in the backend, the 1st method you mentioned does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With