How can i make a dynamic flipping screen(like that of iPhone) in Android

Muhammad Maqsoodur Rehman picture Muhammad Maqsoodur Rehman · Apr 19, 2010 · Viewed 27.2k times · Source

I am parsing data through the web service. I want the flipping horizontally rather than vertically. Here is a tutorial where ViewFlipper is used but it is for static data.


Here is our code where we need flipping between 2 activities:

Splash.java

public class Splash extends Activity{

        /** Called when the activity is first created. */

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

                    startActivity(new Intent(Splash.this, MainMenu.class));
                    Splash.this.finish();                                     
        }
    }

Splash.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/splash">
</AbsoluteLayout>

Menu.java

public class Menu extends Activity{

        /** Called when the activity is first created. */

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);               
            setContentView(R.layout.menu);                                       
        }
    }

Menu.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/menu">
</AbsoluteLayout>

Answer

stealthcopter picture stealthcopter · Apr 19, 2010

You can add pages to a ViewFlipper dynamically using addView.

  flipper= (ViewFlipper) findViewById(R.id.flipper1);
  flipper.addView(myView,myViewIndex);

Where myView is the view you wish to add, and myViewIndex is the index in the viewflipper you want to add this new view.

You can then set the animations to preform on changing of views:

  flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_in));
  flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));

Then to flip to this page you can use:

  flipper.setDisplayedChild(myViewIndex);

Where left_in.xml is defined as

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

and left_out.xml is:

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