Search code examples
androiddelayviewflipper

how to flip view by viewflipper in android?


In android app i m using two view flipper to flip the view. I want to provide the delay between flipping the view. I am invoking the on click handler on a view flipper. Here is my code.

@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.oldmactwo);

            flipper = (ViewFlipper) findViewById(R.id.jetViewflipper);
            flippercow=(ViewFlipper) findViewById(R.id.cowViewflipper);

            flippercow.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "on click method call",Toast.LENGTH_SHORT).show();              

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
                    flipper.setInAnimation(inFromLeftAnimation());
                    flipper.setOutAnimation(outToLeftAnimation());
                    flipper.showPrevious();

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    /*Thread splashThread=new Thread()
                    {
                        public void run() {
                            try {
                                sleep(5000);
                            } catch (InterruptedException e) {
                                // TODO: handle exception
                            }
                            finally{
                                //splashThread.stop();
                            }

                        };
                    };
                    splashThread.start();*/

    Toast.makeText(getApplicationContext(), "delay ends",Toast.LENGTH_SHORT).show();                
                    //getcowFlipper();              
                    flippercow.setInAnimation(inFromBottomAnimation());
                    flippercow.setOutAnimation(outToTopAnimation());
                    flippercow.showNext();
                    //flipper.showPrevious();
    Toast.makeText(getApplicationContext(), "method ends",Toast.LENGTH_SHORT).show();               

                }
            });
     }

In the above code the delay is executed first and then view flips later.


Solution

  • Your inFromLeftAnimation() + inFromRightAnimation() + outFromLeftAnimation() + outFromRightAnimation() methods contains part like this:

    inFromLeft.setDuration(400);
    

    Above part will give a 400ms delay. Ofcourse you also got the inFromRight, outFromLeft etc.

    Example:

        private 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(400);
          inFromLeft.setInterpolator(new AccelerateInterpolator());
          return inFromLeft;
        }