<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 & drop functionality −</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>− 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>− 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'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>− The user releases the dragged item within the bounding box of a View. The system sends the View object's listener a drag event with action type ACTION_DROP.</p>
</li>
<li >
<p ><b >Ended</b>− 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 & 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'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 & 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'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 & 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" >"Action is DragEvent.ACTION_DRAG_STARTED"</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" >"Action is DragEvent.ACTION_DRAG_ENTERED"</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" >"Action is DragEvent.ACTION_DRAG_EXITED"</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" >"Action is DragEvent.ACTION_DRAG_LOCATION"</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" >"Action is DragEvent.ACTION_DRAG_ENDED"</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" >"ACTION_DROP event"</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" >""</span><span class="pun" >,</span><span class="pln" > </span><span class="str" >""</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 −</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="tag" ><RelativeLayout</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >"http://schemas.android.com/apk/res/android"</span><span class="pln" >
</span><span class="atn" >xmlns:tools</span><span class="pun" >=</span><span class="atv" >"http://schemas.android.com/tools"</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"match_parent"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"match_parent"</span><span class="pln" >
</span><span class="atn" >android:paddingLeft</span><span class="pun" >=</span><span class="atv" >"@dimen/activity_horizontal_margin"</span><span class="pln" >
</span><span class="atn" >android:paddingRight</span><span class="pun" >=</span><span class="atv" >"@dimen/activity_horizontal_margin"</span><span class="pln" >
</span><span class="atn" >android:paddingTop</span><span class="pun" >=</span><span class="atv" >"@dimen/activity_vertical_margin"</span><span class="pln" >
</span><span class="atn" >android:paddingBottom</span><span class="pun" >=</span><span class="atv" >"@dimen/activity_vertical_margin"</span><span class="pln" >
</span><span class="atn" >tools:context</span><span class="pun" >=</span><span class="atv" >".MainActivity"</span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><TextView</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"Drag and Drop Example"</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/textView"</span><span class="pln" >
</span><span class="atn" >android:layout_alignParentTop</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >"30dp"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><TextView</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:text</span><span class="pun" >=</span><span class="atv" >"Tutorials Point"</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/textView2"</span><span class="pln" >
</span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >"@+id/textView"</span><span class="pln" >
</span><span class="atn" >android:layout_centerHorizontal</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:textSize</span><span class="pun" >=</span><span class="atv" >"30dp"</span><span class="pln" >
</span><span class="atn" >android:textColor</span><span class="pun" >=</span><span class="atv" >"#ff14be3c"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >>
</span><span class="tag" ><ImageView</span><span class="pln" >
</span><span class="atn" >android:layout_width</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:layout_height</span><span class="pun" >=</span><span class="atv" >"wrap_content"</span><span class="pln" >
</span><span class="atn" >android:id</span><span class="pun" >=</span><span class="atv" >"@+id/imageView"</span><span class="pln" >
</span><span class="atn" >android:src</span><span class="pun" >=</span><span class="atv" >"@drawable/abc"</span><span class="pln" >
</span><span class="atn" >android:layout_below</span><span class="pun" >=</span><span class="atv" >"@+id/textView2"</span><span class="pln" >
</span><span class="atn" >android:layout_alignRight</span><span class="pun" >=</span><span class="atv" >"@+id/textView2"</span><span class="pln" >
</span><span class="atn" >android:layout_alignEnd</span><span class="pun" >=</span><span class="atv" >"@+id/textView2"</span><span class="pln" >
</span><span class="atn" >android:layout_alignLeft</span><span class="pun" >=</span><span class="atv" >"@+id/textView2"</span><span class="pln" >
</span><span class="atn" >android:layout_alignStart</span><span class="pun" >=</span><span class="atv" >"@+id/textView2"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ></RelativeLayout></span></pre>
<p >Following will be the content of<b >res/values/strings.xml</b>to define two new constants −</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="pun" ><?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >"1.0"</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >"utf-8"</span><span class="pun" >?></span><span class="pln" >
</span><span class="tag" ><resources></span><span class="pln" >
</span><span class="tag" ><string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >"app_name"</span><span class="tag" >></span><span class="pln" >My Application</span><span class="tag" ></string></span><span class="pln" >
</span><span class="tag" ><string</span><span class="pln" > </span><span class="atn" >name</span><span class="pun" >=</span><span class="atv" >"action_settings"</span><span class="tag" >></span><span class="pln" >Settings</span><span class="tag" ></string></span><span class="pln" >
</span><span class="tag" ></resources></span></pre>
<p >Following is the default content of<b >AndroidManifest.xml</b>−</p>
<pre class="prettyprint notranslate prettyprinted" >
<span class="pun" ><?</span><span class="pln" >xml version</span><span class="pun" >=</span><span class="str" >"1.0"</span><span class="pln" > encoding</span><span class="pun" >=</span><span class="str" >"utf-8"</span><span class="pun" >?></span><span class="pln" >
</span><span class="tag" ><manifest</span><span class="pln" > </span><span class="atn" >xmlns:android</span><span class="pun" >=</span><span class="atv" >"http://schemas.android.com/apk/res/android"</span><span class="pln" >
</span><span class="atn" >package</span><span class="pun" >=</span><span class="atv" >"com.example.guidemo"</span><span class="pln" >
</span><span class="atn" >android:versionCode</span><span class="pun" >=</span><span class="atv" >"1"</span><span class="pln" >
</span><span class="atn" >android:versionName</span><span class="pun" >=</span><span class="atv" >"1.0"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><uses-sdk</span><span class="pln" >
</span><span class="atn" >android:minSdkVersion</span><span class="pun" >=</span><span class="atv" >"11"</span><span class="pln" >
</span><span class="atn" >android:targetSdkVersion</span><span class="pun" >=</span><span class="atv" >"22"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><application</span><span class="pln" >
</span><span class="atn" >android:allowBackup</span><span class="pun" >=</span><span class="atv" >"true"</span><span class="pln" >
</span><span class="atn" >android:icon</span><span class="pun" >=</span><span class="atv" >"@drawable/ic_launcher"</span><span class="pln" >
</span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >"@string/app_name"</span><span class="pln" >
</span><span class="atn" >android:theme</span><span class="pun" >=</span><span class="atv" >"@style/AppTheme"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><activity</span><span class="pln" >
</span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"com.example.guidemo.MainActivity"</span><span class="pln" >
</span><span class="atn" >android:label</span><span class="pun" >=</span><span class="atv" >"@string/app_name"</span><span class="pln" > </span><span class="tag" >></span><span class="pln" >
</span><span class="tag" ><intent-filter></span><span class="pln" >
</span><span class="tag" ><action</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"android.intent.action.MAIN"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ><category</span><span class="pln" > </span><span class="atn" >android:name</span><span class="pun" >=</span><span class="atv" >"android.intent.category.LAUNCHER"</span><span class="pln" > </span><span class="tag" >/></span><span class="pln" >
</span><span class="tag" ></intent-filter></span><span class="pln" >
</span><span class="tag" ></activity></span><span class="pln" >
</span><span class="tag" ></application></span><span class="pln" >
</span><span class="tag" ></manifest></span></pre>
<p >Let'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'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 −</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>