androidandroid-viewflipper

android view flipper, make toast for every images


how can i make toast for every images that will be view in a view flipper? i mean i want every time a clicked the next button , a toast will appear and tell its the 1st pic. , 2nd pic, 3rd pic, and so on.. here's mah code: main.java

            public class main extends Activity implements  View.OnClickListener
            {
               ViewFlipper viewFlipper;
                Button next;
                Button previous;
                @Override
                public void onCreate(Bundle savedInstanceState)
                {

                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.main);
                 viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
                next = (Button) findViewById(R.id.next);
                previous = (Button) findViewById(R.id.previous);
                next.setOnClickListener(this);
                previous.setOnClickListener(this);



                }

                @Override
                public void onClick(View v) {

                    if (v == next)
                    viewFlipper.showNext();

                    else if (v == previous) {
                    viewFlipper.showPrevious();
                    }
                }


            }

main.xml

                            <?xml version="1.0" encoding="utf-8"?>
                        <RelativeLayout
                            xmlns:android="http://schemas.android.com/apk/res/android"
                            xmlns:tools="http://schemas.android.com/tools"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            tools:context="z.a.q.main"
                        android:layout_centerInParent="true"
                        android:background="@android:color/black" >
                        <ViewFlipper
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:id="@+id/viewFlipper"
                            android:layout_centerInParent="true">
                        <ImageView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:scaleType="fitXY"
                            android:id="@+id/imageView"
                            android:src="@drawable/share"/>
                        <ImageView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:scaleType="fitXY"
                            android:id="@+id/imageView2"
                            android:src="@drawable/rating"/>
                        <ImageView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:scaleType="fitXY"
                            android:id="@+id/imageView3"
                            android:src="@drawable/rate"/>


                        </ViewFlipper>
                        <Button
                            android:id="@+id/next"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Next"
                            android:layout_alignParentBottom="true"
                            android:layout_alignParentRight="true"
                            android:layout_alignParentEnd="true"/>
                        <Button
                            android:id="@+id/previous"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Prev"
                            android:layout_alignParentLeft="true"
                            android:layout_alignParentStart="true"
                            android:layout_alignParentBottom="true"
                            android:layout_alignTop="@+id/next"/>
                        </RelativeLayout>

pls. help..


Solution

    1. use the view ID to identify it.

    2. use toast and in the text use

    viewFlipper.indexOfChild(viewFlipper.getCurrentView())

    it will return the INDEX of the CURRENT displayed view.

    @Override
                    public void onClick(View v) {
                    int viewId = v.getId();
    
                        if (viewId == R.id.next){
                        viewFlipper.showNext();
                   
                      Toast.makeText(getApplicationContext(),
                    "IMAGE - "+viewFlipper.indexOfChild(viewFlipper.getCurrentView())
                    ,Toast.LENGTH_LONG).show();
                        }
    
                        else if (v == R.id.previous) {
                        viewFlipper.showPrevious();
                    
                      Toast.makeText(getApplicationContext(),
                    "IMAGE - "+viewFlipper.indexOfChild(viewFlipper.getCurrentView())
                    ,Toast.LENGTH_LONG).show();
                        }
                    }