LayoutInflator

1 minute read

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(LayoutInflater.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.)

Google Developers Docs


LayoutInflater can be used to make AlertDialog in android. We can dynamically add and remove views using Java code. This class is used to instantiate layout XML file into its corresponding View objects.

  • It is never to be used directly – use getLayoutInflater() or getSystemService(String)
  • getSystemService(String) is used 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.
  • example LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); LayoutInflater inflater = getLayoutInflater();
//------------------------------------
// Inflater
//------------------------------------
LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mView = inflater.inflate(R.layout.alert_record, null, false);

Tags:

Categories:

Updated:

Leave a comment