LayoutInflater


public abstract class LayoutInflater
extends Object

java.lang.Object
   ↳ android.view.LayoutInflater


Instantiates a layout XML file into its corresponding View objects. It is never used directly. Instead, use Activity.getLayoutInflater() or Context.getSystemService 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.

To create a new LayoutInflater with an additional Factory for your own views, you can use cloneInContext(Context) to clone an existing ViewFactory, and then call setFactory(Factory) on it to include your Factory.

For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R.something file.)

Note: This class is not thread-safe and a given instance should only be accessed by a single thread.

Summary

Nested classes

interface LayoutInflater.Factory

 

interface LayoutInflater.Factory2

 

interface LayoutInflater.Filter

Hook to allow clients of the LayoutInflater to restrict the set of Views that are allowed to be inflated. 

Protected constructors

LayoutInflater(Context context)

Create a new LayoutInflater instance associated with a particular Context.

LayoutInflater(LayoutInflater original, Context newContext)

Create a new LayoutInflater instance that is a copy of an existing LayoutInflater, optionally with its Context changed.

Public methods

abstract LayoutInflater cloneInContext(Context newContext)

Create a copy of the existing LayoutInflater object, with the copy pointing to a different Context than the original.

final View createView(Context viewContext, String name, String prefix, AttributeSet attrs)

Low-level function for instantiating a view by name.

final View createView(String name, String prefix, AttributeSet attrs)

Low-level function for instantiating a view by name.

static LayoutInflater from(Context context)

Obtains the LayoutInflater from the given context.

Context getContext()

Return the context we are running in, for access to resources, class loader, etc.

final LayoutInflater.Factory getFactory()

Return the current Factory (or null).

final LayoutInflater.Factory2 getFactory2()

Return the current Factory2.

LayoutInflater.Filter getFilter()
View inflate(int resource, ViewGroup root)

Inflate a new view hierarchy from the specified xml resource.

View inflate(XmlPullParser parser, ViewGroup root)

Inflate a new view hierarchy from the specified xml node.

View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)

Inflate a new view hierarchy from the specified XML node.

View inflate(int resource, ViewGroup root, boolean attachToRoot)

Inflate a new view hierarchy from the specified xml resource.

View onCreateView(Context viewContext, View parent, String name, AttributeSet attrs)

Version of onCreateView(android.view.View, java.lang.String, android.util.AttributeSet) that also takes the inflation context.

void setFactory(LayoutInflater.Factory factory)

Attach a custom Factory interface for creating views while using this LayoutInflater.

void setFactory2(LayoutInflater.Factory2 factory)

Like setFactory(Factory), but allows you to set a Factory2 interface.

void setFilter(LayoutInflater.Filter filter)

Sets the Filter to by this LayoutInflater.

Protected methods

View onCreateView(View parent, String name, AttributeSet attrs)

Version of onCreateView(java.lang.String, android.util.AttributeSet) that also takes the future parent of the view being constructed.

View onCreateView(String name, AttributeSet attrs)

This routine is responsible for creating the correct subclass of View given the xml element name.

Inherited methods

Protected constructors

LayoutInflater

Added in API level 1
protected LayoutInflater (Context context)

Create a new LayoutInflater instance associated with a particular Context. Applications will almost always want to use Context.getSystemService() to retrieve the standard Context.INFLATER_SERVICE.

Parameters
context Context: The Context in which this LayoutInflater will create its Views; most importantly, this supplies the theme from which the default values for their attributes are retrieved.

LayoutInflater

Added in API level 1
protected LayoutInflater (LayoutInflater original, 
                Context newContext)

Create a new LayoutInflater instance that is a copy of an existing LayoutInflater, optionally with its Context changed. For use in implementing cloneInContext(Context).

Parameters
original LayoutInflater: The original LayoutInflater to copy.

newContext Context: The new Context to use.

Public methods

cloneInContext

