Android - Drag and Drop

<p >Android drag/drop framework allows your users to move data from one View to another View in the current layout using a graphical drag and drop gesture. As of<b >API 11</b>drag and drop of view onto other views or view groups is supported.The framework includes following three important components to support drag &amp; drop functionality &minus;</p> <ul class="list" > <li > <p ><b >Drag event class:</b></p> </li> <li > <p ><b >Drag listeners:</b></p> </li> <li > <p ><b >Helper methods and classes:</b></p> </li> </ul> <h2 >The Drag/Drop Process</h2> <p >There are basically four steps or states in the drag and drop process:</p> <ul class="list" > <li > <p ><b >Started</b>&minus; This event occurs when you start dragging an item in a layout, your application calls<i >startDrag()</i>method to tell the system to start a drag. The arguments inside startDrag() method provide the data to be dragged, metadata for this data, and a callback for drawing the drag shadow.</p> <p >The system first responds by calling back to your application to get a drag shadow. It then displays the drag shadow on the device.</p> <p >Next, the system sends a drag event with action type<i >ACTION_DRAG_STARTED</i>to the registered drag event listeners for all the View objects in the current layout.</p> <p >To continue to receive drag events, including a possible drop event, a drag event listener must return<b >true</b>, If the drag event listener returns false, then it will not receive drag events for the current operation until the system sends a drag event with action type ACTION_DRAG_ENDED.</p> </li> <li > <p ><b >Continuing</b>&minus; The user continues the drag. System sends ACTION_DRAG_ENTERED action followed by ACTION_DRAG_LOCATION action to the registered drag event listener for the View where dragging point enters. The listener may choose to alter its View object&#39;s appearance in response to the event or can react by highlighting its View.</p> <p >The drag event listener receives a ACTION_DRAG_EXITED action after the user has moved the drag shadow outside the bounding box of the View.</p> </li> <li > <p ><b >Dropped</b>&minus; The user releases the dragged item within the bounding box of a View. The system sends the View object&#39;s listener a drag event with action type ACTION_DROP.</p> </li> <li > <p ><b >Ended</b>&minus; Just after the action type ACTION_DROP, the system sends out a drag event with action type ACTION_DRAG_ENDED to indicate that the drag operation is over.</p> </li> </ul> <h2 >The DragEvent Class</h2> <p >The<b >DragEvent</b>represents an event that is sent out by the system at various times during a drag and drop operation. This class provides few Constants and important methods which we use during Drag/Drop process.</p> <h3 >Constants</h3> <p >Following are all constants integers available as a part of DragEvent class.</p> <table class="table table-bordered" > <tbody > <tr > <th >Sr.No.</th> <th >Constants &amp; Description</th> </tr> <tr > <td >1</td> <td ><b >ACTION_DRAG_STARTED</b> <p >Signals the start of a drag and drop operation.</p> </td> </tr> <tr > <td >2</td> <td ><b >ACTION_DRAG_ENTERED</b> <p >Signals to a View that the drag point has entered the bounding box of the View.</p> </td> </tr> <tr > <td >3</td> <td ><b >ACTION_DRAG_LOCATION</b> <p >Sent to a View after ACTION_DRAG_ENTERED if the drag shadow is still within the View object&#39;s bounding box.</p> </td> </tr> <tr > <td >4</td> <td ><b >ACTION_DRAG_EXITED</b> <p >Signals that the user has moved the drag shadow outside the bounding box of the View.</p> </td> </tr> <tr > <td >5</td> <td ><b >ACTION_DROP</b> <p >Signals to a View that the user has released the drag shadow, and the drag point is within the bounding box of the View.</p> </td> </tr> <tr > <td >6</td> <td ><b >ACTION_DRAG_ENDED</b> <p >Signals to a View that the drag and drop operation has concluded.</p> </td> </tr> </tbody> </table> <h3 >Methods</h3> <p >Following are few important and most frequently used methods available as a part of DragEvent class.</p> <table class="table table-bordered" > <tbody > <tr > <th >Sr.No.</th> <th >Constants &amp; Description</th> </tr> <tr > <td >1</td> <td ><b >int getAction()</b> <p >Inspect the action value of this event..</p> </td> </tr> <tr > <td >2</td> <td ><b >ClipData getClipData()</b> <p >Returns the ClipData object sent to the system as part of the call to startDrag().</p> </td> </tr> <tr > <td >3</td> <td ><b >ClipDescription getClipDescription()</b> <p >Returns the ClipDescription object contained in the ClipData.</p> </td> </tr> <tr > <td >4</td> <td ><b >boolean getResult()</b> <p >Returns an indication of the result of the drag and drop operation.</p> </td> </tr> <tr > <td >5</td> <td ><b >float getX()</b> <p >Gets the X coordinate of the drag point.</p> </td> </tr> <tr > <td >6</td> <td ><b >float getY()</b> <p >Gets the Y coordinate of the drag point.</p> </td> </tr> <tr > <td >7</td> <td ><b >String toString()</b> <p >Returns a string representation of this DragEvent object.</p> </td> </tr> </tbody> </table> <h2 >Listening for Drag Event</h2> <p >If you want any of your views within a Layout should respond Drag event then your view either implements<b >View.OnDragListener</b>or setup<b >onDragEvent(DragEvent)</b>callback method. When the system calls the method or listener, it passes to them a DragEvent object explained above. You can have both a listener and a callback method for View object. If this occurs, the system first calls the listener and then defined callback as long as listener returns true.</p> <p >The combination of the<i >onDragEvent(DragEvent)</i>method and<i >View.OnDragListener</i>is analogous to the combination of the<b >onTouchEvent()</b>and<b >View.OnTouchListener</b>used with touch events in old versions of Android.</p> <h2 >Starting a Drag Event</h2> <p >You start with creating a<b >ClipData</b>and<b >ClipData.Item</b>for the data being moved. As part of the<i >ClipData</i>object, supply metadata that is stored in a<b >ClipDescription</b>object within the ClipData. For a drag and drop operation that does not represent data movement, you may want to use<b >null</b>instead of an actual object.</p> <p >Next either you can extend extend<b >View.DragShadowBuilder</b>to create a drag shadow for dragging the view or simply you can use<i >View.DragShadowBuilder(View)</i>to create a default drag shadow that&#39;s the same size as the View argument passed to it, with the touch point centered in the drag shadow.</p> <h2 >Example</h2> <p >Following example shows the functionality of a simple Drag &amp; Drop using<b >View.setOnLongClickListener()</b>,<b >View.setOnTouchListener()</b>and<b >View.OnDragEventListener()</b>.</p> <table class="table table-bordered" > <tbody > <tr > <th class="fivepct" >Step</th> <th >Description</th> </tr> <tr > <td >1</td> <td >You will use Android studio IDE to create an Android application and name it as<i >My Application</i>under a package<i >com.example.saira_000.myapplication</i>. While creating this project, make sure you<i >Target SDK</i>and<i >Compile With</i>at the latest version of Android SDK to use higher levels of APIs.</td> </tr> <tr > <td >2</td> <td >Modify<i >src/MainActivity.java</i>file and add the code to define event listeners as well as a call back methods for the logo image used in the example.</td> </tr> <tr > <td >3</td> <td >Copy image abc.png in<i >res/drawable-*</i>folders. You can use images with different resolution in case you want to provide them for different devices.</td> </tr> <tr > <td >4</td> <td >Modify layout XML file<i >res/layout/activity_main.xml</i>to define default view of the logo images.</td> </tr> <tr > <td >5</td> <td >Run the application to launch Android emulator and verify the result of the changes done in the application.</td> </tr> </tbody> </table> <p >Following is the content of the modified main activity file<b >src/MainActivity.java</b>. This file can include each of the fundamental lifecycle methods.</p> <pre class="prettyprint notranslate prettyprinted" > <span class="kwd" >package</span><span class="pln" > com</span><span class="pun" >.</span><span class="pln" >example</span><span class="pun" >.</span><span class="pln" >saira_000</span><span class="pun" >.</span><span class="pln" >myapplication</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >app</span><span class="pun" >.</span><span class="typ" >Activity</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >ClipData</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >content</span><span class="pun" >.</span><span class="typ" >ClipDescription</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >support</span><span class="pun" >.</span><span class="pln" >v7</span><span class="pun" >.</span><span class="pln" >app</span><span class="pun" >.</span><span class="typ" >ActionBarActivity</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >os</span><span class="pun" >.</span><span class="typ" >Bundle</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >util</span><span class="pun" >.</span><span class="typ" >Log</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >DragEvent</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >Menu</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >MenuItem</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >MotionEvent</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >view</span><span class="pun" >.</span><span class="typ" >View</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >ImageView</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >import</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >RelativeLayout</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >class</span><span class="pln" > </span><span class="typ" >MainActivity</span><span class="pln" > </span><span class="kwd" >extends</span><span class="pln" > </span><span class="typ" >Activity</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >ImageView</span><span class="pln" > img</span><span class="pun" >;</span><span class="pln" > </span><span class="typ" >String</span><span class="pln" > msg</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >private</span><span class="pln" > android</span><span class="pun" >.</span><span class="pln" >widget</span><span class="pun" >.</span><span class="typ" >RelativeLayout</span><span class="pun" >.</span><span class="typ" >LayoutParams</span><span class="pln" > layoutParams</span><span class="pun" >;</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >protected</span><span class="pln" > </span><span class="kwd" >void</span><span class="pln" > onCreate</span><span class="pun" >(</span><span class="typ" >Bundle</span><span class="pln" > savedInstanceState</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onCreate</span><span class="pun" >(</span><span class="pln" >savedInstanceState</span><span class="pun" >);</span><span class="pln" > setContentView</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >layout</span><span class="pun" >.</span><span class="pln" >activity_main</span><span class="pun" >);</span><span class="pln" > img</span><span class="pun" >=(</span><span class="typ" >ImageView</span><span class="pun" >)</span><span class="pln" >findViewById</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >imageView</span><span class="pun" >);</span><span class="pln" > img</span><span class="pun" >.</span><span class="pln" >setOnLongClickListener</span><span class="pun" >(</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >OnLongClickListener</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onLongClick</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > v</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >ClipData</span><span class="pun" >.</span><span class="typ" >Item</span><span class="pln" > item </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >ClipData</span><span class="pun" >.</span><span class="typ" >Item</span><span class="pun" >((</span><span class="typ" >CharSequence</span><span class="pun" >)</span><span class="pln" >v</span><span class="pun" >.</span><span class="pln" >getTag</span><span class="pun" >());</span><span class="pln" > </span><span class="typ" >String</span><span class="pun" >[]</span><span class="pln" > mimeTypes </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >{</span><span class="typ" >ClipDescription</span><span class="pun" >.</span><span class="pln" >MIMETYPE_TEXT_PLAIN</span><span class="pun" >};</span><span class="pln" > </span><span class="typ" >ClipData</span><span class="pln" > dragData </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >ClipData</span><span class="pun" >(</span><span class="pln" >v</span><span class="pun" >.</span><span class="pln" >getTag</span><span class="pun" >().</span><span class="pln" >toString</span><span class="pun" >(),</span><span class="pln" >mimeTypes</span><span class="pun" >,</span><span class="pln" > item</span><span class="pun" >);</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >DragShadowBuilder</span><span class="pln" > myShadow </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >DragShadowBuilder</span><span class="pun" >(</span><span class="pln" >img</span><span class="pun" >);</span><span class="pln" > v</span><span class="pun" >.</span><span class="pln" >startDrag</span><span class="pun" >(</span><span class="pln" >dragData</span><span class="pun" >,</span><span class="pln" >myShadow</span><span class="pun" >,</span><span class="kwd" >null</span><span class="pun" >,</span><span class="lit" >0</span><span class="pun" >);</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >});</span><span class="pln" > img</span><span class="pun" >.</span><span class="pln" >setOnDragListener</span><span class="pun" >(</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >OnDragListener</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onDrag</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > v</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >DragEvent</span><span class="pln" > </span><span class="kwd" >event</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >switch</span><span class="pun" >(</span><span class="kwd" >event</span><span class="pun" >.</span><span class="pln" >getAction</span><span class="pun" >())</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >case</span><span class="pln" > </span><span class="typ" >DragEvent</span><span class="pun" >.</span><span class="pln" >ACTION_DRAG_STARTED</span><span class="pun" >:</span><span class="pln" > layoutParams </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="typ" >RelativeLayout</span><span class="pun" >.</span><span class="typ" >LayoutParams</span><span class="pun" >)</span><span class="pln" >v</span><span class="pun" >.</span><span class="pln" >getLayoutParams</span><span class="pun" >();</span><span class="pln" > </span><span class="typ" >Log</span><span class="pun" >.</span><span class="pln" >d</span><span class="pun" >(</span><span class="pln" >msg</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >&quot;Action is DragEvent.ACTION_DRAG_STARTED&quot;</span><span class="pun" >);</span><span class="pln" > </span><span class="com" >// Do nothing</span><span class="pln" > </span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >case</span><span class="pln" > </span><span class="typ" >DragEvent</span><span class="pun" >.</span><span class="pln" >ACTION_DRAG_ENTERED</span><span class="pun" >:</span><span class="pln" > </span><span class="typ" >Log</span><span class="pun" >.</span><span class="pln" >d</span><span class="pun" >(</span><span class="pln" >msg</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >&quot;Action is DragEvent.ACTION_DRAG_ENTERED&quot;</span><span class="pun" >);</span><span class="pln" > </span><span class="kwd" >int</span><span class="pln" > x_cord </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="kwd" >int</span><span class="pun" >)</span><span class="pln" > </span><span class="kwd" >event</span><span class="pun" >.</span><span class="pln" >getX</span><span class="pun" >();</span><span class="pln" > </span><span class="kwd" >int</span><span class="pln" > y_cord </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="kwd" >int</span><span class="pun" >)</span><span class="pln" > </span><span class="kwd" >event</span><span class="pun" >.</span><span class="pln" >getY</span><span class="pun" >();</span><span class="pln" > </span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >case</span><span class="pln" > </span><span class="typ" >DragEvent</span><span class="pun" >.</span><span class="pln" >ACTION_DRAG_EXITED </span><span class="pun" >:</span><span class="pln" > </span><span class="typ" >Log</span><span class="pun" >.</span><span class="pln" >d</span><span class="pun" >(</span><span class="pln" >msg</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >&quot;Action is DragEvent.ACTION_DRAG_EXITED&quot;</span><span class="pun" >);</span><span class="pln" > x_cord </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="kwd" >int</span><span class="pun" >)</span><span class="pln" > </span><span class="kwd" >event</span><span class="pun" >.</span><span class="pln" >getX</span><span class="pun" >();</span><span class="pln" > y_cord </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="kwd" >int</span><span class="pun" >)</span><span class="pln" > </span><span class="kwd" >event</span><span class="pun" >.</span><span class="pln" >getY</span><span class="pun" >();</span><span class="pln" > layoutParams</span><span class="pun" >.</span><span class="pln" >leftMargin </span><span class="pun" >=</span><span class="pln" > x_cord</span><span class="pun" >;</span><span class="pln" > layoutParams</span><span class="pun" >.</span><span class="pln" >topMargin </span><span class="pun" >=</span><span class="pln" > y_cord</span><span class="pun" >;</span><span class="pln" > v</span><span class="pun" >.</span><span class="pln" >setLayoutParams</span><span class="pun" >(</span><span class="pln" >layoutParams</span><span class="pun" >);</span><span class="pln" > </span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >case</span><span class="pln" > </span><span class="typ" >DragEvent</span><span class="pun" >.</span><span class="pln" >ACTION_DRAG_LOCATION </span><span class="pun" >:</span><span class="pln" > </span><span class="typ" >Log</span><span class="pun" >.</span><span class="pln" >d</span><span class="pun" >(</span><span class="pln" >msg</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >&quot;Action is DragEvent.ACTION_DRAG_LOCATION&quot;</span><span class="pun" >);</span><span class="pln" > x_cord </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="kwd" >int</span><span class="pun" >)</span><span class="pln" > </span><span class="kwd" >event</span><span class="pun" >.</span><span class="pln" >getX</span><span class="pun" >();</span><span class="pln" > y_cord </span><span class="pun" >=</span><span class="pln" > </span><span class="pun" >(</span><span class="kwd" >int</span><span class="pun" >)</span><span class="pln" > </span><span class="kwd" >event</span><span class="pun" >.</span><span class="pln" >getY</span><span class="pun" >();</span><span class="pln" > </span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >case</span><span class="pln" > </span><span class="typ" >DragEvent</span><span class="pun" >.</span><span class="pln" >ACTION_DRAG_ENDED </span><span class="pun" >:</span><span class="pln" > </span><span class="typ" >Log</span><span class="pun" >.</span><span class="pln" >d</span><span class="pun" >(</span><span class="pln" >msg</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >&quot;Action is DragEvent.ACTION_DRAG_ENDED&quot;</span><span class="pun" >);</span><span class="pln" > </span><span class="com" >// Do nothing</span><span class="pln" > </span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >case</span><span class="pln" > </span><span class="typ" >DragEvent</span><span class="pun" >.</span><span class="pln" >ACTION_DROP</span><span class="pun" >:</span><span class="pln" > </span><span class="typ" >Log</span><span class="pun" >.</span><span class="pln" >d</span><span class="pun" >(</span><span class="pln" >msg</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >&quot;ACTION_DROP event&quot;</span><span class="pun" >);</span><span class="pln" > </span><span class="com" >// Do nothing</span><span class="pln" > </span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" > </span><span class="kwd" >default</span><span class="pun" >:</span><span class="pln" > </span><span class="kwd" >break</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >});</span><span class="pln" > img</span><span class="pun" >.</span><span class="pln" >setOnTouchListener</span><span class="pun" >(</span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >OnTouchListener</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onTouch</span><span class="pun" >(</span><span class="typ" >View</span><span class="pln" > v</span><span class="pun" >,</span><span class="pln" > </span><span class="typ" >MotionEvent</span><span class="pln" > </span><span class="kwd" >event</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >if</span><span class="pln" > </span><span class="pun" >(</span><span class="kwd" >event</span><span class="pun" >.</span><span class="pln" >getAction</span><span class="pun" >()</span><span class="pln" > </span><span class="pun" >==</span><span class="pln" > </span><span class="typ" >MotionEvent</span><span class="pun" >.</span><span class="pln" >ACTION_DOWN</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="typ" >ClipData</span><span class="pln" > data </span><span class="pun" >=</span><span class="pln" > </span><span class="typ" >ClipData</span><span class="pun" >.</span><span class="pln" >newPlainText</span><span class="pun" >(</span><span class="str" >&quot;&quot;</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >&quot;&quot;</span><span class="pun" >);</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >DragShadowBuilder</span><span class="pln" > shadowBuilder </span><span class="pun" >=</span><span class="pln" > </span><span class="kwd" >new</span><span class="pln" > </span><span class="typ" >View</span><span class="pun" >.</span><span class="typ" >DragShadowBuilder</span><span class="pun" >(</span><span class="pln" >img</span><span class="pun" >);</span><span class="pln" > img</span><span class="pun" >.</span><span class="pln" >startDrag</span><span class="pun" >(</span><span class="pln" >data</span><span class="pun" >,</span><span class="pln" > shadowBuilder</span><span class="pun" >,</span><span class="pln" > img</span><span class="pun" >,</span><span class="pln" > </span><span class="lit" >0</span><span class="pun" >);</span><span class="pln" > img</span><span class="pun" >.</span><span class="pln" >setVisibility</span><span class="pun" >(</span><span class="typ" >View</span><span class="pun" >.</span><span class="pln" >INVISIBLE</span><span class="pun" >);</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="kwd" >else</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >false</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >});</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onCreateOptionsMenu</span><span class="pun" >(</span><span class="typ" >Menu</span><span class="pln" > menu</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="com" >// Inflate the menu; this adds items to the action bar if it is present.</span><span class="pln" > getMenuInflater</span><span class="pun" >().</span><span class="pln" >inflate</span><span class="pun" >(</span><span class="pln" >R</span><span class="pun" >.</span><span class="pln" >menu</span><span class="pun" >.</span><span class="pln" >menu_main</span><span class="pun" >,</span><span class="pln" > menu</span><span class="pun" >);</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="lit" >@Override</span><span class="pln" > </span><span class="kwd" >public</span><span class="pln" > </span><span class="kwd" >boolean</span><span class="pln" > onOptionsItemSelected</span><span class="pun" >(</span><span class="typ" >MenuItem</span><span class="pln" > item</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="com" >// Handle action bar item clicks here. The action bar will</span><span class="pln" > </span><span class="com" >// automatically handle clicks on the Home/Up button, so long</span><span class="pln" > </span><span class="com" >// as you specify a parent activity in AndroidManifest.xml.</span><span class="pln" > </span><span class="kwd" >int</span><span class="pln" > id </span><span class="pun" >=</span><span class="pln" > item</span><span class="pun" >.</span><span class="pln" >getItemId</span><span class="pun" >();</span><span class="pln" > </span><span class="com" >//noinspection SimplifiableIfStatement</span><span class="pln" > </span><span class="kwd" >if</span><span class="pln" > </span><span class="pun" >(</span><span class="pln" >id </span><span class="pun" >==</span><span class="pln" > R</span><span class="pun" >.</span><span class="pln" >id</span><span class="pun" >.</span><span class="pln" >action_settings</span><span class="pun" >)</span><span class="pln" > </span><span class="pun" >{</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >true</span><span class="pun" >;</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="kwd" >return</span><span class="pln" > </span><span class="kwd" >super</span><span class="pun" >.</span><span class="pln" >onOptionsItemSelected</span><span class="pun" >(</span><span class="pln" >item</span><span class="pun" >);</span><span class="pln" > </span><span class="pun" >}</span><span class="pln" > </span><span class="pun" >}</span></pre> <p >Following will be the content of<b >res/layout/activity_main.xml</b>file &minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="tag" >&lt;RelativeLayout</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/apk/res/android&quot;</span><span class="pln" > </span><span class="atn" >xmlns:tools</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/tools&quot;</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;match_parent&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;match_parent&quot;</span><span class="pln" > </span><span class="atn" >android:paddingLeft</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_horizontal_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingRight</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_horizontal_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingTop</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_vertical_margin&quot;</span><span class="pln" > </span><span class="atn" >android:paddingBottom</span><span class="pun" >=</span><span class="atv" >&quot;@dimen/activity_vertical_margin&quot;</span><span class="pln" > </span><span class="atn" >tools:context</span><span class="pun" >=</span><span class="atv" >&quot;.MainActivity&quot;</span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;TextView</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Drag and Drop Example&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignParentTop</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >&quot;30dp&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;TextView</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >&quot;Tutorials Point&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView2&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView&quot;</span><span class="pln" > </span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >&quot;30dp&quot;</span><span class="pln" > </span><span class="atn" >android:textColor</span><span class="pun" >=</span><span class="atv" >&quot;#ff14be3c&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" >&gt; </span><span class="tag" >&lt;ImageView</span><span class="pln" > </span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >&quot;wrap_content&quot;</span><span class="pln" > </span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >&quot;@+id/imageView&quot;</span><span class="pln" > </span><span class="atn" >android:src</span><span class="pun" >=</span><span class="atv" >&quot;@drawable/abc&quot;</span><span class="pln" > </span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView2&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignRight</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView2&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignEnd</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView2&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignLeft</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView2&quot;</span><span class="pln" > </span><span class="atn" >android:layout_alignStart</span><span class="pun" >=</span><span class="atv" >&quot;@+id/textView2&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;/RelativeLayout&gt;</span></pre> <p >Following will be the content of<b >res/values/strings.xml</b>to define two new constants &minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="pun" >&lt;?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >&quot;1.0&quot;</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >&quot;utf-8&quot;</span><span class="pun" >?&gt;</span><span class="pln" > </span><span class="tag" >&lt;resources&gt;</span><span class="pln" > </span><span class="tag" >&lt;string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >&quot;app_name&quot;</span><span class="tag" >&gt;</span><span class="pln" >My Application</span><span class="tag" >&lt;/string&gt;</span><span class="pln" > </span><span class="tag" >&lt;string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >&quot;action_settings&quot;</span><span class="tag" >&gt;</span><span class="pln" >Settings</span><span class="tag" >&lt;/string&gt;</span><span class="pln" > </span><span class="tag" >&lt;/resources&gt;</span></pre> <p >Following is the default content of<b >AndroidManifest.xml</b>&minus;</p> <pre class="prettyprint notranslate prettyprinted" > <span class="pun" >&lt;?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >&quot;1.0&quot;</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >&quot;utf-8&quot;</span><span class="pun" >?&gt;</span><span class="pln" > </span><span class="tag" >&lt;manifest</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >&quot;http://schemas.android.com/apk/res/android&quot;</span><span class="pln" > </span><span class="atn" >package</span><span class="pun" >=</span><span class="atv" >&quot;com.example.guidemo&quot;</span><span class="pln" > </span><span class="atn" >android:versionCode</span><span class="pun" >=</span><span class="atv" >&quot;1&quot;</span><span class="pln" > </span><span class="atn" >android:versionName</span><span class="pun" >=</span><span class="atv" >&quot;1.0&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;uses-sdk</span><span class="pln" > </span><span class="atn" >android:minSdkVersion</span><span class="pun" >=</span><span class="atv" >&quot;11&quot;</span><span class="pln" > </span><span class="atn" >android:targetSdkVersion</span><span class="pun" >=</span><span class="atv" >&quot;22&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;application</span><span class="pln" > </span><span class="atn" >android:allowBackup</span><span class="pun" >=</span><span class="atv" >&quot;true&quot;</span><span class="pln" > </span><span class="atn" >android:icon</span><span class="pun" >=</span><span class="atv" >&quot;@drawable/ic_launcher&quot;</span><span class="pln" > </span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >&quot;@string/app_name&quot;</span><span class="pln" > </span><span class="atn" >android:theme</span><span class="pun" >=</span><span class="atv" >&quot;@style/AppTheme&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;activity</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;com.example.guidemo.MainActivity&quot;</span><span class="pln" > </span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >&quot;@string/app_name&quot;</span><span class="pln" > </span><span class="tag" >&gt;</span><span class="pln" > </span><span class="tag" >&lt;intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;action</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;android.intent.action.MAIN&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;category</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >&quot;android.intent.category.LAUNCHER&quot;</span><span class="pln" > </span><span class="tag" >/&gt;</span><span class="pln" > </span><span class="tag" >&lt;/intent-filter&gt;</span><span class="pln" > </span><span class="tag" >&lt;/activity&gt;</span><span class="pln" > </span><span class="tag" >&lt;/application&gt;</span><span class="pln" > </span><span class="tag" >&lt;/manifest&gt;</span></pre> <p >Let&#39;s try to run your<b >My Application</b>application. I assume you had created your<b >AVD</b>while doing environment setup. To run the app from Android Studio, open one of your project&#39;s activity files and click Run<img alt="Eclipse Run Icon" class="inline" src="http://www.tutorialspoint.com/android/images/eclipse_run.jpg" />icon from the toolbar. Android studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window &minus;</p> <p><img alt="Android Drag and Drop" src="http://www.tutorialspoint.com/android/images/drag.jpg" /></p> <p >Now do long click on the displayed TutorialsPoint logo and you will see that logo image moves a little after 1 seconds long click from its place, its the time when you should start dragging the image. You can drag it around the screen and drop it at a new location.</p>
RangeTime:0.006239s
RangeMem:251.53 KB
返回顶部 留言