Skip to main content

ViewFlipper Example–a simple FlashCard

UPDATE: Improved with Fling gesture (Sept 2012)

UPDATE: ViewFlipper with Flip-In and Flip-Out Animation (August 2012)

This tutorial is to demonstrate the ViewFlipper layout that is almost similar to CardLayout (in Java). The app will produce a simple Flash card that provide several screens with different picture for each card. Flip-in and Flip-out animation provided.

Added in Sept 2012 – an improvement to support Fling gesture – enjoy… The amendment is only on the coding part.

Some how the layout design (main.xml) is quite long. Later I’ll produce separated screen by including several XML layout from outside files.

Screenshots;

ViewFlipper of Simple FlashCard apps with Flip AnimationViewFlipper of Simple FlashCard apps with Flip AnimationViewFlipper of Simple FlashCard apps with Flip Animation

/layout/main.xml;

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

<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_height="fill_parent"
android:background="@drawable/smalldroid">
<!-- admob here -->
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="738a44d913034b9f"
/>
</TableRow>

<!-- header -->
<LinearLayout android:id="@+id/LinearLayout03"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:id="@+id/tvwelcome" android:layout_height="wrap_content"
android:text="Kad Tunjuk" android:layout_gravity="center"
android:layout_width="match_parent"></TextView>
</LinearLayout>

<!-- content -->
<LinearLayout android:id="@+id/LinearLayout02"
android:baselineAligned="false" android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="wrap_content">

<ViewFlipper android:id="@+id/ViewFlipper01"
android:layout_height="wrap_content" android:layout_width="wrap_content" >
<!-- adding views to ViewFlipper -->

<!-- View 1- Home/ketetapan - card1 -->
<TableLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView android:text="HOME"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow android:layout_width="match_parent">
<ImageView android:layout_width="wrap_content"
android:id="@+id/imageView1" android:layout_height="wrap_content"
android:src="@drawable/icon"></ImageView>
</TableRow>
<TableRow>
<TextView android:text="FLASHCARD ringkas"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>

<!-- View 2 - A - card2-->
<TableLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView android:text="HURUF A"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<ImageView android:layout_width="wrap_content"
android:id="@+id/imageView1" android:layout_height="wrap_content"
android:src="@drawable/arnab"></ImageView>
</TableRow>
<TableRow>
<TextView android:text="ARNAB"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>

<!-- View 3 - K - card3-->
<TableLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView android:text="HURUF K"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<ImageView android:layout_width="wrap_content"
android:id="@+id/imageView1" android:layout_height="wrap_content"
android:src="@drawable/kereta"></ImageView>
</TableRow>
<TableRow>
<TextView android:text="KERETA LUMBA"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>

</ViewFlipper>

</LinearLayout>

<!-- footer -->
<LinearLayout android:id="@+id/LinearLayout03"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:orientation="horizontal" android:layout_gravity="center">
<Button android:id="@+id/Button02" android:layout_height="wrap_content"
android:text="&lt;Previous" android:layout_width="wrap_content"
android:layout_gravity="center"></Button>
<Button android:id="@+id/ButtonHome" android:text=" Home "
android:layout_height="match_parent" android:layout_width="wrap_content"
android:layout_gravity="center"></Button>
<Button android:id="@+id/Button01" android:text=" Next&gt;"
android:layout_height="match_parent" android:layout_width="wrap_content"
android:layout_gravity="center"></Button>
</LinearLayout>

</LinearLayout>


Notice that each card is represented as a TableLayout in the ViewFlipper (ID=ViewFlipper01). inside the TableLayout there are one ImageView and two TextViews. The three buttons (Previous, Home and Next) are situated in another LinearLayout marked as footer.


There are additionals XML file you need to create. These files are needed as the animation setting. Start by creating a new folder in res, named anim/res/anim


/res/anim/flipinnext.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<translate
android:fromXDelta="-100%"
android:toXDelta="0%"
android:duration="500" />
</set>


/res/anim/flipoutnext.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<translate
android:fromXDelta="0%"
android:toXDelta="100%"
android:duration="500" />
</set>


/res/anim/flipinprevious.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<translate
android:fromXDelta="100%"
android:toXDelta="0%"
android:duration="500" />
</set>


/res/anim/flipoutprevious.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<translate
android:fromXDelta="0%"
android:toXDelta="-100%"
android:duration="500" />
</set>


 


Java Sourcecode; the project name is Flashcard2…


 



package net.kerul.Flashcard2;
 
//No animation on card transition
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ViewFlipper;
 
import com.google.ads.AdRequest;
import com.google.ads.AdView;
 
public class Flashcard2Activity extends Activity implements OnClickListener, OnGestureListener {
    protected GestureDetector gestureScanner;
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
 