Added in API level 1
public abstract LayoutInflater cloneInContext (Context newContext)

Create a copy of the existing LayoutInflater object, with the copy pointing to a different Context than the original. This is used by ContextThemeWrapper to create a new LayoutInflater to go along with the new Context theme.

Parameters
newContext Context: The new Context to associate with the new LayoutInflater. May be the same as the original Context if desired.

Returns
LayoutInflater Returns a brand spanking new LayoutInflater object associated with the given Context.

createView

Added in API level 29
public final View createView (Context viewContext, 
                String name, 
                String prefix, 
                AttributeSet attrs)

Low-level function for instantiating a view by name. This attempts to instantiate a view class of the given name found in this LayoutInflater's ClassLoader.

There are two things that can happen in an error case: either the exception describing the error will be thrown, or a null will be returned. You must deal with both possibilities -- the former will happen the first time createView() is called for a class of a particular name, the latter every time there-after for that class name.

Parameters
viewContext Context: The context used as the context parameter of the View constructor This value cannot be null.

name String: The full name of the class to be instantiated. This value cannot be null.

prefix String: This value may be null.

attrs AttributeSet: The XML attributes supplied for this instance. This value may be null.

Returns
View View The newly instantiated view, or null.

Throws
ClassNotFoundException
InflateException

createView

Added in API level 1
public final View createView (String name, 
                String prefix, 
                AttributeSet attrs)

Low-level function for instantiating a view by name. This attempts to instantiate a view class of the given name found in this LayoutInflater's ClassLoader. To use an explicit Context in the View constructor, use createView(android.content.Context, java.lang.String, java.lang.String, android.util.AttributeSet) instead.

There are two things that can happen in an error case: either the exception describing the error will be thrown, or a null will be returned. You must deal with both possibilities -- the former will happen the first time createView() is called for a class of a particular name, the latter every time there-after for that class name.

Parameters
name String: The full name of the class to be instantiated.

prefix String

attrs AttributeSet: The XML attributes supplied for this instance.

Returns
View View The newly instantiated view, or null.

Throws
ClassNotFoundException
InflateException

from

Added in API level 1
public static LayoutInflater from (Context context)

Obtains the LayoutInflater from the given context.

Parameters
context Context

Returns
LayoutInflater

getContext

Added in API level 1
public Context getContext ()

Return the context we are running in, for access to resources, class loader, etc.

Returns
Context

getFactory

Added in API level 1
public final LayoutInflater.Factory getFactory ()

Return the current Factory (or null). This is called on each element name. If the factory returns a View, add that to the hierarchy. If it returns null, proceed to call onCreateView(name).

Returns
LayoutInflater.Factory

getFactory2

Added in API level 11
public final LayoutInflater.Factory2 getFactory2 ()

Return the current Factory2. Returns null if no factory is set or the set factory does not implement the Factory2 interface. This is called on each element name. If the factory returns a View, add that to the hierarchy. If it returns null, proceed to call onCreateView(name).

Returns
LayoutInflater.Factory2

getFilter

Added in API level 1
public LayoutInflater.Filter getFilter ()

Returns
LayoutInflater.Filter The Filter currently used by this LayoutInflater to restrict the set of Views that are allowed to be inflated.

inflate

Added in API level 1
public View inflate (int resource, 
                ViewGroup root)

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters
resource int: ID for an XML layout resource to load (e.g., R.layout.main_page)

root ViewGroup: Optional view to be the parent of the generated hierarchy. This value may be null.

Returns
View The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.

inflate

Added in API level 1
public View inflate (XmlPullParser parser, 
                ViewGroup root)

Inflate a new view hierarchy from the specified xml node. Throws InflateException if there is an error. *

Important   For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.

Parameters
parser XmlPullParser: XML dom node containing the description of the view hierarchy.

root ViewGroup: Optional view to be the parent of the generated hierarchy. This value may be null.

Returns
View The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.

