Android tutorial for beginners part 9, Viewflipper, Viewflipper Animation

  • 23 April 20
  • Posted By : Admin
  • 59 Hits
  • 10 Comment

How to create Viewflipper and animate that Animation

}

Java code

Copy and paste below code on MainActivity.java file

package com.salam.andoridviewflipper; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ViewFlipper; public class MainActivity extends AppCompatActivity { public ViewFlipper viewFlipper; public Button next,previous; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewFlipper = findViewById(R.id.viewflipper); next = findViewById(R.id.next); previous = findViewById(R.id.previous); next.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { viewFlipper.setInAnimation(MainActivity.this, android.R.anim.slide_in_left); viewFlipper.setOutAnimation(MainActivity.this, android.R.anim.slide_out_right); viewFlipper.showNext(); } }); previous.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { viewFlipper.setInAnimation(MainActivity.this,R.anim.slide_in_right); viewFlipper.setOutAnimation(MainActivity.this, R.anim.slide_out_left); viewFlipper.showPrevious(); } }); } }

Create xml file

paste below code on activity_main.xml file

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".MainActivity"> <ViewFlipper android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/viewflipper"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" android:layout_gravity="center"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/nature" android:scaleType="centerCrop" android:layout_gravity="center"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:layout_gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="text view" android:layout_gravity="center"/> </ViewFlipper> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="prevous" android:id="@+id/previous" android:layout_alignParentBottom="true" android:layout_marginLeft="16dp" android:layout_marginBottom="16dp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next" android:id="@+id/next" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_marginBottom="16dp" android:layout_marginRight="16dp"/> </RelativeLayout>