Android - ViewSwitcher is not switching views
-
04-10-2019 - |
Question
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()
?
Solution
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
.
OTHER TIPS
In ViewSwitcher
to navigate between views you can use the methods showPrevious()
and showNext()
Or use also can go to specific view by setDisplayedChild(int index)
method where index can be either 0
or 1