Question

I’m working on an app in which I want to drag a button. I have written two programs. The problem is that each of them is fulfilling only a specific part of what I want. I want a button which can be moved freely on the screen (no disappearing and stuff) and I also need the program to give me the coordinates of the local button position while moving (permanent).

In the first program is the problem that the button disappears while dragging. This happens for the negative values of x and y and also for positive x values (not for positive y values; here is everything perfect). Positive y values are everything above the button, positive x values everything on the right side of the button and so on. For the negative values the button disappears like there is wall which covers the button. For the positive x values the button vanishes with rising x value (a little bit like a fizzing tablet in water).

Here is the code:

seat_leftright = (ImageButton) findViewById(R.id.seat_leftright);
    seat_leftright.setOnTouchListener(new OnTouchListener() {

   int k = 0;
   int prevX,prevY;
   int x=0;
   int y=0;

    @Override
    public boolean onTouch(final View v,final MotionEvent event)
      { 
      final LinearLayout.LayoutParams par=(LinearLayout.LayoutParams)v.getLayoutParams();        

      switch(event.getAction())
        {
        case MotionEvent.ACTION_MOVE:
          {

              if(k == 0){ //otherwise there would be an offset when touching the button the first time
                            prevY=(int)event.getRawY(); 
                prevX=(int)event.getRawX();
              }
              y+=prevY -(int)event.getRawY();
              prevY=(int)event.getRawY();                 
              par.bottomMargin = y;


              x+=(int)event.getRawX()-prevX;
              prevX=(int)event.getRawX();
                  par.leftMargin = x;


              Log.i("LOG","x: "+ x +", y: " + y );

              k++;

              v.setLayoutParams(par);

              return true; 
          }
        case MotionEvent.ACTION_UP:
          {
          par.bottomMargin=0;
          par.leftMargin=0;
          k=0;                  
          y=0;
          x=0;
          v.setLayoutParams(par);
          return true;
          }
        }
      return false;
      }
  });

}

In the second program I used the DragShadowBuilder function. The button works perfectly in this program, so it is not disappearing or the like. Here I have problems with receiving the values. I constantly need the x and y position of the button while moving. I tried it with the Action_drag_location, but it only returns the coordinates when I’m dragging the button above another button (here it is the button “arrow_down”). Replacing the “arrow_down” button with my background for constantly receiving the coordinates didn’t work at all. I also tried to combine my first program with the second with the result that I didn’t received any values at all.

I hope you can help me with this. I’m grateful for every kind of help!

Below the code of the second program.

OnTouchListener myOnTouchListener = new OnTouchListener() {


        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                        view);
                view.startDrag(null, shadowBuilder, view, 0);
                view.setVisibility(View.INVISIBLE);
                return true;
            }



            if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                view.setVisibility(View.VISIBLE);}
                return true;

        }
    };

    OnDragListener myDragListener = new OnDragListener() {

        @Override
        public boolean onDrag(View layoutview, DragEvent dragevent) {

            int action = dragevent.getAction();
            View view = (View) dragevent.getLocalState();

            switch (action) {
            case DragEvent.ACTION_DRAG_STARTED:
                Log.i("LOG","DragStarted");
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                break;
            case DragEvent.ACTION_DRAG_LOCATION:
                float x = (int)layoutview.getX();
                float y = (int)layoutview.getY();
                Log.i("COORDS","X: " + x +" Y: " + y);
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                break;
            case DragEvent.ACTION_DROP:                 
                break;

            case DragEvent.ACTION_DRAG_ENDED:
                Log.d("LOG", "Drag ended");
                if (dropEventNotHandled(dragevent)) {
                    view.setVisibility(View.VISIBLE);
                }
                break;
            default:
                break;
            }
            return true;
        }

        private boolean dropEventNotHandled(DragEvent dragEvent) {
            return !dragEvent.getResult();
        }
    };

    findViewById(R.id.arrow_up).setOnTouchListener(myOnTouchListener);    
    findViewById(R.id.arrow_down).setOnDragListener(myDragListener);  
Was it helpful?

Solution

@Jay: Thanks for your help, but that didn't solve the problem. The disappearing buttons were due to my layout, where I had this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/seat" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="20dp"
    android:layout_marginBottom="160dp"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/seat_updown"
        android:layout_width="120dp"
        android:layout_height="140dp"
        android:scaleType="fitCenter"
        android:src="@drawable/doublearrow"
        android:rotation="90" /> />


</LinearLayout>

Because the ImageButton was imbedded in the LinearLayout it disappeared!

OTHER TIPS

For button drag & drop I share my code here check it.

Here button touch listener.

MultiTouchListener touchListener = new MultiTouchListener(this);
onButton.setOnTouchListener(touchListener);

Touch listener class.

public class MultiTouchListener implements OnTouchListener {

    private float mPrevX;
    private float mPrevY;

    public MainActivity mainActivity;

    public MultiTouchListener(MainActivity mainActivity1) {
        mainActivity = mainActivity1;
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        float currX, currY;
        int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN: {

            mPrevX = event.getX();
            mPrevY = event.getY();
            break;
        }

        case MotionEvent.ACTION_MOVE: {

            currX = event.getRawX();
            currY = event.getRawY();

            MarginLayoutParams marginParams = new MarginLayoutParams(
                    view.getLayoutParams());
            marginParams.setMargins((int) (currX - mPrevX),
                    (int) (currY - mPrevY), 0, 0);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    marginParams);
            view.setLayoutParams(layoutParams);

            break;
        }

        case MotionEvent.ACTION_CANCEL:
            break;

        case MotionEvent.ACTION_UP:

            break;
        }

        return true;
    }

}

Sure, you can get these, make sure the views are drawn atleast once before you try to get the positions. You could try to get the positions in onResume() and try these functions

view.getLocationInWindow() or view.getLocationOnScreen()

or if you need something relative to the parent, use

view.getLeft(), view.getTop()

Follow this link for coordinates.

Retrieve the X & Y coordinates of a button in android?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top