6

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

JiTHiN
  • 6,548
  • 5
  • 43
  • 69
user3505180
  • 79
  • 1
  • 4
  • 2
    I think here a good answer for you http://stackoverflow.com/questions/17808177/difference-between-setcontentview-and-inflater – Liem Vo Apr 07 '14 at 03:19

4 Answers4

18

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.

Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
3

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

Manish Mulimani
  • 17,535
  • 2
  • 41
  • 60
1

setContentView () is generally used to load an activity. inflate only Layout formed a view class object and, if necessary, then setContentView (view). General activity via setContentView () interface displayed, but how to control layout is set to operate in non-activity, which need LayoutInflater dynamic loading. For example when you use adapter class for spinner probably you have to use LayoutInflater.

Zusee Weekin
  • 1,348
  • 2
  • 18
  • 41
1

setContentView() : this method set your background layout.

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

inflater: it is need for your layou design, keep value for layoutInflat

Custom Toast: it is how your string output.custom toast means your wish for design output.stylist output show.

sorry bro i also did not know alertbox.Thank you vary much

user3467178
  • 73
  • 3
  • 10