Android - ViewSwitcher is not switching views

Bostone picture Bostone · Sep 21, 2010 · Viewed 9.6k times · Source

It should be easy, right? So - here's how I have ViewSwitcher defined in XML (Id's and layouts omitted for brevity)

<ViewSwitcher android:layout_height="wrap_content" android:layout_width="fill_parent" >       
    <!-- First view, comes up OK --> 
    <LinearLayout android:id="@+id/header1">
        <!-- some more controls: Progress bar, test view and the button -->
    </LinearLayout>
    <!-- second view. changing to the actual (not include) layout has no effect -->
    <include android:id="@+id/header2" />
</ViewSwitcher>

Then somewhere in my Java code I have this code

ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);
// more code that basically executes background search
// when call comes back - switch
switcher.bringToFront(); // does nothing
// switcher.bringChildToFront(findViewById(R.id.header2)); // no effect ether

It's just not switching. I develop for API v. 3 (1.5+) and to my surprise there are very few references to ViewSwitcher. I'm I missing something obvious here?

P.S. I just found out by brute force that this works:

switcher.setDisplayedChild(1);

Still - why no luck with bringToFront()?

Answer

Konstantin Burov picture Konstantin Burov · Sep 21, 2010

bringToFront() actually has nothing to do with ViewSwitcher, the method is inherited from View class and stands for z-order manipulation of current view: https://developer.android.com/reference/android/view/View.html#bringToFront()

You have to use showNext() and showPrevious() methods inherited from ViewAnimator.