    private Button next,previous;
    private AdView adView;
    //private ViewFlipper vf;
    //declaration for animation items
//    private ViewFlipper vf=(ViewFlipper)findViewById(R.id.ViewFlipper01);
//    private Animation animFlipInNext = AnimationUtils.loadAnimation(this, R.anim.flipinnext);
//    private Animation animFlipOutNext = AnimationUtils.loadAnimation(this, R.anim.flipoutnext);
//    private Animation animFlipInPrevious = AnimationUtils.loadAnimation(this, R.anim.flipinprevious);
//    private Animation animFlipOutPrevious = AnimationUtils.loadAnimation(this, R.anim.flipoutprevious);
    
    private ViewFlipper vf;
    private Animation animFlipInNext,animFlipOutNext, animFlipInPrevious, animFlipOutPrevious;
    
 
    /** Called when the activity is first created. */
    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gestureScanner = new GestureDetector(this);
        
        //vf for viewflipper
        vf=(ViewFlipper)findViewById(R.id.ViewFlipper01);
        animFlipInNext = AnimationUtils.loadAnimation(this, R.anim.flipinnext);
        animFlipOutNext = AnimationUtils.loadAnimation(this, R.anim.flipoutnext);
        animFlipInPrevious = AnimationUtils.loadAnimation(this, R.anim.flipinprevious);
        animFlipOutPrevious = AnimationUtils.loadAnimation(this, R.anim.flipoutprevious);
        
        //admob widget - private AdView adView;
        adView = (AdView)findViewById(R.id.ad);
        adView.loadAd(new AdRequest());
        
        next = (Button) findViewById(R.id.Button01);
        previous = (Button) findViewById(R.id.Button02);
        next.setOnClickListener(this);
        previous.setOnClickListener(this);
    }
 
    //@Override
    public void onClick(View v) {
        if (v == next) {
            vf.setInAnimation(animFlipInNext);
            vf.setOutAnimation(animFlipOutNext);
            vf.showNext();
        }
        if (v == previous) {
            vf.setInAnimation(animFlipInPrevious);
            vf.setOutAnimation(animFlipOutPrevious);
            vf.showPrevious();
        }
    }
    //this is the part to handle Gesture Listener
    @Override
    public boolean onTouchEvent(MotionEvent me){
        return gestureScanner.onTouchEvent(me);
    }
    public boolean onDown(MotionEvent e){
        return true;
    }
    //FLING gesture listener
    public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY){
        try {
            if(e1.getX() > e2.getX() && Math.abs(e1.getX() - e2.getX()) > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(this.getApplicationContext(), "Left", Toast.LENGTH_SHORT).show();
                vf.setInAnimation(animFlipInPrevious);
                vf.setOutAnimation(animFlipOutPrevious);
                vf.showPrevious();
            }else if (e1.getX() < e2.getX() && e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(this.getApplicationContext(), "Right", Toast.LENGTH_SHORT).show();
                vf.setInAnimation(animFlipInNext);
                vf.setOutAnimation(animFlipOutNext);
                vf.showNext();
            }
        } catch (Exception e) {
            // nothing
        }
        return true;
        
    }
    
    public boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY){
        return true;
    }
    public void onLongPress(MotionEvent e){}
    public void onShowPress(MotionEvent e){}
    public boolean onSingleTapUp(MotionEvent e){ return true;}
}
Images;

All images are situated in the /res/drawable-mdpi


ViewFlipper Example–a simple FlashCard - images


Complete project download;


