Android Viewflipper example

In this tutorial, we are going to learn how to use viewflipper in android. ViewFlipper is a simple Viewanimator that will animate between two or more views that have been added to it. Only one child is shown at a time. If requested, can automatically flip between each child at a regular interval.

The App looks Like

Github: https://github.com/rincemathew/ViewFlipper

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ViewFlipper
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </ViewFlipper>

    <RelativeLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">

        <ViewFlipper
            android:id="@+id/viewflipper"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:text="Button1"
                android:textSize="40dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <TextView
                android:text="Button2"
                android:textSize="40dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <TextView
                android:text="Button3"
                android:textSize="40dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </ViewFlipper>



    </RelativeLayout>

    <LinearLayout
        android:layout_weight=".1"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp">
        <Button
            android:id="@+id/button1"
            android:text="button1"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/button2"
            android:text="button2"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/button3"
            android:text="button3"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />



    </LinearLayout>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {


    Button button1,button2,button3;
    ViewFlipper flipper;
    TextView text1,text2,text3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1=findViewById(R.id.button1);
        button2=findViewById(R.id.button2);
        button3=findViewById(R.id.button3);
        flipper=findViewById(R.id.viewflipper);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flipper.setDisplayedChild(0);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flipper.setDisplayedChild(1);
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flipper.setDisplayedChild(2);
            }
        });
    }
}

Keep exploring more tutorial from this site. In case of any queries make a comment. Happy coding!!!!

Add a Comment

Your email address will not be published. Required fields are marked *