Android: setContentView and LayoutInflater

What is the difference between setContentView and LayoutInflater ? And what is the reason we use inflater in custom toast and setContentView in custom alertbox ?

+6
source share
4 answers

You need to understand a few things before

On Android each Activity has one ViewRoot and usually one Window attached to it. However, SurfaceView has its own window. So, if an Activity has a SurfaceView , it will have more than one window.

This operation is used to display a screen that occupies the entire window. Views are attached to this window. Each window has a surface, and the surface uses Canvas to draw on the surface. The window to which the view is attached belongs to the surface.

Basically, ViewRoot is responsible for collecting and sending input, and View is responsible for managing focus / key events, Canvas is only responsible for the drawing operation using onDraw() .

setContentView(View) is a method available only to Activity . Inside, it calls setContentView(View) Window . This method sets the contents of an activity to an explicit representation. This view is placed directly in the hierarchy of activities. Calling this function “blocks” various characteristics of the window that cannot be changed from this point forward. Therefore, it is called only once.

LayoutInflater used to instantiate an XML file into the corresponding View objects. Basically the goal is to create view objects at runtime depending on the requirement. The best example is AdapterViews , like ListView , Spinner , etc., where at runtime a separate view object is created corresponding to a single record, depending on the number of records.

In the case of Toast , a LayoutInflater used if the child’s representation is changed dynamically, for example. changing the image at runtime. If no changes are required in the child views, just setView(View) from the toast is enough to set the layout for the toast.

Same as Toast with AlertDialog if you are closely watching.

Hope this helps you.

+15
source

setContentView () usually used to load activity. inflate Only the layout formed the object of the view class and, if necessary, then setContentView (view). General activity through the setContentView ( ) interface, but how to control the layout is configured to work in inactive mode, which requires LayoutInflater dynamic loading. For example, if you use an adapter class for spinner, you probably need to use LayoutInflater .

+1
source

setContentView (): this method sets the background layout.

LoayoutInflater: suppose if you use listview and your list show show 10 item.then layoutIflat do work for show 10 item

blower: this is necessary for your layou project, keep value for layoutInflat

Custom toast: this is how your line of output.custom toast means your desire to output the output of output.stylist.

Sorry, I didn’t know alertbox.Thank you too much

+1
source

setContentView internally uses Inflater to achieve its functionality. This is a convenience method that is responsible for assigning the parent / root view element to inflate the layout. It also initializes the ActionBar.

Here is the Android source code: Activity.java

 public void setContentView(int layoutResID) { getWindow().setContentView(layoutResID); initActionBar(); } 

com / android / internal / politics / real / PhoneWindow.java

 @Override public void setContentView(int layoutResID) { if (mContentParent == null) { installDecor(); } else { mContentParent.removeAllViews(); } mLayoutInflater.inflate(layoutResID, mContentParent); final Callback cb = getCallback(); if (cb != null && !isDestroyed()) { cb.onContentChanged(); } } 

As for your second question, we use inflater both in a custom toast and in a special notification dialog box. For example, creating a custom toast:

 Toast toast = new Toast(getApplicationContext()); toast.setView(inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.toast_layout_root))); 

eg. Create a custom alert dialog:

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setView(inflater.inflate(R.layout.dialog_signin, null)); 

In the case of Alert Dialog, we do not provide the root for the bloated layout, as the layout is added to the FrameLayout element with the identifier 'custom', as indicated in alert_dialog.xml

+1
source

All Articles