4

I am developing an application which have multiple activities and I want to implement swipe(left,right,up,down) Transition between activities(Not fragments) like snapchat does.I did some research but I was failed to find a proper approach.Can anyone suggest how can I approach for above situation?Thank You.

Sanju Talreja
  • 39
  • 2
  • 7

2 Answers2

14

I don't believe you can get the same smooth swipe by using activities. (Someone feel free to correct me). You could capture the swipe manually and load the next activity. You wouldn't see a 'Preview' and it would be very similar to just clicking a button.

The smoothness that a view pager provides is due to fragments. Again (if I am not mistaken), only one activity can be presented to the user at a time. So you can't show a preview of the next activity. Fragments gets around this because you can have multiple fragments.

I am not sure if you have a strong reason to not use fragments. They are pretty straight forward to use and you more or less can still use activities. You would have a fragment per activity.

So although I don't want to side-step answering the question when you want to use activities but I don't believe you will be able to continue using activities and get the desired results. You will have an easier time using fragments than trying to customize the functionality with activities.

http://developer.android.com/training/animation/screen-slide.html

http://developer.android.com/training/implementing-navigation/lateral.html

How to change activity with left-right swipe

Update: Android - 2 Activities active at the same time

Wanted to verify you can't have 2 activities at the same time. So no matter what your going to have to split the functionality out of a single activity to do this. Although you 'can' do this without fragments, you will end up with a complicated solution. I would suggest using what Android provides and converting it to fragments. Its pretty straight forward.

Community
  • 1
  • 1
Kalel Wade
  • 7,742
  • 3
  • 39
  • 55
  • I think you are right Kalel and sorry i couldnt vote up your answer because I am short of reputations:( – Sanju Talreja Apr 02 '15 at 17:19
  • @SanjuTalreja Thanks, yes, I would recommend fragments unless you have a specific reason not to use them. They are pretty easy to set up. – Kalel Wade Apr 02 '15 at 19:49
  • Can you use `ViewPager` on 4 directions (up, down, left, right) like Snapchat or should you nest 2 `ViewPager`s? I read that nesting them is a bad idea – Alex Burdusel Feb 17 '17 at 08:54
  • At that point, you may be better of handling the gestures themselves and handling the behavior accordingly. – Kalel Wade Apr 26 '17 at 16:03
-1

You can do that with 5 layouts putting them on each other...making only 1 visible the menu for example ..than when the user touches the right side for example the layout_right will pop will try to move from right to left but only if the user will swipe the layout from right to left...than the layout_right will come over the first layout of the menu_layout...and will start to fade in ... and if user wants to go to menu will do a left to right swipe to come to the first layout the menu

some example :

private void slide(final View v) {
    if (v.getLeft() != 0) {
        Animation toLeft = new TranslateAnimation(v.getRight() - 72, 0, 0, 0);
        toLeft.setDuration(animDuration);
        toLeft.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                v.clearAnimation();
                RelativeLayout.LayoutParams params = (LayoutParams) v
                        .getLayoutParams();
                params.leftMargin = 0;
                params.rightMargin = 0;
                v.setLayoutParams(params);
            }
        });
        v.startAnimation(toLeft);
    }
    else {
        Animation toRigh = new TranslateAnimation(0, -v.getWidth() - 72, 0, 0);
        toRigh.setDuration(animDuration);
        toRigh.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                v.clearAnimation();
                RelativeLayout.LayoutParams params = (LayoutParams) v
                        .getLayoutParams();
                params.leftMargin = (int) -(v.getWidth() - v.getWidth() * .15f);
                params.rightMargin = (int) (v.getWidth() * .85f);
                v.setLayoutParams(params);
            }
        });
        v.startAnimation(toRigh);
    }
}

private void slideBack(final View v) {
    if (v.getLeft() != 0) {
        Animation toLeft = new TranslateAnimation(-v.getLeft(), 0, 0, 0);
        toLeft.setDuration(animDuration + 100);
        toLeft.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                v.clearAnimation();
                RelativeLayout.LayoutParams params = (LayoutParams) v
                        .getLayoutParams();
                params.leftMargin = 0;
                params.rightMargin = 0;
                v.setLayoutParams(params);
            }
        });
        v.startAnimation(toLeft);
    }
}


@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        xPos = (int) event.getX();
    }

    if (event.getAction() == MotionEvent.ACTION_MOVE) {
        RelativeLayout.LayoutParams params = (LayoutParams) v.getLayoutParams();
        if (params.rightMargin + 1 > 0) {
            params.leftMargin = (int) (event.getRawX() - xPos);
            params.rightMargin = (int) (-event.getRawX() + xPos);
            v.setLayoutParams(params);
        }
    }
    if (event.getAction() == MotionEvent.ACTION_UP) {
        RelativeLayout.LayoutParams params = (LayoutParams) v.getLayoutParams();
        if (params.rightMargin < v.getWidth() / 2) {
            params.leftMargin = 0;
            params.rightMargin = 0;
        }
        else {
            params.leftMargin = (int) -(v.getWidth() - v.getWidth() * .15f);
            params.rightMargin = (int) (v.getWidth() * .85f);
        }

        v.setLayoutParams(params);
    }
    return true;
}

}

dodo
  • 38
  • 12
  • This option pretty much 'mimics' fragments but without the efficiency that the view pager provides. Also, doesn't get around pulling out code from the separate activities. I would still advise using fragments with view pager. – Kalel Wade Apr 02 '15 at 19:14
  • 1
    Transition between activities(Not fragments) thats what he said – dodo Apr 02 '15 at 19:30
  • Your option is not a transition between activities. Its a transition between views? Views are not activities. – Kalel Wade Apr 02 '15 at 19:35