Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create multiple view with drag and drop functionality in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.50k
    Comment on it

    This tutorial will help to the develop the Drag and Drop functionality, individual can create multiple view in their android application. Each view will have different onTouchListener and onLongClickListener.

    Step 1- Create a XML file res/activity_main.xml

    <Button
        android:id="@+id/button"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:text="Button View" />
    
    <AbsoluteLayout
        android:id="@+id/boardLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/board"
        android:gravity="center"
        android:padding="20dp" >
    
    </AbsoluteLayout>
    

     

    Step-2 Create a Activity called src/MainActivity

    import android.app.Activity;
    import android.content.ClipData;
    import android.os.Bundle;
    import android.view.DragEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnLongClickListener;
    import android.view.ViewGroup;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.AbsoluteLayout;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    
    
    public class DragDrop extends Activity implements OnLongClickListener,
    View.OnDragListener,OnClickListener{
    
        private RelativeLayout relativeLayoutrectangleView;
        private ImageView rectangleView;
        private String IMAGE_RECTANGLE = "RECTANGLE";
        float X,Y;
        boolean temp;
        private AbsoluteLayout absoluteLayout, boardLayout,containView;
        private Button button;
        private int heightlayout,widthlayout;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            boardLayout=(AbsoluteLayout)findViewById(R.id.boardLayout);
            boardLayout.setOnDragListener(this);
    
            button=(Button)findViewById(R.id.button);
            button.setOnClickListener(this);
        }
    
    // Override onDrag() method called when start to drag a view with some different event 
    
    
    
     @Override
            public boolean onDrag(View v, DragEvent event) {
    
                // Defines a variable to store the action type for the incoming
                // event
                final int action = event.getAction();
    
                // Handles each of the expected events
                switch (action) {
    
                case DragEvent.ACTION_DRAG_STARTED:
    
                    // Invalidate the view to force a redraw in the new tint
                    v.invalidate();
    
                    // returns true to indicate that the View can accept the
                    // dragged data.
                    return true;
    
                case DragEvent.ACTION_DRAG_ENTERED:
    
                    // Invalidate the view to force a redraw in the new tint
                    v.invalidate();
    
                    return false;
    
                case DragEvent.ACTION_DRAG_LOCATION:
    
                    X = event.getX();
                    Y = event.getY();
    
                    if(X>widthlayout && Y>heightlayout){
                        temp = true;
                    }
                    // Ignore the event
                    return false;
    
                case DragEvent.ACTION_DRAG_EXITED:
    
    
                    //v.invalidate();
    
                    return false;
    
                case DragEvent.ACTION_DROP:
    
                    // Gets the item containing the dragged data
                    ClipData dragData = event.getClipData();
    
                    // Gets the text data from the item.
                    final String tag = dragData.getItemAt(0).getText().toString();
                    View view = (View) event.getLocalState();
                    ViewGroup viewgroup = (ViewGroup) view.getParent();
                    viewgroup.removeView(view);
    
                    X = event.getX();
                    Y = event.getY();
                    view.setX(X-(view.getWidth()/2));
                    view.setY(Y-(view.getHeight()/2));
                    containView = (AbsoluteLayout) v;
                    containView.addView(view);
    
                    view.setVisibility(View.VISIBLE);
                    // Invalidates the view to force a redraw
                    v.invalidate();
    
                    // Returns true. DragEvent.getResult() will return true.
                    return true;
    
    
                case DragEvent.ACTION_DRAG_ENDED:
    
                    v.invalidate();
    
    
                    return false;
    
                default:
                    break;
                }
    
                return false;
            }
    

    // Override onLongClick() method used to create a shadow of view when a start drag .

    @Override
        public boolean onLongClick(View v) {
            String tag = v.getTag().toString();
            ClipData dragData = ClipData.newPlainText("TAG", tag);
    
            // Instantiates the drag shadow builder.
            View.DragShadowBuilder myShadow = null;
            //v.setBackgroundResource(0);
    
    
    
            if(tag.equals(IMAGE_RECTANGLE)){
                v.setVisibility(View.INVISIBLE);
    
                myShadow = new View.DragShadowBuilder(relativeLayoutrectangleView);
            }
            // Starts the drag
            v.startDrag(dragData, myShadow, v, 0);
            return false;
    
        }
    

    // this method will call when press button to create a new view each time press this button.

    @Override
        public void onClick(View v) {
    
            createCustomView();
    
        }
    
    
    private void createCustomView() {
            absoluteLayout=new AbsoluteLayout(this);
            absoluteLayout.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            relativeLayoutrectangleView = new RelativeLayout(this); 
            relativeLayoutrectangleView.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    
            relativeLayoutrectangleView.getLayoutParams().height=100;
            relativeLayoutrectangleView.getLayoutParams().width=100;
    
            rectangleView=new ImageView(this);
            rectangleView.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            relativeLayoutrectangleView.setTag(IMAGE_RECTANGLE);
            rectangleView.getLayoutParams().height=300;
            rectangleView.getLayoutParams().width=300;
            rectangleView.setImageResource((R.drawable.rectangle));
            relativeLayoutrectangleView.setOnLongClickListener(this);
            relativeLayoutrectangleView.setBackgroundResource(R.drawable.rectangle);
            ViewGroup.LayoutParams la_pm = rectangleView.getLayoutParams();
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(la_pm);
            //relativeLayoutrectangleView.addView(rectangleView,lp);  
    
            absoluteLayout.addView(relativeLayoutrectangleView);
            boardLayout.addView(absoluteLayout);
    
    
    
    
        }
    
    
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus){
            widthlayout=boardLayout.getWidth();
            heightlayout=boardLayout.getHeight();
        }
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: