How to use ViewFlipper
You have probably already heard about ViewFlipper. If not, go to website: http://developer.android.com/r
and check it.
"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."
Sounds good. Let's try it!
Create a ViewFlipper in your main xml layout like this:
- <ViewFlipper android:id="@+id/flipper"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- android:layout_below="@+id/CockpitLayout"
> - <include android:id="@+id/firstlayout" layout="@layout/first" />
- <include android:id="@+id/listlayout" layout="@layout/list" />
- <include android:id="@+id/thirdlayout" layout="@layout/productlayout&qu
ot; /> - <include android:id="@+id/fourthlayout" layout="@layout/vendorlayout&quo
t; /> - </ViewFlipper>
Notice that you have a main layout, and 4 other (firstlayout.xml, listlayout.xml, thirdlayout.xml and fourthlayout.xml) to get it work.
Once you have done with all the layouts, you can set up your Main activity to handle and switch them.
create these variables first:
- private ViewFlipper mFlipper;
- private int mCurrentLayoutState;
put this in your onCreate():
- mFlipper = (ViewFlipper) findViewById(R.id.flipper);
- mCurrentLayoutState = 0;
Now do a method wich is switch between layouts:
- /**
- * Switches the layout to the given constant ID as a parameter
- * @param switchTo (should be 0 or a positive number)
- */
- public void switchLayoutStateTo(int switchTo){
- while(mCurrentLayoutState != switchTo){
- if(mCurrentLayoutState > switchTo){
- mCurrentLayoutState--;
- mFlipper.setInAnimation(inFromLeftAnimation());
- mFlipper.setOutAnimation(outToRightAnimation());
- mFlipper.showPrevious();
- } else {
- mCurrentLayoutState++;
- mFlipper.setInAnimation(inFromRightAnimation());
- mFlipper.setOutAnimation(outToLeftAnimation());
- mFlipper.showNext();
- }
- };
- }
Finally the animations:
- protected Animation inFromRightAnimation() {
- Animation inFromRight = new TranslateAnimation(
- Animation.RELATIVE_TO_PARENT, +1.0f,
- Animation.RELATIVE_TO_PARENT, 0.0f,
- Animation.RELATIVE_TO_PARENT, 0.0f,
- Animation.RELATIVE_TO_PARENT, 0.0f);
- inFromRight.setDuration(500);
- inFromRight.setInterpolator(new AccelerateInterpolator());
- return inFromRight;
- }
- protected Animation outToLeftAnimation() {
- Animation outtoLeft = new TranslateAnimation(
- Animation.RELATIVE_TO_PARENT, 0.0f,
- Animation.RELATIVE_TO_PARENT, -1.0f,
- Animation.RELATIVE_TO_PARENT, 0.0f,
- Animation.RELATIVE_TO_PARENT, 0.0f);
- outtoLeft.setDuration(500);
- outtoLeft.setInterpolator(new AccelerateInterpolator());
- return outtoLeft;
- }
- protected Animation inFromLeftAnimation() {
- Animation inFromLeft = new TranslateAnimation(
- Animation.RELATIVE_TO_PARENT, -1.0f,
- Animation.RELATIVE_TO_PARENT, 0.0f,
- Animation.RELATIVE_TO_PARENT, 0.0f,
- Animation.RELATIVE_TO_PARENT, 0.0f);
- inFromLeft.setDuration(500);
- inFromLeft.setInterpolator(new AccelerateInterpolator());
- return inFromLeft;
- }
- protected Animation outToRightAnimation() {
- Animation outtoRight = new TranslateAnimation(
- Animation.RELATIVE_TO_PARENT, 0.0f,
- Animation.RELATIVE_TO_PARENT, +1.0f,
- Animation.RELATIVE_TO_PARENT, 0.0f,
- Animation.RELATIVE_TO_PARENT, 0.0f);
- outtoRight.setDuration(500);
- outtoRight.setInterpolator(new AccelerateInterpolator());
- return outtoRight;
- }
I hope you found it useful!
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@StephanieNich10 (Stephanie Nichols)#android I laughed so hard at ochocinco 's avi. Hahahaha http://t.co/MPuhhi3m
26 weeks 4 hours ago -
@CarlaAtkins8 (Carla Atkins)#android Omg! This is actually f'n interesting http://t.co/JodMOehr
26 weeks 4 hours ago -
@MarianMcleod12 (Marian Mcleod)#android Precisely what song is? http://t.co/YmJXU0rB
26 weeks 4 hours ago -
@JoBeach15 (Jo Beach)#android haha this made me laugh, i love ted:-) http://t.co/gtcWQ79C
26 weeks 4 hours ago -
@aochart3 (青ちゃ)Start playing Paradise Island on Android http://t.co/DEID0Ao5 #Android #Androidgames #Gameinsight http://t.co/e1bifSeL
26 weeks 4 hours ago
Poll
Useful resources
Android Development Projects
- Android and iOS apps for magnetic sensor data by hutington
- Aplicacion para celulares by xeon19
- Create Android Clock Widgets by mobstarmedia
- Mobile App Graphic Design by sentersix
- PhoneGap/Cordova Plugin for PJSIP - repost by bzimmer
- Druapl pro wanted with api for apps experience - start right now - repost by tampacoder
- A simple Ramadan App for Android - 5 days completion! by abidchy
- Referral group site and APP for Iphone and Androd by newhomepage
- Simple Android Game - repost by aymanabsi
- face recognition application by bertjan41



