Pr.Pg Next Pg

View Flipper Tutorials

 

  • 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.

  • To illustrtate View Flipper we build an appliaction in which the views are flipped continuously.

  • Below is the XML code of the file.

 

flipper.xml

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

 

<ViewFlipper

android:id="@+id/viewFlipper1"

android:layout_width="match_parent"

android:layout_height="fill_parent">

 

<TextView

android:id="@+id/textView1"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_gravity="center"

android:gravity="center"

android:textSize="90sp"

android:text="Hey" />

 

<TextView

android:id="@+id/textView2"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_gravity="center"

android:gravity="center"

android:textSize="100sp"

android:text="I" />

 

<TextView

android:id="@+id/textView3"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_gravity="center"

android:gravity="center"

android:textSize="100sp"

android:text="Am" />

 

<TextView

android:id="@+id/textView4"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_gravity="center"

android:textSize="90sp"

android:gravity="center"

android:text="Android" />

</ViewFlipper>

 

</LinearLayout>

  • The views inside the ViewFlipper will be flipped.

  • Below is the Java Code of the file.

 

Flipper.java

 

package com.example.helloandroid;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ViewFlipper;

 

public class Flipper extends Activity implements OnClickListener {

ViewFlipper ourFlipper;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.flipper);

ourFlipper = (ViewFlipper)findViewById(R.id.viewFlipper1);

ourFlipper.setFlipInterval(500);

ourFlipper.setOnClickListener(this);

}

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

if(ourFlipper.isFlipping()){

ourFlipper.stopFlipping();

}

else{

ourFlipper.startFlipping();

}

}

 

}

 

 

  • In this activity we will Flipped through the various Text Views when the user clicks on the screen and will stop the flipping when the user taps on the screen again.

 

Pr.Pg border                                              Next Pg