0

I want to implement an Android-Application like gallery or photos. I have several small photos. When I click on a photo, I open another activity which displays the foto over the whole screen. I want to be able to drag the activity to the bottom and this drag-event to be treated like the back-button.

How is this possible?

user650708
  • 55
  • 4

1 Answers1

0

You could try to use the onTouchEvent and GestureDetector to detect the drag, then use the onBackPress():

public class StackAct1 extends AppCompatActivity {
protected static final float FLIP_DISTANCE = 50;
GestureDetector mDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stack);

    mDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {

        @Override
        public boolean onSingleTapUp(MotionEvent e) { return false;        }

        @Override
        public void onShowPress(MotionEvent e) {}
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  return false;     }

        @Override
        public void onLongPress(MotionEvent e) { }

        /**
         *
         * e1 The first down motion event that started the fling. e2 The
         * move motion event that triggered the current onFling.
         */
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (e1.getX() - e2.getX() > FLIP_DISTANCE) {
                Log.i("MYTAG", "Slide left...");
                return true;
            }
            if (e2.getX() - e1.getX() > FLIP_DISTANCE) {
                Log.i("MYTAG", "Slide right...");
                return true;
            }
            if (e1.getY() - e2.getY() > FLIP_DISTANCE) {
                Log.i("MYTAG", "Slide up...");
                return true;
            }
            if (e2.getY() - e1.getY() > FLIP_DISTANCE) {
                Log.i("MYTAG", "Slide down...");
                onBackPressed();
                return true;
            }
            Log.d("TAG", e2.getX() + " " + e2.getY());
            return false;
        }

        @Override
        public boolean onDown(MotionEvent e) { return false; }
    });
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return mDetector.onTouchEvent(event);
}}

Slide down in the UI of activity, it will run the onBackPress().

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • Thank you, is there a possibility to make the activity a bit smaller and seeing the other activity on the background, like it is handled in the photo and gallery app? – user650708 Dec 27 '21 at 11:42