Search code examples
androidvisibilityviewflipper

setVisibility not working within ViewFlipper


I'm trying to disable a TextView within a ViewFlipper via setVisibility to GONE and cannot get it to act like I'm wanting. My code:

switch(index) {        
    case 0:
        //Do Stuff
     findViewById(R.id.o2).setVisibility(8);
     findViewById(R.id.o3).setVisibility(8);
     break;
    case 1:
        //Do Stuff
     findViewById(R.id.o3).setVisibility(8);         
     break;
    case 2:
        //Do Stuff
     break;
    }

my XML

<ViewFlipper android:id="@+id/oFlipper"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:flipInterval="1000"
     android:inAnimation="@anim/push_up_in"
     android:outAnimation="@anim/push_up_out">
        <TextView android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:gravity="center_horizontal"
         android:textSize="26sp"
         android:text="Opponents:"/>
        <TextView android:id="@+id/o1"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:gravity="center_horizontal"
         android:textSize="26sp"
         android:visibility="gone"/>
        <TextView android:id="@+id/o2"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:gravity="center_horizontal"
         android:textSize="26sp"
         android:text="2"/>
        <TextView android:id="@+id/o3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textSize="26sp"
            android:text="3"/>
 </ViewFlipper>

I've tried the code before and after .startFlipping() to no avail. It appears that the TextView is gone for one view flip and then reappears. But even when hardcoded to GONE in the XML file the view is simply blank rather than shifting the other views up in its place. I basically just want the TextView to go away completely. Is there any way to accomplish this?


Solution

  • I have had the same issue. Using INVISIBLE is better than GONEthe view is not displayed but it still takes up a timeslot e.g. you get the previous view for twice as long as you should.

    I have got things working the way I want by adding and removing the child views from the ViewFlipper. In the code where I was setting each sub view to GONEor VISIBLE(in onResume) I now remove them all from the flipper with .removeAllViews() then if I was setting them to visable I add them back in with .addView(mView); It even looks to do the right thing when the ViewFlipper is empty.