Android viewFlipper working source code working Oreo

Android viewFlipper working in newer Android OS version

This is the best choice for programmer to use multiple layout in single activity page. Suppose in a single activity page you want to show many layout design contain Button, ImageButton, ImageView etc then try this viewFlipper working with android latest API or OS version like Oreo Nougat Marshmallow and Lollipop. Below the complete source code of android viewFlipper working. Click here to watch viewFlipper video tutorial best for beginner.

package write your package name here;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class MainActivity extends AppCompatActivity {

    private Context context;
    private float initialX;
    private ViewFlipper viewFlipper;

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

        context = this;
        viewFlipper = (ViewFlipper) this.findViewById(R.id.view_flipper);
nextFlip = (Button) findViewById(R.id.playViewFlip);
nextFlip.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        viewFlipper.setAutoStart(false);  //set True if you want to run non stop.
        viewFlipper.setFlipInterval(1500);
        viewFlipper.startFlipping();
        Toast.makeText(getApplicationContext(),"Auto ViewFlipper Started",Toast.LENGTH_SHORT).show();
    }
});

previousFlip = (Button) findViewById(R.id.stopViewFlip);
previousFlip.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        viewFlipper.stopFlipping();
        Toast.makeText(getApplicationContext(),"Auto ViewFlipper Stopped",Toast.LENGTH_SHORT).show();
    }
});
 }

 @Override
 public boolean onTouchEvent(MotionEvent touch_event) {
 switch (touch_event.getAction()) {
 case MotionEvent.ACTION_DOWN:
 initialX = touch_event.getX();
 break;
 case MotionEvent.ACTION_UP:
 float finalInX = touch_event.getX();
 if (initialX > finalInX) {
 if (viewFlipper.getDisplayedChild() == 1)
 break;

 viewFlipper.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.in_from_left_side));
 viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.out_from_left_side));

 //Click to show next Layout
 viewFlipper.showNext();
 }
 else {
 if (viewFlipper.getDisplayedChild() == 0)
 break;

 viewFlipper.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.in_from_right_side));
 viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.out_from_right_side));

 //Click to show last Layout
 viewFlipper.showPrevious();
 }
 break;
 }
 return false;
 }
}

another example
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;

public class MainActivity extends AppCompatActivity {

    ViewFlipper viewFlipper;
    Button Next, Previous;

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

        viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);

        Next = (Button) findViewById(R.id.NextFlip);
        Previous = (Button) findViewById(R.id.PreviousFlip);

        Next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                viewFlipper.showNext();
            }
        });

        Previous.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                viewFlipper.showPrevious();
            }
        });
    }
}
<?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"
    android:background="#bebebe"
    tools:context=".MainActivity"
    >
    <ViewFlipper
        android:id="@+id/view_flipper"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true">

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

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#8b8788"
                android:scaleType="centerCrop"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true" 
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:text="First Layout"
                android:textColor="#bfff7b"
                android:textSize="20sp"
                android:textStyle="bold"
                />
        </RelativeLayout>

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

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:background="@android:color/holo_blue_bright"
                android:scaleType="centerCrop" 
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:textStyle="bold"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:text="Second Layout"
                />
        </RelativeLayout>

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

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:background="@android:color/holo_green_light"
                android:scaleType="centerCrop" 
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:textSize="20sp"
                android:textStyle="bold"
                android:gravity="center"
                android:text="Third Layout"
                />
        </RelativeLayout>
        
    </ViewFlipper>

    <RelativeLayout

        android:layout_width ="match_parent"
        android:layout_height ="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true">

        <Button
            android:id="@+id/playViewFlip"
            android:layout_width ="60dp"
            android:layout_height ="wrap_content"
            android:text="PLAY"
            android:background="#f99ca3"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_marginStart="90dp" 
            />

        <Button
            android:id="@+id/stopViewFlip"
            android:layout_width ="50dp"
            android:layout_height ="wrap_content"
            android:background ="#fd9098"
            android:text ="STOP"
            android:layout_marginEnd ="79dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true" 
            />
    </RelativeLayout>

</RelativeLayout>

Visit ATTRACTION ANDROID more latest android code less deprecation with newer version checked frequently.

Add a Comment

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