English | Site Directory

Android - An Open Handset Alliance Project

android.view
public abstract class

android.view.ViewInflate

java.lang.Object
android.view.ViewInflate

This class is used to instantiate layout XML file into its corresponding View objects. It is never be used directly -- use getViewInflate() or getSystemService(String) to retrieve a standard ViewInflate instance that is already hooked up to the current context and correctly configured for the device you are running on.

To create a new ViewInflate with an additional ViewInflate.Factory for your own views, you can use cloneInContext(Context) to clone an existing ViewFactory, and then call setFactory(ViewInflate.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 ViewInflate with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R.something file.)

Nested Classes
ViewInflate.Factory  
ViewInflate.InflateException This exception is thrown by the inflator on error conditions. 

Summary

Fields

protected    final  Context  mContext   

Protected Constructors

          ViewInflate(Context context)
Create a new ViewInflate instance associated with a particular Context.
          ViewInflate(ViewInflate original, Context newContext)
Create a new ViewInflate instance that is a copy of an existing ViewInflate, optionally with its Context changed.

Public Methods

abstract        ViewInflate  cloneInContext(Context newContext)
Create a copy of the existing ViewInflate object, with the copy pointing to a different Context than the original.
    final    View  createView(String name, String prefix, AttributeSet attrs, Map inflateParams)
Low-level function for instantiating a view by name.
      static  ViewInflate  from(Context context)
Obtains the ViewInflate from the given context.
        Context  getContext()
Return the context we are running in, for access to resources, class loader, etc.
    final    Factory  getFactory()
Return the current factory (or null).
        View  inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot, Map inflateParams)
Inflate a new view hierarchy from the specified XML node.
        View  inflate(int resource, ViewGroup root, boolean attachToRoot, Map inflateParams)
Inflate a new view hierarchy from the specified xml resource.
        View  inflate(XmlPullParser parser, ViewGroup root, Map inflateParams)
Inflate a new view hierarchy from the specified xml node.
        View  inflate(int resource, ViewGroup root, Map inflateParams)
Inflate a new view hierarchy from the specified xml resource.
        void  setFactory(Factory factory)
Attach a custom Factory interface for creating views while using this ViewInflate.

Protected Methods

        View  onCreateView(String name, AttributeSet attrs, Map inflateParams)
This routine is responsible for creating the correct subclass of View given the xml element name.
Methods inherited from class java.lang.Object

Details

Fields

protected final Context mContext

Protected Constructors

protected ViewInflate(Context context)

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

Parameters

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

protected ViewInflate(ViewInflate original, Context newContext)

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

Parameters

original The original ViewInflate to copy.
newContext The new Context to use.

Public Methods

public abstract ViewInflate cloneInContext(Context newContext)

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

Parameters

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

Returns

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

public final View createView(String name, String prefix, AttributeSet attrs, Map inflateParams)

Low-level function for instantiating a view by name. This attempts to instantiate a view class of the given name found in this ViewInflate'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

name The full name of the class to be instantiated.
attrs The XML attributes supplied for this instance.
inflateParams Optional map of additional attribute name/value pairs to apply to the root element. Can be null. Currently, this parameter is only intended to be used internally by Android.

Returns

  • View The newly instantied view, or null.

public static ViewInflate from(Context context)

Obtains the ViewInflate from the given context.

public Context getContext()

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

public final 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).

public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot, Map inflateParams)

Inflate a new view hierarchy from the specified XML node. Throws ViewInflate.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 ViewInflate with an XmlPullParser over a plain XML file at runtime.

Parameters

parser XML dom node containing the description of the view hierarchy.
root 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.)
attachToRoot 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.
inflateParams Optional map of additional attribute name/value pairs to apply to the root element. Can be null. Currently, this parameter is only intended to be used internally by Android.

Returns

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

public View inflate(int resource, ViewGroup root, boolean attachToRoot, Map inflateParams)

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

Parameters

resource ID for an XML layout resource to load (e.g., R.layout.main_page)
root 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.)
attachToRoot 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.
inflateParams Optional map of additional attribute name/value pairs to apply to the root element. Can be null. Currently, this parameter is only intended to be used internally by Android.

Returns

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

public View inflate(XmlPullParser parser, ViewGroup root, Map inflateParams)

Inflate a new view hierarchy from the specified xml node. Throws ViewInflate.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 ViewInflate with an XmlPullParser over a plain XML file at runtime.

Parameters

parser XML dom node containing the description of the view hierarchy.
root Optional view to be the parent of the generated hierarchy.
inflateParams Optional map of additional attribute name/value pairs to apply to the root element. Can be null. Currently, this parameter is only intended to be used internally by Android.

Returns

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

public View inflate(int resource, ViewGroup root, Map inflateParams)

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

Parameters

resource ID for an XML layout resource to load (e.g., R.layout.main_page)
root Optional view to be the parent of the generated hierarchy.
inflateParams Optional map of additional attribute name/value pairs to apply to the root element. Can be null. Currently, this parameter is only intended to be used internally by Android.

Returns

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

public void setFactory(Factory factory)

Attach a custom Factory interface for creating views while using this ViewInflate. 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(String, AttributeSet, Map) method is called.

If you have an existing ViewInflate 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.

Protected Methods

protected View onCreateView(String name, AttributeSet attrs, Map inflateParams)

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 The fully qualified class name of the View to be create.
attrs An AttributeSet of attributes to apply to the View.
inflateParams Optional map of additional attribute name/value pairs to apply to the root element. Can be null. Currently, this parameter is only intended to be used internally by Android.

Returns

  • View The View created.
Build m5-rc15g - 14 May 2008 12:50