15

Is there a way to call setContentView(id) multiple times with different id during one Activity to render different views or do I absolutely have to start a new Activity?

Christian
  • 25,249
  • 40
  • 134
  • 225

3 Answers3

10

Per Austyn's comment, I did manage to locate some guidance on how to use ViewFlipper to accomplish this in another post (see the checkmarked top answer here.)

If you don't want to use ViewFlipper, I found a nice example of how to switch between layouts in the same view here:

XML:

<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView 
        android:src="@drawable/icon"
        android:scaleType="fitCenter"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"/>
    <TextView
        android:text="Learn-Android.com"
        android:textSize="24sp"
        android:textColor="#000000"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:gravity="center"/>
</FrameLayout>

Code:

private void SwitchLayout2() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);

// Enable Layout 2 and Disable Layout 1
Layout1 .setVisibility(View.GONE);
Layout2.setVisibility(View.VISIBLE);
}

private void SwitchLayout1() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);

// Enable Layout 1 & Disable Layout2
Layout1.setVisibility(View.VISIBLE);
Layout2.setVisibility(View.GONE);
}
Community
  • 1
  • 1
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
  • 4
    You should really be using a `ViewFlipper` to do this and not just changing the visibility on each `View` – Austyn Mahoney Feb 08 '11 at 21:48
  • 1
    @AustynMahoney, it'd be great to know the reason why it's best practice to use a ViewFlipper instead of glenviewjeff's described method. – user438293456 Dec 12 '11 at 06:21
  • Why would you want to reinvent the wheel when Android already provides a very simple way to accomplish this exact thing? What happens when you want to expand this to 3 layouts, or maybe 18? If you used `ViewFlipper` it's dead simple, if you didn't well good luck maintaining that code base. – Austyn Mahoney Feb 07 '12 at 23:15
  • 1
    Given link of example is in underconstruction. – Pratik Butani Jan 23 '14 at 04:51
  • Fixed with archive.org @PratikButani – Jeff Axelrod Jan 23 '14 at 19:53
  • 1
    This will cause unnecessarily memory load. Every layout will be created and kept in the memory, when you make them visible/invisible they are just shown on hidden. So say, if you have 5 layouts, you are unnecessarily loading a lot of UI component, which is why this is a bad design. – Codevalley Jun 17 '16 at 13:53
3

No, you can't call it multiple times easily. You either need to entirely remove all views and then inflate the new layout, or use a ViewFlipper (or FrameLayout) to switch between different views.

On a side note, this question has been asked before, although I couldn't immediately find it.

EboMike
  • 76,846
  • 14
  • 164
  • 167
0

You can try this from your activity:

getWindow().addContentView(View, ViewGroup.LayoutParams);

The two content view's will be one on top of the other. But, there's no straight forward way to remove a certain view added this way.

Also note that calling setContentView after the last call, will remove all previous added content views.

Vlad
  • 735
  • 2
  • 10
  • 19