Second view of ViewSwitcher doesn't show up

649 Views Asked by At

I'm trying to add a simple ViewSwitcher with two ImageViews as children. But no matter what I try, the second child remains invisible. In the XML layout preview in Android Studio it looks like the second child has a size 0x0 pixels. The first child shows up just fine. What's the problem here?

<ViewSwitcher
    android:id="@+id/viewSwitcher"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/view1"
        android:scaleType="fitXY"
        android:src="@drawable/view_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ImageView
        android:id="@+id/view2"
        android:scaleType="fitXY"
        android:src="@drawable/view_2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</ViewSwitcher>

I'm switching beween the two views simply using gradientViewSwitcher.showNext().

1

There are 1 best solutions below

2
On

Try use a LinearLayout and wrap_conent like this:

 <!-- ViewSwitcher with two ImageView-->
 <ViewSwitcher
 android:id="@+id/viewSwitcher"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

       <ImageView
       android:id="@+id/view1"
       android:scaleType="fitXY"
       android:src="@drawable/view_1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="center"/>

       <ImageView
       android:id="@+id/view2"
       android:scaleType="fitXY"
       android:src="@drawable/view_2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="center"/>

    </LinearLayout>   
     </ViewSwitcher>