inflate

Added in API level 1
public View inflate (XmlPullParser parser, 
                ViewGroup root, 
                boolean attachToRoot)

Inflate a new view hierarchy from the specified XML node. Throws InflateException if there is an error.

Important   For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.

Parameters
parser XmlPullParser: XML dom node containing the description of the view hierarchy.

root ViewGroup: Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.) This value may be null.

attachToRoot boolean: Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

Returns
View The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

inflate

Added in API level 1
public View inflate (int resource, 
                ViewGroup root, 
                boolean attachToRoot)

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters
resource int: ID for an XML layout resource to load (e.g., R.layout.main_page)

root ViewGroup: Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.) This value may be null.

attachToRoot boolean: Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

Returns
View The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

onCreateView

Added in API level 29
public View onCreateView (Context viewContext, 
                View parent, 
                String name, 
                AttributeSet attrs)

Version of onCreateView(android.view.View, java.lang.String, android.util.AttributeSet) that also takes the inflation context. The default implementation simply calls onCreateView(android.view.View, java.lang.String, android.util.AttributeSet).

Parameters
viewContext Context: The Context to be used as a constructor parameter for the View This value cannot be null.

parent View: The future parent of the returned view. Note that this may be null.

name String: The fully qualified class name of the View to be create. This value cannot be null.

attrs AttributeSet: An AttributeSet of attributes to apply to the View. This value may be null.

Returns
View View The View created. This value may be null.

Throws
ClassNotFoundException

setFactory

Added in API level 1
public void setFactory (LayoutInflater.Factory factory)

Attach a custom Factory interface for creating views while using this LayoutInflater. This must not be null, and can only be set once; after setting, you can not change the factory. This is called on each element name as the xml is parsed. If the factory returns a View, that is added to the hierarchy. If it returns null, the next factory default onCreateView(Context, View, String, AttributeSet) method is called.

If you have an existing LayoutInflater and want to add your own factory to it, use cloneInContext(Context) to clone the existing instance and then you can use this function (once) on the returned new instance. This will merge your own factory with whatever factory the original instance is using.

Parameters
factory LayoutInflater.Factory

setFactory2

Added in API level 11
public void setFactory2 (LayoutInflater.Factory2 factory)

Like setFactory(Factory), but allows you to set a Factory2 interface.

Parameters
factory LayoutInflater.Factory2

setFilter

Added in API level 1
public void setFilter (LayoutInflater.Filter filter)

Sets the Filter to by this LayoutInflater. If a view is attempted to be inflated which is not allowed by the Filter, the inflate(int, android.view.ViewGroup) call will throw an InflateException. This filter will replace any previous filter set on this LayoutInflater.

Parameters
filter LayoutInflater.Filter: The Filter which restricts the set of Views that are allowed to be inflated. This filter will replace any previous filter set on this LayoutInflater.

Protected methods

onCreateView

Added in API level 11
protected View onCreateView (View parent, 
                String name, 
                AttributeSet attrs)

Version of onCreateView(java.lang.String, android.util.AttributeSet) that also takes the future parent of the view being constructed. The default implementation simply calls onCreateView(java.lang.String, android.util.AttributeSet).

Parameters
parent View: The future parent of the returned view. Note that this may be null.

name String: The fully qualified class name of the View to be create.

attrs AttributeSet: An AttributeSet of attributes to apply to the View.

Returns
View View The View created.

Throws
ClassNotFoundException

onCreateView

Added in API level 1
protected View onCreateView (String name, 
                AttributeSet attrs)

This routine is responsible for creating the correct subclass of View given the xml element name. Override it to handle custom view objects. If you override this in your subclass be sure to call through to super.onCreateView(name) for names you do not recognize.

Parameters
name String: The fully qualified class name of the View to be create.

attrs AttributeSet: An AttributeSet of attributes to apply to the View.

Returns
View View The View created.

Throws
ClassNotFoundException