-1

I'm using this code to move a button in the screen but when I reach the sides it is moving out of the screens.

    private float mPrezX, mPrevY;

    private static final int MAX_CLICK_DURATION = 200;
    private long startClickTime;

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        int action = event.getActionMasked();

        Button gvup = (Button)findViewById(R.id.giveup);
        gvup.setBackground(getResources().getDrawable(R.drawable.btn));

        switch (action ) {
            case MotionEvent.ACTION_DOWN: {
                mPrevX = view.getX() - event.getRawX();
                mPrevY = view.getY() - event.getRawY();
                startClickTime = Calendar.getInstance().getTimeInMillis();//!!
                gvup.setBackground(getResources().getDrawable(R.drawable.btn1));
                break;
            }
            case MotionEvent.ACTION_MOVE:
            {
                view.animate()
                    .x(event.getRawX() + mPrevX)
                    .y(event.getRawY() + mPrevY)
                    .setDuration(0)
                    .start();

                gvup.setBackground(getResources().getDrawable(R.drawable.btn1));
                break;
            }
            case MotionEvent.ACTION_CANCEL:
                gvup.setBackground(getResources().getDrawable(R.drawable.btn1));
                break;
            case MotionEvent.ACTION_UP:
                long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
                if(clickDuration < MAX_CLICK_DURATION) {
                    //click event has occurred
                    gvup.setBackground(getResources().getDrawable(R.drawable.btn));
                    giveUp();
                }
                break;
        }
        return true;
    }

I'm also changing its background color if pressed and also made it clickable.

enter image description here

I need to keep that button inside the layout.

I have also tried this and this links but both of these approaches leading to squeeze the button when moved to the sides.

Please help. Thanks in advance.

enter image description here

Sourav Roy
  • 323
  • 4
  • 12

1 Answers1

0

Well you need to get its X location and add its width to it to see where the "right" edge is and compare if it greater or equal to your viewport's width. I haven't written JAVA in a while but that should do the trick.

Are you checking its location?

Perhaps

int rightEdge = currentPos.X + itsWidth;
if(rightEdge >= screenWidth) // something here

Maybe in your animate() method do your checking. Its hard for me to guess withough looking at what is making this happen. What is firing the event? What is event.getRawX() equal to? Is that a touch point on the screen? Are you animating after the point is selected? Or you can define a min/max x and min/max y and not let your circle past that.

            view.animate()
                if( .x(event.getRawX() + mPrevX) > screenWidth ){
                       .x(screenwidth - circleWidth)
                } // do the same for the Y - must check the max and min for both Y and X
                .x(event.getRawX() + mPrevX)
                .y(event.getRawY() + mPrevY)
                .setDuration(0)
                .start();
Radmation
  • 1,486
  • 1
  • 13
  • 30