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

WebDev

http://blog.kerul.net PHP DEVELOPMENT TOOLS Download the XAMPP latest version from www.apachefriends.org . This installation file contains the Apache web server, PHP 5 and 4 interpreter, and the MySQL 5 Community edition. - download latest version MozillaFireFox (OpenSource web browser firefox) – download latest version Google Chrome – fastest web browser on earcth – fast download chrome here TEACHING PLAN Download the teaching plan here for Web/Internet Programming ( download ) NOTES HTML references HTML Editor -  http://www.sublimetext.com/ Lab 1: HTML Basics -  http://www.w3schools.com/html/ Lab 2: Responsive Design:  http://www.w3schools.com/html/html_responsive.asp Lab 3: HTML Forms  http://www.w3schools.com/html/html_forms.asp Lab 4: HTML 5  http://www.w3schools.com/html/html5_intro.asp Lab 5: Bootstrap for responsive Web -  http://www.w3schools.com/bootstra

Submit your blog address here

Create your own blog and send the address by submitting the comment of this article. Make sure to provide your full name, matrix and URL address of your blog. Refer to the picture below. Manual on developing a blog using blogger.com and AdSense, download here … Download Windows Live Writer (a superb offline blog post editor)

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.

Contact Us at blog.kerul.net

Powered by EMF HTML Contact Form