Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : What is the difference between View.inflate and getLayoutInflater().inflate?

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.

like image 523
Denis Avatar asked Feb 22 '16 08:02

Denis


People also ask

What is view inflate in Android?

Inflate (One Pane) The inflate method inflates the View hierarchy and binds to it all it one step.

What does inflated view mean?

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

What does Inflater inflate do in Android?

The inflate() function of the Inflater class is used to uncompress the input data and fill the given buffer with the uncompressed data.

What does Inflater inflate do?

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 .


1 Answers

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.

like image 166
Tafveez Mehdi Avatar answered Sep 24 '22 00:09

Tafveez Mehdi