Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling setContentView() multiple times

Tags:

android

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?

like image 530
Christian Avatar asked Oct 25 '10 20:10

Christian


2 Answers

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);
}
like image 177
Jeff Axelrod Avatar answered Nov 08 '22 10:11

Jeff Axelrod


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.

like image 3
EboMike Avatar answered Nov 08 '22 11:11

EboMike