Search code examples
androidhorizontalscrollview

How to move images one by one using horizontalscrollview in android


I'm new to android development world. I want to display images one by one in imageView using HorizontalScrollView. I have tried HorizontalScrollView LinearLayout ImageView, but i displaying images continuously.


Solution

  • Just add ViewFlipper in you xml first :-

      <ViewFlipper
                    android:id="@+id/view_flipper"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_margin="6dip" >
    
                <!--  The child Views/Layout to flip -->
    
                <!--  Layout 1 for 1st Screen -->
                    <LinearLayout
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:gravity="center"
                            android:orientation="vertical" >
    
    
                            <ImageView
                                android:layout_marginTop="15dp"
                                android:id="@+id/imageView1"
                                android:layout_width="450dp"
                                android:layout_height="450dp"
                                android:src="@drawable/image1" />
    
                    </LinearLayout>
    
                     <!--  Layout 2 for 2nd Screen -->
    
                    <LinearLayout
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:gravity="center"
                            android:orientation="vertical" >
    
                                 <ImageView
                            android:layout_marginTop="15dp"
                            android:id="@+id/imageView1"
                            android:layout_width="450dp"
                            android:layout_height="450dp"
                            android:src="@drawable/image3" />
    
    
                    </LinearLayout>
    
    
                </ViewFlipper>
    

    And then just inflate the ViewFlipper inside your activity :-

    @Override
                protected void onCreate(Bundle savedInstanceState) 
                {
                             super.onCreate(savedInstanceState);
                             setContentView(R.layout.view_flipper_main);
                             viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
                }
    

    And if you want to show the next image you can simply use :-

     // Show the next Screen
                                         viewFlipper.showNext();
    

    And if you want to show the previous image you can simply use :-

    // Show The Previous Screen
                                     viewFlipper.showPrevious();
    

    The above code will show one by one image and later you can animate it accordingly.