Advanced Android - ViewFlipper



ViewFlipper is an extension class of ViewAnimator, which animates between two or more views that have been added to it. Specfically, only one child is shown at a time

Example

This example demostrate about how to integrate Android ViewFlipper.

Step 1 − Create a new project in Android Studio,go to File New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

Here abc indiacates the logo of tutorialspoint.com
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:orientation = "vertical"
   android:layout_width = "fill_parent"
   android:layout_height = "fill_parent" >
   <Button android:id = "@+id/flip_me"
      android:layout_width = "fill_parent"
      android:layout_height = "wrap_content"
      android:text = "Flip Me!" />
   <ViewFlipper android:id = "@+id/details"
      android:layout_width = "fill_parent"
      android:layout_height = "fill_parent" >
      <ImageView
         android:id = "@+id/imageView1"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:src = "@drawable/abc" />
      <TextView
         android:layout_width = "fill_parent"
         android:layout_height = "wrap_content"
         android:textStyle = "bold"
         android:textColor = "#FFFF0000"
         android:text = "This is the second panel" />
      <TextView
         android:layout_width = "fill_parent"
         android:layout_height = "wrap_content"
         android:textStyle = "bold"
         android:textColor = "#FFFFFF00"
         android:text = "This is the third panel" />
    </ViewFlipper>
</LinearLayout>

Step 3 − Add the following code to src/MainActivity.java

package myapplication.example.com.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;

public class MainActivity extends Activity {
   ViewFlipper flipper;
   @Override
   public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.activity_main);
      flipper = (ViewFlipper)findViewById(R.id.details);
      Button btn=(Button)findViewById(R.id.flip_me);
      btn.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
            flipper.showNext();
         } 
      });
   }
}

Step 4 − No need to change manifest.xml

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Flip
Advertisements