1

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().

Incinerator
  • 2,589
  • 3
  • 23
  • 30

1 Answers1

0

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>
Roberto Pinheiro
  • 1,260
  • 13
  • 30
  • Thanks for your answer! Unfortunately, it won't be possible to switch between the two views using this solution (`gradientViewSwitcher.showNext()`) – Incinerator Dec 06 '17 at 11:57
  • That example works standalone, but when I put it into my own app it stops working. The only thing that differs is that i use ConstraintLayout on the top level, so perhaps it's an issue with that...? – Incinerator Dec 07 '17 at 12:09