Search code examples
androidviewflipper

Android ViewFlipper more control


I have 1 viewflipper and it contains 3 layouts . Layout 1 has 2 button one to shownext and the other to go to another activity. the 2nd layout has 3 buttons. what I want is to know how to control the 2nd layout and make on click listenter on each button so I can make one for show next and the other to go to another activity.

this is the xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ViewFlipper
        android:id="@+id/viewFlipper1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <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="44dp"
                android:src="@drawable/ballchr3" />


            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/imageView1"
                android:layout_toLeftOf="@+id/imageView3"
                android:src="@drawable/ballchr4" />



        </RelativeLayout>


 <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginTop="73dp"
                android:layout_toRightOf="@+id/imageView3"
                android:src="@drawable/ballchr3" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/imageView1"
                android:layout_toLeftOf="@+id/imageView3"
                android:src="@drawable/ballchr4" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="128dp"
                android:src="@drawable/ballchr5" />

        </RelativeLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignRight="@+id/imageView2"
            android:layout_marginTop="76dp"
            android:src="@drawable/ballchr3" />

    </RelativeLayout>        



    </ViewFlipper>

</RelativeLayout>

Activity Code

public class MainActivity extends Activity implements OnClickListener {

    ViewFlipper viewflipper;
    ImageView btn, fail;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewflipper = (ViewFlipper)findViewById(R.id.viewFlipper1);
        btn = (ImageView)findViewById(R.id.imageView1);
        fail = (ImageView)findViewById(R.id.imageView2);

        btn.setOnClickListener(this);
        fail.setOnClickListener(this);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        switch(arg0.getId()){
        case R.id.imageView1:
            viewflipper.showNext();
            break;

        case R.id.imageView2:
            Intent i = new Intent(MainActivity.this, Fail.class);
            startActivity(i);
            break;
        }
    }

Solution

  • First of all, do not repeat the id's in the same xml file, i can see that u used imageView1, 2 and 3 multiple times. Do not do that.

    To answer your question, Declare more imageViews in your activity and assign them to your respective buttons as you have already done above and set their onClickListeners,implement them. since its in the same xml, when the view exits its references will also exist.