views:

57

answers:

3

Is it possible to load my main.xml layout file in onCreate() and then later in the app, switch to a new XML layout? I've tried placing setContentView() later on in the app but that seems to not actually set the new View because my references to objects in the newly set xml layout file are returning as null. I've tried doing some layout inflating using the LayoutInflater but I'm having no luck (I'm probably doing something wrong)...

So is there any other way to swap layout xml layouts at runtime? Thanks!

+1  A: 

Can't you just create a new activity? Otherwise, I think you might want to look at removing views from the main container and rebuilding your UI trough code. setContentView can only be called once per Activity if I remember well...

Sephy
Hi! Unfortunately I did try making a new activity, but I need the transition to be seamless. It looks like when loading the new activity, there is some load time involved, which is something I can't have.
justinl
ps - I do believe you are right about the setContentView() only being allowed to call once. It doesn't throw an exception but when I try to access layouts using findViewById() it gives me a null pointer when trying to reference those variables.
justinl
Indeed,if you want to access views from outside setContentView, you should follow EboMike's advice. This way, you could call findViewById on the view returned by the inflater.
Sephy
+1  A: 

I also think that it really sounds like you want a new activity, but I believe you can re-inflate something if you manually delete the existing views and then call getLayoutInflater().inflate(resourceId, rootViewGroup).

EboMike
Thanks for your answer. I'll give this a try next time I get stuck!
justinl
+1  A: 

You could have your layouts in a single xml file. The ones you don't want to see immediately, set android:visibility="gone"in the XML, and at runtime you would do a call to hiddenView.setVisibility(View.VISIBLE);.

Your XML would look something like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/layout1"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
    </LinearLayout>
    <LinearLayout android:id="@+id/layout2"
        android:visibility="gone"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
    </LinearLayout>
</RelativeLayout>

at runtime you could do:

layout1.setVisibility(View.GONE);
layout2.setVisiblity(View.VISIBLE);

This would essentially create the appearance of the layouts swapping.

I believe this would also work if you have shared objects between the two layouts. Just make sure you set the id first using android:id="@+id/view1" and every subsequent setting of this id you would do android:id="@id/view1" (without the +).

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/layout1"
        android:layout_width="fill_parent">
        <View android:id="@+id/view1"/>
    </LinearLayout>
    <LinearLayout android:id="@+id/layout2"
        android:visibility="gone"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
         <View android:id="@id/view1"/>
    </LinearLayout>
</RelativeLayout>
SSZero
I like this answer a lot. I personally use a ViewFlipper to manage multiple layouts so that I don't have to manage [Resource] variables coming into scope and out as their layout is set to the content view. Both approaches are viable.
Brad Hein
Ah, I have no experience with the ViewFlipper class. I should check it out next time I need to do something like this.
SSZero
Oh wow that worked perfectly :D Thank you!
justinl