How to avoid pressing the Android button on the side of the screen

I have implemented drag and drop buttons in my application, but the problem is that the drag and drop button comes out from the side, I can not see the button. but fix it. I need to drag the side of the screen. I tried under the code, if I am not corrected correctly,

@Override
    public boolean onTouch(View v, MotionEvent event) 
    {
            final int d_X = (int) event.getRawX();
            final int d_Y = (int) event.getRawY();
            PointF start = new PointF();

            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    start.set(event.getX(),event.getY());
                    if(shouldAllowToDrag) // if it true then i will allow to drag.
                    {
                        RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                        _xDelta = d_X - lParams.leftMargin;
                        _yDelta = d_Y - lParams.topMargin;
                    }
                    else 
                    {
                        //check for double click
                        long pressTime = System.currentTimeMillis();
                        // If double click...
                        String buttonTitle = moveButton.getText().toString();
                        if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
                            Log.i(TAG,"doubleTappedistrue");

                            hasLatchedDown = false;
                            if(buttonTitle.equals("Unlocked"))
                            {
                                hasButtonUnlocked = false;
                                moveButton.setText("Locked");
                            }
                            else if(buttonTitle.equals("Locked"))
                            {
                                hasButtonUnlocked = true;
                                moveButton.setText("Unlocked");
                            }
                        }
                        else
                        {
                            hasLatchedDown = true;
                            buttonLastStateText = buttonTitle;
                            moveButton.setText("Unlocked");
                        }
                        lastPressTime = pressTime;  

                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if(!shouldAllowToDrag)
                    {
                        if(hasLatchedDown)
                        {
                            hasLatchedDown = false;
                            moveButton.setText(buttonLastStateText);
                        }
                    }
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    if(shouldAllowToDrag){
                        View parentView = (View) v.getParent();
                        parentView.getWidth();
                        parentView.getHeight();
                        imageView.getWidth();
                        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                        layoutParams.leftMargin = d_X - _xDelta;
                        layoutParams.topMargin = d_Y - _yDelta;

                    v.setLayoutParams(layoutParams);
                    }




                    break;
            }
            return true;
    }
+3
source share
1 answer

You should add a min / max check:

layoutParams.leftMargin = Math.min(Math.max(d_X - _xDelta,0), parentView.getWidth() - layoutParams.width);

Same idea for topMargin. (code does not compile) Let me know if this helped.

+2
source

All Articles