Dragable button just like unlock button in Android default emulator

192 Views Asked by At

I need a drag able button just like unlock button in Android default emulator (similar to image below). Can any one help me how to implement it? I need layout or code for it.

enter image description here

1

There are 1 best solutions below

2
On BEST ANSWER

To make the button draggable attach a listener to the Object such as OnTouchListener, OnLongClickListener . When the user drags the data a image is displayed and it is called as Drag Shadow. It is initialized using the constructor View.DragShadowBuilder. The dragging is started using startDrag() method which has four parameters.

To create a Drop view we must attach OnDragListener to the view where the data is to be dropped. Various actions which execute during the process are.

Sample code:

  myOnTouchListener = new OnTouchListener() {
         public boolean onTouch(View v, MotionEvent me){
             if (me.getAction() == MotionEvent.ACTION_DOWN){
                 oldXvalue = me.getX();
                 oldYvalue = me.getY();
                 Log.i(myTag, "Action Down " + oldXvalue + "," + oldYvalue);
             }else if (me.getAction() == MotionEvent.ACTION_MOVE  ){
                LayoutParams params = new LayoutParams(v.getWidth(), v.getHeight(),(int)(me.getRawX() - (v.getWidth() / 2)), (int)(me.getRawY() - (v.getHeight())));
                v.setLayoutParams(params);
             }
             return true;
         }
     };

For more details, please refer here.