Search code examples
javaandroidimageviewviewflipper

View Flipper that uses Only One ImageView


I'm new to Android, and am currently using a ViewFlipper. I'd like to know if it's possible to use only one ImageView? That way I can just create an Arraylist of my images. I think using multiple ImageViews is not a good practice since I have 50+ images.

public void initContent() {

    imageArrayList = new ArrayList<Integer>();
    imageArrayList.add(R.drawable.pig2);
    imageArrayList.add(R.drawable.pig3);
    imageArrayList.add(R.drawable.pig4);
    imageArrayList.add(R.drawable.pig5);
    imageArrayList.add(R.drawable.pig6);
    imageArrayList.add(R.drawable.pig7);
    imageArrayList.add(R.drawable.pig8);
    imageArrayList.add(R.drawable.pig9);
    imageArrayList.add(R.drawable.pig10);
    imageArrayList.add(R.drawable.pig11);
    imageArrayList.add(R.drawable.pig12);
    imageArrayList.add(R.drawable.pig13);
    imageArrayList.add(R.drawable.pig14);
    imageArrayList.add(R.drawable.pig24);
    imageArrayList.add(R.drawable.pig25);
    imageArrayList.add(R.drawable.theend);

Solution

  • MainActivity.java

    public class MainActivity extends Activity {
        ViewFlipper viewFlipper;
        Button Next;
        private Integer images[] = {R.drawable.ic_launcher,
                R.drawable.ic_no_image, R.drawable.calendar52};
        ImageView imageView1;
        private int currImage = 0;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            viewFlipper = (ViewFlipper) findViewById(R.id.ViewFlipper01);
    
            Next = (Button) findViewById(R.id.Next);
            imageView1 = (ImageView) findViewById(R.id.imageView1);
            Next.setOnClickListener(new View.OnClickListener() {
    
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    currImage++;
                    if (currImage == 3) {
                        currImage = 0;
                    }
                    final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
                    imageView.setImageResource(images[currImage]);
                    viewFlipper.showNext();
                }
            });
        }
    }
    

    activity_main.xml

     <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/LinearLayout01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
    
            <RelativeLayout
                android:id="@+id/RelativeLayout02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >
    
                <ViewFlipper
                    android:id="@+id/ViewFlipper01"
                    android:layout_width="fill_parent"
                    android:layout_height="400dp" >
    
                    <RelativeLayout
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:background="#4B0082" >
    
                        <ImageView
                            android:id="@+id/imageView1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="117dp"
                            android:src="@drawable/ic_launcher" />
    
                    </RelativeLayout>
    
          </ViewFlipper>
            </RelativeLayout>
    
            <RelativeLayout
                android:id="@+id/RelativeLayout03"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:background="#000000"
                android:gravity="center" >
    
                <Button
                    android:id="@+id/Next"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:layout_marginBottom="5dp"
                    android:layout_marginRight="20dp"
                    android:text="Next" />
            </RelativeLayout>
    
        </LinearLayout>