You will find all source codes and images in the file, not to forget the Flashcard2.apk .Available for download here… (MS Skydrive - http://sdrv.ms/Q4LsXP) or (Google Drive - https://docs.google.com/open?id=0B34ZxOOoeSDdMVFiejVDY3ZxdWs)


All d best for your experiment…

Comments

  1. thanks for the sample UI layout tip

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. No problem, bro... Glad to help. One of my active project...

    ReplyDelete
  4. Superb sample.. good wrk bro :)

    ReplyDelete
  5. Hello D Rahman,

    My daughter is doing a tech fair and would like to use your example / tutorial for the basis of her programing project. Could we have your permission.

    ReplyDelete
  6. fine thanks but how to work with list with onitemselection

    ReplyDelete
  7. Its very helpful to me.
    But How to work with list items election for restaurent app

    ReplyDelete
  8. its gud, but here //admob widget - private AdView adView is not def ined properly. so it showing error. pleaqse check and some one help me.

    ReplyDelete
    Replies
    1. Not sure where's the error, pls check the code here... http://sdrv.ms/QvZeXn

      Delete
    2. Wondering what's the error, maybe u could share it with us...

      Delete
    3. yeah... i'll try this example( http://sdrv.ms/QvZeXn).
      soon i'll tell whether i can work properly with that or not !

      Delete
  9. thx brother , it's very simple . easy to understand .

    ReplyDelete
  10. Great example ! It was very useful for me.
    But, are animations in the right direction?

    ReplyDelete
  11. thank you,it is a nice example

    ReplyDelete
  12. This is a great tutorial thanks Mr Rahman. One question though; If I want to add an Audio to each image (screen) like an ABC Flashcards is this possible and if so, can you give us an exmaple please? Thanks

    Ali

    ReplyDelete
  13. That's a very good example brother and I have already downloaded but there is one problem and I need a little help with if you don't mind...I want each Audio to start 'Auto' without needing to press the play button and when hit next-button I want the current Audio (0) to stop and start the next audio (1) without the need to press the play button again to stop it. Please help am new Android and Java. Thanks. Ali

    ReplyDelete
  14. That's a very good example brother and I have already downloaded but there is one problem and I need a little help with if you don't mind...I want each Audio to start 'Auto' without needing to press the play button (if possible) and when press next-button I want the current Audio (0) to stop and start the next audio (1) without the need to press the play button again to stop it. Please help am new Android and Java. Thanks. Ali

    ReplyDelete
    Replies
    1. Whisking u all the best in learning android...

      Delete
  15. hello mr kerul
    ım from turkey
    your product is very nice.
    but there is error message
    Flashcard2' is missing required library: 'C:\android-juno\workspace\Flashcard2\libs\GoogleAdMobAdsSdk-6.1.0.jar' Flashcard2 Build path Build Path Problem

    thanks a lot

    ReplyDelete
    Replies
    1. I have prepared a simple tutorial for u to install the AdMob library. Pls go to this articel -> http://blog.kerul.net/2013/03/installing-admob-6-library-into-android.html

      Delete

Post a Comment

Popular posts from this blog

Several English proverbs and the Malay pair

Or you could download here for the Malay proverbs app – https://play.google.com/store/apps/details?id=net.kerul.peribahasa English proverbs and the Malay pair Corpus Reference: Amir Muslim, 2009. Peribahasa dan ungkapan Inggeris-Melayu. DBP, Kuala Lumpur http://books.google.com.my/books/about/Peribahasa_dan_ungkapan_Inggeris_Melayu.html?id=bgwwQwAACAAJ CTRL+F to search Proverbs in English Definition in English Similar Malay Proverbs Definition in Malay 1 Where there is a country, there are people. A country must have people. Ada air adalah ikan. Ada negeri adalah rakyatnya. 2 Dry bread at home is better than roast meat home's the best hujan emas di negeri orang,hujan batu di negeri sendiri Betapa baik pun tempat orang, baik lagi tempat sendiri. 3 There's no accounting for tastes We can't assume that every people have a same feel Kepala sama hitam hati lain-lain. Dalam kehidupan ini, setiap insan berbeza cara, kesukaan, perangai, tabia

Applications of Web 2.0

Web 2.0 describes the changing trends in the use of World Wide Web technology and web design that aim to enhance creativity , secure information sharing, collaboration and functionality of the web. Web 2.0 concepts have led to the development and evolution of web-based communities and hosted services , such as social-networking sites , video sharing sites , wikis , blogs . Find a website or web application that conform to the criteria of Web 2.0. Put the name of the application and the URL in the comment below. Please provide your full name and matrix number. Make sure the application you choose is not already chosen by your friend in the previous comment.

Login JSON Android using Login Activity

I’ve been trying to release this tutorial quite a while. At last after a long hard effort. Since HttpClient is not supported any more in Android SDK 23, I have to resort to org.json.JSONObject and java.net.HttpURLConnection library to do online database with JSON. The objective of this tutorial is to log-in from a mobile client with the username and password stored in an online database facility. STEP 1: Create a new Android project, this time choose the LoginActivity .

Most used STRING functions in my PHP coding

These are my favourite string manipulation functions in my daily coding life. Dedicated especially to Web Programming students. Read them and have fun. Expect a happiness after a storm , and you’ll find your “inner peace”… This post is still in draft. I’ll update and refine with more examples that I’ve personally develop. More after the break…

Bootstrap Template for PHP database system - MyCompanyHR

HTML without framework is dull. Doing hard-coded CSS and JS are quite difficult with no promising result on cross platform compatibility. So I decided to explore BootStrap as they said it is the most popular web framework. What is BootStrap? - Bootstrap is the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first web sites. (  http://www.w3schools.com/bootstrap/   ) Available here -  http://getbootstrap.com/ Why you need Flat-UI? Seems like a beautiful theme to make my site look professional. Anyway you could get variety of BootStrap theme out there, feel free to select here  http://bootstraphero.com/the-big-badass-list-of-twitter-bootstrap-resources/ Flat-UI is from DesignModo -   http://designmodo.com/flat/ Web Programming MyCompanyHR – PHP & MySQL mini project (with Boostrap HTML framework) Template 1: Template for the Lab Exercise. This is a project sample of a staff record management system. It has the PHP structured co