0

I'm using a screen that I want to switch between layouts on the click of a button. I want both layouts to occupy the full height of the screen, when the button on the first layout is pressed, i want this layout to disappear and the other layout to take it's place, and then same on the next layout.

<LinearLayout 
    android:id="@+id/layoutFirst"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    ..... views

</LinearLayout>

<LinearLayout 
    android:id="@+id/layoutSecond"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    ..... views

</LinearLayout>

On each layout I have a button. I want this button to switch so the other layout occupies the full screen, but nothing happens. The code in the on-click event for the first screen is the following

LinearLayout layoutFirst = (LinearLayout) this.findViewById(R.id.layoutFirst);
layoutFirst.setVisibility(View.GONE);
LinearLayout layoutSecond = (LinearLayout) this.findViewById(R.id.layoutSecond);
layoutSecond.setVisibility(View.VISIBLE);

Just in case the problem was to do with the fill_parent, I also tried this with the height being set to wrap_content. Initially I can see both layouts on the screen, when I press the button, the layout still does nothing.

Can anyone tell me if I am doing something wrong or if there's a way to solve this issue. Any help would be greatly appreciated it

Graham Baitson
  • 580
  • 1
  • 11
  • 29
  • 1
    Did you add those two LinearLayouts into a FrameLayout? – thaussma Jan 07 '13 at 10:49
  • Keep the id's of both layout same..and initially set visibility of one of them gone.Now in code,take a static variable and change its value upon click ,and hence show the other layout and hide first one. – Payal Jan 07 '13 at 10:54
  • No, my root layout is a linear layout which takes up the whole parent, then I have these two linear layouts direction inside the main layout – Graham Baitson Jan 07 '13 at 10:54
  • Should both the linear layouts be inside a frame layout @Herrmann? @Payal if I keep the ids of both layouts the same, how do I distinguish which one to make visible upon the button click and which once to make gone? – Graham Baitson Jan 07 '13 at 10:59

2 Answers2

1

Put the two LinearLayouts inside a FrameLayout:

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

    <LinearLayout
        android:id="@+id/layoutFirst"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:visibility="gone">
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layoutSecond"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    </LinearLayout>
</FrameLayout>

Give the LinearLayouts different ids!

thaussma
  • 9,756
  • 5
  • 44
  • 46
  • Hi @Herrmann, thanks very much for the response. I put these LinearLayouts into the FrameLayout parent, and it's still the same. On the click event, I find each layout, and set the current layout visibility to gone and the second layout visibility to visible. The current layout just stays there without anything happening to it. I honestly don't know why it's not working. I've also come across a post that uses ViewFlipper, and that's the same aswell, nothing changes – Graham Baitson Jan 07 '13 at 11:58
  • Yes a ViewFlipper is the better alternative. It supports animations too. Does invalidating the FrameLayout help? (Call framelayout.invalidate()) – thaussma Jan 07 '13 at 12:02
  • I've just tried the invalidate() with and without the ViewFlipper butt still the same. Nothing at all is happening. Is there anything I can try to print out or anything that may help you solve the problem – Graham Baitson Jan 07 '13 at 12:09
  • Hi @Herrmann, thanks so much for your help. I've stripped down my code to the very basics and put it in a new activity to test and it worked fine. I've used the ViewFlipper and it works great. I then copied across my exact code into the new activity and it worked fine again. I've just realised that I'm calling all this code from within a dialog. I'm trying to switch view within a dialog window. I didn't think this would matter but obviously it does, as the same code works within an activity. Does this mean that switching views within a dialog is not supported – Graham Baitson Jan 07 '13 at 12:31
  • Hi @Herrmann, thanks very much for your help, I've realised what I was doing. I've had a copy of my dialog and was using that variable which meant it wasn't the live dialog that I was changing. Thanks for taking the time to respond – Graham Baitson Jan 07 '13 at 12:39
1

Is your parent layout a "RelativeLayout" ? => not necessary here.

Try also the solution proposed in this quite similar thread how to hide linearlayout from java code?

Update:

/*global or add final keyword if not global in order to use inside listener */
LinearLayout layoutFirst;
LinearLayout layoutSecond;
/*end global*/

/*in onCreate(Bundle) method*/
layoutFirst = (LinearLayout) this.findViewById(R.id.layoutFirst);
layoutSecond = (LinearLayout) this.findViewById(R.id.layoutSecond);

/*and then in your listener, alternatively*/
layoutFirst.setVisibility(View.GONE);
layoutSecond.setVisibility(View.VISIBLE);
Community
  • 1
  • 1
cdesir
  • 139
  • 5
  • 1
    Hi @Eudhan, my parent layout is a LinearLayout which is set to match_parent. Under this there's the two layouts which I've described above. I've tried the solution proposed in the thread, but still no avail. It just doesn't seem to be reacting the way I think it should be? – Graham Baitson Jan 07 '13 at 11:24
  • I have just tested a little implementation and the error seems to be the local declaration of your layouts. Just put their declaration in the global scope and it should work. /*global*/ LinearLayout layoutFirst; LinearLayout layoutSecon; /*end global*/ /*in onCreate(Bundle) method*/ layoutFirst = (LinearLayout) this.findViewById(R.id.layoutFirst); layoutSecond = (LinearLayout) this.findViewById(R.id.layoutSecond); /*and then in your listener, alternatively*/ layoutFirst.setVisibility(View.GONE); layoutSecond.setVisibility(View.VISIBLE); – cdesir Jan 07 '13 at 12:21
  • Hi @Eudhan, thanks so much for your help. I've stripped down my code to the very basics and put it in a new activity to test and it worked fine. I then copied across my exact code into the new activity and it worked fine again. I've just realised that I'm called all this code from within a dialog. I'm trying to switch view within a dialog window. I didn't think this would matter but obviously it does, as the same code works within an activity. Does this mean that switching views within a dialog is not supported – Graham Baitson Jan 07 '13 at 12:30
  • Hi @Eudhan, thanks very much for your help, I've realised what I was doing. I've had a copy of my dialog and was using that variable which meant it wasn't the live dialog that I was changing. I done what you suggested with your global variables and that lead me to the solution. Thanks for taking the time to respond – Graham Baitson Jan 07 '13 at 12:38