-1

My MainActivity extends BaseActivity. Inside my BaseActivity, I override setContentView(int layoutResId) (refer below) so that when my MainActivity calls setContentView(R.layout.activity_main), the view component inside BaseActivity -- the activityContainer will contain R.layout.activity_main as its child after the method getLayoutInflater().inflate(layoutResID, activityContainer, true); is called. How can I achieve the same thing if I want to override setContentView(View view) instead of setContentView(int layoutResId) in BaseActivity?

Edit: What are the differences between these two methods?

Inside BaseActivity:

@Override
public void setContentView(int layoutResId) {
        RelativeLayout baseActivity = (RelativeLayout) getLayoutInflater()
            .inflate(R.layout.activity_base, null);

        FrameLayout activityContainer = (FrameLayout) baseActivity
            .findViewById(R.id.activity_container);

        getLayoutInflater().inflate(layoutResID, activityContainer, true);
}

activity_base.xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <FrameLayout
        android:id="@+id/activity_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    .... other components
</RelativeLayout>
karansky
  • 538
  • 1
  • 4
  • 24

3 Answers3

3

The descriptions of the two methods:

setContentView (int layoutResID):

Set the activity content from a layout resource.
The resource will be inflated, adding all top-level views to the activity.

setContentView (View view, ViewGroup.LayoutParams params):

Set the activity content to an explicit view.
This view is placed directly into the activity's view hierarchy.
It can itself be a complex view hierarchy.

Basically the difference is that the one which expects the layout ID inflates it first and then adds it to the activity root view.

The one with the View does not do any inflating, it just immediately adds it to the root view, using the given LayoutParams.

To sum up, your overridden method would be something like this:

@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
    RelativeLayout baseActivity = (RelativeLayout) getLayoutInflater()
        .inflate(R.layout.activity_base, null);
    FrameLayout activityContainer = (FrameLayout) baseActivity
        .findViewById(R.id.activity_container);
    activityContainer.addView(view, params);
}
Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
  • Correct me if I'm wrong. `activityContainer.addView(view, params);` is almost the same as `getLayoutInflater().inflate(layoutResID, activityContainer, true);`? The only difference is I need to inflate a layout to get a view first before I can use the `addView` method? – karansky Feb 01 '16 at 09:55
1

There are two abstract methods in Activity class which you can implement in you BaseActivity.

public abstract void setContentView(View view);

and

public abstract void setContentView(View view, ViewGroup.LayoutParams params);

Once implemented you can use these methods in MainActivity

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

I think the different is (View layoutResID) and (View view)

With method setContentView(int layoutResID): You set the activity content from a layout resource.

With method setContentView(View view): You set the activity content from a view (this view you can get from layout resource or you can create programmatically)

Linh
  • 57,942
  • 23
  • 262
  • 279