What is the difference between setContentView
and LayoutInflater
?
And whats the reason that we use inflater
in custom toast
and setContentView
in custom alertbox
?
android.view.LayoutInflater. Instantiates a layout XML file into its corresponding View objects. It is never used directly. Instead, use Activity.
SetContentView(View)Set the activity content to an explicit view.
onCreate() method calls the setContentView() method to set the view corresponding to the activity. By default in any android application, setContentView point to activity_main. xml file, which is the layout file corresponding to MainActivity.
kotlin.Any. ↳ android.view.LayoutInflater. Instantiates a layout XML file into its corresponding android.
You need to understand few things before,
In Android
,each Activity
has one ViewRoot
and usually one Window
,attached to it. However, a SurfaceView
has its own window. So, if an Activity
has a SurfaceView
it will have more than one Window.
This activity is used for screen display occupying the entire Window. Views are attached to this Window. Every Window has a Surface and Surface uses Canvas
to draw on the surface.The window the view is attached to owns the surface.
Basically ViewRoot
is responsible for for collecting and dispatching the input and View
is responsible for managing focus/key events, Canvas
is only responsible for "drawing" operation using onDraw()
.
setContentView(View)
is a method exclusively available for Activity
. Internally it calls the setContentView(View)
of Window
. This method sets the activity content to an explicit view. This view is placed directly into the activity's view hierarchy. Calling this function "locks in" various characteristics of the window that can not, from this point forward, be changed. Hence it is called only once.
LayoutInflater
is used to instantiate layout XML file into its corresponding View objects. Basically the purpose is to create view objects at runtime depending on the requirement.
Best example is the AdapterViews
like ListView
, Spinner
etc, where a single view object corresponding to single record is created at run time depending on the number of records.
In case of Toast
, LayoutInflater
is used if the child view is going to be altered dynamically eg. changing the image at run time. If no changes to child views are to be made then simplly setView(View)
of toast is enough to set the layout view for toast.
Same as Toast
is with the AlertDialog
if you observe carefully.
Hope it helps you.
setContentView internally uses Inflater to achieve it's functionality. It is a convenience method, which will take the responsibility of assigning parent / root view element for the layout being inflated. 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/policy/impl/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();
}
}
Regarding your 2nd question, we do use inflater in both custom toast and custom alert dialog. e.g. Custom Toast creation:
Toast toast = new Toast(getApplicationContext());
toast.setView(inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.toast_layout_root)));
e.g. Custom Alert Dialog creation:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(inflater.inflate(R.layout.dialog_signin, null));
In case of Alert Dialog, we don't provide the root for the inflated layout, as the layout is added to FrameLayout element with id 'custom' as specified in the alert_dialog.xml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With