Site icon C1CTech

Android Drag and Drop Example

&NewLine;<p>This tutorial is about Android drag and drop feature and how to use it in an Android application with a simple example&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class&equals;"wp-block-embed&lowbar;&lowbar;wrapper">&NewLine;<span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"vD0QsyHfVlE" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;vD0QsyHfVlE" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;vD0QsyHfVlE&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;vD0QsyHfVlE&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span>&NewLine;<&sol;div><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-buttons aligncenter is-layout-flex wp-block-buttons-is-layout-flex">&NewLine;<div class&equals;"wp-block-button"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;DragAndDropDemo" style&equals;"background-color&colon;&num;520599" target&equals;"&lowbar;blank" rel&equals;"noreferrer noopener"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<p>Android <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Drag and Drop<&sol;strong><&sol;span> framework&comma; allows you to move data from one View to another using a graphical drag and drop gesture&period; Although the framework is primarily designed for data movement&comma; you can use it for other UI actions&period; For example&comma; you could create an app that mixes colors when the user drags a color icon over another icon&period;&nbsp&semi;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Android drag&sol;drop process<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>In android&comma; the Drag and Drop process contains 4 steps or states&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Started<&sol;strong><&sol;span><&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Continuing<&sol;strong><&sol;span><&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Dropped<&sol;strong><&sol;span><&sol;li><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Ended<&sol;strong><&sol;span><&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Started<&sol;strong><&sol;span><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p>In response to the user&&num;8217&semi;s gesture <strong>&lpar;for example&comma; on a long press&rpar;<&sol;strong> to begin a drag&comma; your application calls <span style&equals;"color&colon; &num;0000ff&semi;"><strong><span style&equals;"color&colon; &num;008000&semi;">startDrag&lpar;&rpar;<&sol;span><&sol;strong><&sol;span>&nbsp&semi;to tell the system to start a drag&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>The&nbsp&semi;<span style&equals;"color&colon; &num;000080&semi;"><strong>startDrag&lpar;&rpar;<&sol;strong>&nbsp&semi;<&sol;span>method arguments will provide data to be dragged&comma; metadata for this data and a call-back for drawing the drag shadow&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>After that&comma; the system sends a drag event with action type&nbsp&semi;<span style&equals;"color&colon; &num;000080&semi;"><strong>ACTION&lowbar;DRAG&lowbar;STARTED<&sol;strong><&sol;span>&nbsp&semi;to the drag event listeners for all the View objects in the current layout&period;&nbsp&semi;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Continuing<&sol;strong><&sol;span><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p>The user continues the drag&comma; and the system now sends <span style&equals;"color&colon; &num;000080&semi;"><strong>ACTION&lowbar;DRAG&lowbar;ENTERED<&sol;strong><&sol;span>&nbsp&semi;action followed by&nbsp&semi;<span style&equals;"color&colon; &num;000080&semi;"><strong>ACTION&lowbar;DRAG&lowbar;LOCATION<&sol;strong><&sol;span> action to the registered drag event listener for the View&period; <span style&equals;"font-size&colon; inherit&semi;">The listener may choose to alter its View object’s appearance in response to the event or can react by highlighting its View&period;<&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>The drag event listener receives a&nbsp&semi;<span style&equals;"color&colon; &num;000080&semi;"><strong>ACTION&lowbar;DRAG&lowbar;EXITED<&sol;strong><&sol;span>&nbsp&semi;action after the user has moved the drag shadow outside the bounding box of the View&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Dropped<&sol;strong><&sol;span><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p>User drops the dragged item&comma; and the system sends the <strong><span style&equals;"color&colon; &num;000080&semi;">ACTION&lowbar;DROP<&sol;span> <&sol;strong>event only if the user drops the drag shadow within the bounding box of a View which listener is registered to receive drag events&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>For example&colon;<&sol;strong><&sol;span> If there are two views <strong>View1<&sol;strong> and <strong>View2<&sol;strong> and <strong>View1<&sol;strong> is registered for dragListener but <strong>view2<&sol;strong> is not&comma; then if user drops the item in <strong>View1<&sol;strong> then the drop action will be called for <strong>View1<&sol;strong> but if user drops the item in <strong>view2<&sol;strong> then no drop action will be called for <strong>View2<&sol;strong>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Ended<&sol;strong><&sol;span><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p>The drag-and-drop operation has ended&comma; and the system sends out a drag event with action type <span style&equals;"color&colon; &num;000080&semi;"><strong>ACTION&lowbar;DRAG&lowbar;ENDED<&sol;strong><&sol;span>&period; All of the views that were listening for the drag-and-drop events now get the&nbsp&semi;<strong><span style&equals;"color&colon; &num;000080&semi;">ACTION&lowbar;DRAG&lowbar;ENDED<&sol;span><&sol;strong> event&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong><span class&equals;"has-inline-color">OnDragListener<&sol;span><&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>To receive the drag-and-drop events&comma; we need to register a&nbsp&semi;<strong><span style&equals;"color&colon; &num;008000&semi;">View&period;OnDragListener<&sol;span>&nbsp&semi;<&sol;strong>on the View using <span style&equals;"color&colon; &num;008000&semi;"><strong>setOnDragListener&lpar;&rpar;<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>The&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>View&period;OnDragListener<&sol;strong><&sol;span>&nbsp&semi;having a callback method&comma;<strong><span style&equals;"color&colon; &num;008000&semi;"> onDrag&lpar;View v&comma; DragEvent event&rpar;<&sol;span>&period;<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>v<&sol;strong><&sol;span>&colon; the <span style&equals;"color&colon; &num;008000&semi;"><strong>view<&sol;strong><&sol;span> that received the event&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>event<&sol;strong><&sol;span>&colon; <span style&equals;"color&colon; &num;008000&semi;"><strong>DragEvent<&sol;strong><&sol;span>&nbsp&semi;object&comma; which contains all the information about the drag-and-drop event&comma; including the location of the event&comma; the drag action &lpar;state&rpar;&comma; and the data it carries&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Drag events<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>When&nbsp&semi;<span style&equals;"color&colon; &num;000080&semi;"><strong>startDrag&lpar;&rpar; <&sol;strong><&sol;span>function is called&comma; then the system identifies the type of drag event that is currently taking place and return the drag event to the application&period; It sends out a drag event in the form of a&nbsp&semi;<span style&equals;"color&colon; &num;000080&semi;"><strong>DragEvent <&sol;strong><&sol;span>object&period;&nbsp&semi;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">getAction&lpar;&rpar; <&sol;span><&sol;strong>is used to identify which type of drag event is taking place&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>The DragEvent&nbsp&semi;object contains the following different action types&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ol class&equals;"wp-block-list"><li><strong><span style&equals;"color&colon; &num;0000ff&semi;">ACTION&lowbar;DRAG&lowbar;STARTED&colon;<&sol;span>&nbsp&semi;<&sol;strong>receives this event action type just after the application calls&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>startDrag&lpar;&rpar;<&sol;strong><&sol;span>&nbsp&semi;and gets a drag shadow&period;<&sol;li><li><strong><span style&equals;"color&colon; &num;0000ff&semi;">ACTION&lowbar;DRAG&lowbar;ENTERED<&sol;span>&colon;<&sol;strong>&nbsp&semi;receives this event action type when the drag shadow has just entered the bounding box of the View&period;<&sol;li><li><strong><span style&equals;"color&colon; &num;0000ff&semi;">ACTION&lowbar;DRAG&lowbar;LOCATION<&sol;span>&colon;&nbsp&semi;<&sol;strong>receives this event action type after it receives a <span style&equals;"color&colon; &num;008000&semi;"><strong>ACTION&lowbar;DRAG&lowbar;ENTERED<&sol;strong><&sol;span> event&comma; when the drag is taking place i&period;e&period; the user is dragging the item from one place to other&period;<&sol;li><li><strong><span style&equals;"color&colon; &num;0000ff&semi;">ACTION&lowbar;DRAG&lowbar;EXITED<&sol;span>&colon;&nbsp&semi;<&sol;strong> receives this event action type after it receives a&nbsp&semi;<strong><span style&equals;"color&colon; &num;008000&semi;">ACTION&lowbar;DRAG&lowbar;ENTERED<&sol;span><&sol;strong>&nbsp&semi;and at least one&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>ACTION&lowbar;DRAG&lowbar;LOCATION<&sol;strong><&sol;span>&nbsp&semi;event&comma; and after the user has moved the drag shadow outside the bounding box of the View&period;<&sol;li><li><strong><span style&equals;"color&colon; &num;0000ff&semi;">ACTION&lowbar;DROP<&sol;span>&colon;<&sol;strong>&nbsp&semi; receives this event action type when the user releases the drag shadow over the View object&period; This action type is not sent if the user releases the drag shadow on a View whose listener is not registered&period; The listener is expected to return boolean <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong> if it successfully processes the drop&period; Otherwise&comma; it should return <span style&equals;"color&colon; &num;008000&semi;"><strong>false<&sol;strong><&sol;span>&period;<&sol;li><li><strong><span style&equals;"color&colon; &num;0000ff&semi;">ACTION&lowbar;DRAG&lowbar;ENDED<&sol;span>&colon;<&sol;strong> receives this event action type when the system is ending the drag operation&period; This action type is not necessarily preceded by an&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>ACTION&lowbar;DROP<&sol;strong><&sol;span>&nbsp&semi;event&period; The listener must call&nbsp&semi;<span style&equals;"color&colon; &num;000080&semi;"><strong>getResult&lpar;&rpar;<&sol;strong><&sol;span>&nbsp&semi;to get the value that was returned in response to&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>ACTION&lowbar;DROP<&sol;strong><&sol;span>&period; If an&nbsp&semi;<strong>ACTION&lowbar;DROP<&sol;strong>&nbsp&semi;event was not sent&comma; then&nbsp&semi;<strong>getResult&lpar;&rpar;<&sol;strong>&nbsp&semi;returns&nbsp&semi;<strong><span style&equals;"color&colon; &num;008000&semi;">false<&sol;span><&sol;strong>&period;<&sol;li><&sol;ol>&NewLine;&NewLine;&NewLine;&NewLine;<p>let’s implement the drag and drop feature in our app&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Android Drag and Drop Example<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>In this example&comma; we will drag and drop text view and button from one place to another in a bounding area and outside the bounding area &lpar;in another view&rpar;&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>1 &period; Open the layout file <span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span> and add the below code&period; It consist of two LinearLayouts &lpar;with equal weight&rpar; and the upper LinearLayout consist of a textview and a button&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em>&lt&semi;&quest;<&sol;em><strong>xml version&equals;"1&period;0" encoding&equals;"utf-8"<&sol;strong><em>&quest;&gt&semi;&NewLine;<&sol;em>&lt&semi;<strong>LinearLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;orientation&equals;"vertical"<&sol;strong>&gt&semi;&NewLine;&NewLine; &lt&semi;<strong>LinearLayout&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;top&lowbar;layout"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;weight&equals;"1"&NewLine; android&colon;background&equals;"&commat;color&sol;colorPrimary"&NewLine; android&colon;gravity&equals;"center"&NewLine; android&colon;orientation&equals;"vertical"<&sol;strong>&gt&semi;&NewLine;&NewLine; &lt&semi;<strong>Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;draggable"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;text&equals;"&commat;string&sol;btn&lowbar;text" <&sol;strong>&sol;&gt&semi;&NewLine;&NewLine; &lt&semi;<strong>TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;draggable"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;gravity&equals;"center"&NewLine; android&colon;layout&lowbar;marginTop&equals;"&commat;dimen&sol;margin&lowbar;10dp"&NewLine; android&colon;text&equals;"&commat;string&sol;tv&lowbar;text"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white"&NewLine; android&colon;textSize&equals;"20sp"&NewLine; android&colon;textStyle&equals;"bold" <&sol;strong>&sol;&gt&semi;&NewLine; &lt&semi;&sol;<strong>LinearLayout<&sol;strong>&gt&semi;&NewLine;&NewLine; &lt&semi;<strong>LinearLayout&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;bottom&lowbar;layout"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;weight&equals;"1"&NewLine; android&colon;gravity&equals;"center"&NewLine; android&colon;background&equals;"&commat;color&sol;colorAccent"&NewLine; android&colon;orientation&equals;"vertical" <&sol;strong>&sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;<strong>LinearLayout<&sol;strong>&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>2 &period; Open <strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;java<&sol;span><&sol;strong> and add the below code&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <&sol;strong>com&period;c1ctech&period;draganddropdemo&semi;&NewLine;&NewLine;<strong>import <&sol;strong>android&period;content&period;ClipData&semi;&NewLine;<strong>import <&sol;strong>android&period;content&period;ClipDescription&semi;&NewLine;<strong>import <&sol;strong>android&period;graphics&period;Color&semi;&NewLine;<strong>import <&sol;strong>android&period;graphics&period;PorterDuff&semi;&NewLine;<strong>import <&sol;strong>android&period;os&period;Bundle&semi;&NewLine;<strong>import <&sol;strong>android&period;util&period;Log&semi;&NewLine;<strong>import <&sol;strong>android&period;view&period;DragEvent&semi;&NewLine;<strong>import <&sol;strong>android&period;view&period;View&semi;&NewLine;<strong>import <&sol;strong>android&period;view&period;ViewGroup&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;Button&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;LinearLayout&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;TextView&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;Toast&semi;&NewLine;&NewLine;<strong>import <&sol;strong>androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;&NewLine;<strong>public class <&sol;strong>MainActivity <strong>extends <&sol;strong>AppCompatActivity <strong>implements <&sol;strong>View&period;OnLongClickListener&comma; View&period;OnDragListener &lbrace;&NewLine;&NewLine; TextView <strong>textView<&sol;strong>&semi;&NewLine; Button <strong>button<&sol;strong>&semi;&NewLine;&NewLine; <em><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; Create a string for the TextView and Button label<&sol;strong><&sol;span>&NewLine;<&sol;em> <strong>private static final <&sol;strong>String <strong><em>TEXTVIEW&lowbar;TAG <&sol;em><&sol;strong>&equals; <strong>"DRAG TEXT"<&sol;strong>&semi;&NewLine; <strong>private static final <&sol;strong>String <strong><em>BUTTON&lowbar;TAG <&sol;em><&sol;strong>&equals; <strong>"DRAG BUTTON"<&sol;strong>&semi;&NewLine;&NewLine; &commat;Override&NewLine; <strong>protected void <&sol;strong>onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; <strong>super<&sol;strong>&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;<strong><em>activity&lowbar;main<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;find views by id<&sol;span><&sol;strong>&NewLine;<&sol;em> <strong>textView <&sol;strong>&equals; findViewById&lpar;R&period;id&period;<strong><em>tv&lowbar;draggable<&sol;em><&sol;strong>&rpar;&semi;&NewLine; <strong>button <&sol;strong>&equals; findViewById&lpar;R&period;id&period;<strong><em>btn&lowbar;draggable<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Sets the tag<&sol;span><&sol;strong>&NewLine;<&sol;em> <strong>textView<&sol;strong>&period;setTag&lpar;<strong><em>TEXTVIEW&lowbar;TAG<&sol;em><&sol;strong>&rpar;&semi;&NewLine; <strong>button<&sol;strong>&period;setTag&lpar;<strong><em>BUTTON&lowbar;TAG<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; implementEvents&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <em><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Implement long click and drag listener<&sol;strong><&sol;span>&NewLine;<&sol;em> <strong>private void <&sol;strong>implementEvents&lpar;&rpar; &lbrace;&NewLine;&NewLine; <strong>textView<&sol;strong>&period;setOnLongClickListener&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine; <strong>button<&sol;strong>&period;setOnLongClickListener&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine;&NewLine; findViewById&lpar;R&period;id&period;<strong><em>top&lowbar;layout<&sol;em><&sol;strong>&rpar;&period;setOnDragListener&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine; findViewById&lpar;R&period;id&period;<strong><em>bottom&lowbar;layout<&sol;em><&sol;strong>&rpar;&period;setOnDragListener&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> <em>&sol;&sol; This is the method that the system calls when it dispatches a drag event to the&NewLine;<&sol;em><&sol;span><&sol;strong><em><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; listener&period;<&sol;span><&sol;strong>&NewLine;<&sol;em> &commat;Override&NewLine; <strong>public boolean <&sol;strong>onDrag&lpar;View view&comma; DragEvent dragEvent&rpar; &lbrace;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Defines a variable to store the action type for the incoming event<&sol;span><&sol;strong>&NewLine;<&sol;em> <strong>int <&sol;strong>action &equals; dragEvent&period;getAction&lpar;&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Handles each of the expected events<&sol;span><&sol;strong>&NewLine;<&sol;em> <strong>switch <&sol;strong>&lpar;action&rpar; &lbrace;&NewLine; <strong>case <&sol;strong>DragEvent&period;<strong><em>ACTION&lowbar;DRAG&lowbar;STARTED<&sol;em><&sol;strong>&colon;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Determines if this View can accept the dragged data<&sol;span><&sol;strong>&NewLine;<&sol;em> <strong>if <&sol;strong>&lpar;dragEvent&period;getClipDescription&lpar;&rpar;&period;hasMimeType&lpar;ClipDescription&period;<strong><em>MIMETYPE&lowbar;TEXT&lowbar;PLAIN<&sol;em><&sol;strong>&rpar;&rpar; &lbrace;&NewLine; <em>&sol;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol; if you want to apply color when drag started to your view you can uncomment below lines<&sol;span><&sol;strong>&NewLine;<&sol;em><strong><span style&equals;"color&colon; &num;008000&semi;"><em> &sol;&sol; to give any color tint to the View to indicate that it can accept&NewLine;<&sol;em><&sol;span><&sol;strong><em><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; data&period;<&sol;span><&sol;strong>&NewLine;<&sol;em><strong><span style&equals;"color&colon; &num;0000ff&semi;"><em> &sol;&sol;view&period;getBackground&lpar;&rpar;&period;setColorFilter&lpar;Color&period;BLUE&comma; PorterDuff&period;Mode&period;SRC&lowbar;IN&rpar;&semi;&NewLine;<&sol;em><&sol;span><&sol;strong><em>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; Invalidate the view to force a redraw in the new tint<&sol;strong><&sol;span>&NewLine;<&sol;em><strong><span style&equals;"color&colon; &num;0000ff&semi;"><em> &sol;&sol; view&period;invalidate&lpar;&rpar;&semi;&NewLine;<&sol;em><&sol;span><&sol;strong><em>&NewLine;<&sol;em><strong><span style&equals;"color&colon; &num;008000&semi;"><em> &sol;&sol; returns true to indicate that the View can accept the dragged data&period;&NewLine;<&sol;em><&sol;span> return true<&sol;strong>&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"><em>&sol;&sol; Returns false&period; During the current drag and drop operation&comma; this View will&NewLine;<&sol;em><&sol;span><&sol;strong><em><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; not receive events again until ACTION&lowbar;DRAG&lowbar;ENDED is sent&period;<&sol;span><&sol;strong>&NewLine;<&sol;em> <strong>return false<&sol;strong>&semi;&NewLine;&NewLine; <strong>case <&sol;strong>DragEvent&period;<strong><em>ACTION&lowbar;DRAG&lowbar;ENTERED<&sol;em><&sol;strong>&colon;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong><em>&sol;&sol; Applies a MAGENTA or any color tint to the View&comma;&NewLine; &sol;&sol; Return true&semi; the return value is ignored&period;&NewLine;<&sol;em><&sol;strong><&sol;span><em>&NewLine;<&sol;em> view&period;getBackground&lpar;&rpar;&period;setColorFilter&lpar;Color&period;<strong><em>MAGENTA<&sol;em><&sol;strong>&comma; PorterDuff&period;Mode&period;<strong><em>SRC&lowbar;IN<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Invalidate the view to force a redraw in the new tint<&sol;span><&sol;strong>&NewLine;<&sol;em> view&period;invalidate&lpar;&rpar;&semi;&NewLine;&NewLine; <strong>return true<&sol;strong>&semi;&NewLine;&NewLine; <strong>case <&sol;strong>DragEvent&period;<strong><em>ACTION&lowbar;DRAG&lowbar;LOCATION<&sol;em><&sol;strong>&colon;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Ignore the event<&sol;span><&sol;strong>&NewLine;<&sol;em> <strong>return true<&sol;strong>&semi;&NewLine;&NewLine; <strong>case <&sol;strong>DragEvent&period;<strong><em>ACTION&lowbar;DRAG&lowbar;EXITED<&sol;em><&sol;strong>&colon;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> <em>&sol;&sol; Re-sets the color tint to blue&comma; if you had set the BLUE color or any color in ACTION&lowbar;DRAG&lowbar;STARTED&period; Returns true&semi; the return value is ignored&period;&NewLine;<&sol;em><&sol;strong><&sol;span><em>&NewLine;<&sol;em><span style&equals;"color&colon; &num;0000ff&semi;"><strong><em> &sol;&sol; view&period;getBackground&lpar;&rpar;&period;setColorFilter&lpar;Color&period;BLUE&comma; PorterDuff&period;Mode&period;SRC&lowbar;IN&rpar;&semi;&NewLine;<&sol;em><&sol;strong><&sol;span><em>&NewLine;<&sol;em><span style&equals;"color&colon; &num;008000&semi;"><strong><em> &sol;&sol;If u had not provided any color in ACTION&lowbar;DRAG&lowbar;STARTED then clear color filter&period;&NewLine;<&sol;em><&sol;strong><&sol;span> view&period;getBackground&lpar;&rpar;&period;clearColorFilter&lpar;&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> <em>&sol;&sol; Invalidate the view to force a redraw in the new tint&NewLine;<&sol;em><&sol;span><&sol;strong> view&period;invalidate&lpar;&rpar;&semi;&NewLine;&NewLine; <strong>return true<&sol;strong>&semi;&NewLine;&NewLine; <strong>case <&sol;strong>DragEvent&period;<strong><em>ACTION&lowbar;DROP<&sol;em><&sol;strong>&colon;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Gets the item containing the dragged data<&sol;span><&sol;strong>&NewLine;<&sol;em> ClipData&period;Item item &equals; dragEvent&period;getClipData&lpar;&rpar;&period;getItemAt&lpar;0&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Gets the text data from the item&period;<&sol;span><&sol;strong>&NewLine;<&sol;em> String dragData &equals; item&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Displays a message containing the dragged data&period;<&sol;span><&sol;strong>&NewLine;<&sol;em> Toast&period;<em>makeText<&sol;em>&lpar;<strong>this<&sol;strong>&comma; <strong>"Dragged data is " <&sol;strong>&plus; dragData&comma; Toast&period;<strong><em>LENGTH&lowbar;SHORT<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Turns off any color tints<&sol;span><&sol;strong>&NewLine;<&sol;em> view&period;getBackground&lpar;&rpar;&period;clearColorFilter&lpar;&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Invalidates the view to force a redraw<&sol;span><&sol;strong>&NewLine;<&sol;em> view&period;invalidate&lpar;&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;get dragged view<&sol;span><&sol;strong>&NewLine;<&sol;em> View v &equals; &lpar;View&rpar; dragEvent&period;getLocalState&lpar;&rpar;&semi;&NewLine; ViewGroup owner &equals; &lpar;ViewGroup&rpar; v&period;getParent&lpar;&rpar;&semi;&NewLine; owner&period;removeView&lpar;v&rpar;&semi; <em><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;remove the dragged view<&sol;strong><&sol;span>&NewLine;<&sol;em> LinearLayout container &equals; &lpar;LinearLayout&rpar; view&semi; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;caste the view into LinearLayout as our drag acceptable layout is LinearLayout<&sol;span><&sol;strong>&NewLine;<&sol;em> container&period;addView&lpar;v&rpar;&semi; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Add the dragged view<&sol;span><&sol;strong>&NewLine;<&sol;em> v&period;setVisibility&lpar;View&period;<strong><em>VISIBLE<&sol;em><&sol;strong>&rpar;&semi;<em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;finally set Visibility to VISIBLE<&sol;span><&sol;strong>&NewLine;&NewLine;<&sol;em><strong><span style&equals;"color&colon; &num;008000&semi;"><em> &sol;&sol; Returns true&period; DragEvent&period;getResult&lpar;&rpar; will return true&period;&NewLine;<&sol;em><&sol;span> return true<&sol;strong>&semi;&NewLine;&NewLine; <strong>case <&sol;strong>DragEvent&period;<strong><em>ACTION&lowbar;DRAG&lowbar;ENDED<&sol;em><&sol;strong>&colon;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Turns off any color tinting<&sol;span><&sol;strong>&NewLine;<&sol;em> view&period;getBackground&lpar;&rpar;&period;clearColorFilter&lpar;&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> <em>&sol;&sol; Invalidates the view to force a redraw&NewLine;<&sol;em><&sol;span><&sol;strong> view&period;invalidate&lpar;&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> <em>&sol;&sol; invoke getResult&lpar;&rpar;&comma; and displays what happened&period;&NewLine;<&sol;em><&sol;span> if <&sol;strong>&lpar;dragEvent&period;getResult&lpar;&rpar;&rpar;&NewLine; Toast&period;<em>makeText<&sol;em>&lpar;<strong>this<&sol;strong>&comma; <strong>"The drop was handled&period;"<&sol;strong>&comma; Toast&period;<strong><em>LENGTH&lowbar;SHORT<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; <strong>else&NewLine;<&sol;strong> Toast&period;<em>makeText<&sol;em>&lpar;<strong>this<&sol;strong>&comma; <strong>"The drop didn't work&period;"<&sol;strong>&comma; Toast&period;<strong><em>LENGTH&lowbar;SHORT<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; returns true&semi; the value is ignored&period;<&sol;span><&sol;strong>&NewLine;<&sol;em> <strong>return true<&sol;strong>&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; An unknown action type was received&period;<&sol;span><&sol;strong>&NewLine;<&sol;em> <strong>default<&sol;strong>&colon;&NewLine; Log&period;<em>e<&sol;em>&lpar;<strong>"DragDrop Example"<&sol;strong>&comma; <strong>"Unknown action type received by OnDragListener&period;"<&sol;strong>&rpar;&semi;&NewLine; <strong>break<&sol;strong>&semi;&NewLine; &rcub;&NewLine; <strong>return false<&sol;strong>&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; <strong>public boolean <&sol;strong>onLongClick&lpar;View view&rpar; &lbrace;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> <em>&sol;&sol; Create a new ClipData&period;Item from the tag&NewLine;<&sol;em><&sol;span><&sol;strong> ClipData&period;Item item &equals; <strong>new <&sol;strong>ClipData&period;Item&lpar;&lpar;CharSequence&rpar; view&period;getTag&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine; String&lbrack;&rsqb; mimeTypes &equals; &lbrace;ClipDescription&period;<strong><em>MIMETYPE&lowbar;TEXT&lowbar;PLAIN<&sol;em><&sol;strong>&rcub;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong><em>&sol;&sol; Create a new ClipData using the tag as a label&comma; the plain text MIME type&comma; and&NewLine; &sol;&sol; the already-created item&period; This will create a new ClipDescription object within the&NewLine;<&sol;em><&sol;strong><&sol;span><em><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; ClipData&comma; and set its MIME type entry to "text&sol;plain"<&sol;strong><&sol;span>&NewLine;<&sol;em> ClipData data &equals; <strong>new <&sol;strong>ClipData&lpar;view&period;getTag&lpar;&rpar;&period;toString&lpar;&rpar;&comma; mimeTypes&comma; item&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Instantiates the drag shadow builder&period;<&sol;span><&sol;strong>&NewLine;<&sol;em> View&period;DragShadowBuilder shadowBuilder &equals; <strong>new <&sol;strong>View&period;DragShadowBuilder&lpar;view&rpar;&semi;&NewLine;&NewLine; <em><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; Starts the drag<&sol;strong><&sol;span>&NewLine;<&sol;em> view&period;startDrag&lpar;data <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;data to be dragged<&sol;span><&sol;strong>&NewLine;<&sol;em> &comma; shadowBuilder <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;drag shadow<&sol;span><&sol;strong>&NewLine;<&sol;em> &comma; view <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;local data about the drag and drop operation<&sol;span><&sol;strong>&NewLine;<&sol;em> &comma; 0 <em><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;flags &lpar;not currently used&comma; set to 0&rpar;<&sol;span><&sol;strong>&NewLine;<&sol;em> &rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> <em>&sol;&sol;Set view visibility to INVISIBLE as we are going to drag the view&NewLine;<&sol;em><&sol;span><&sol;strong> view&period;setVisibility&lpar;View&period;<strong><em>INVISIBLE<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <strong>return true<&sol;strong>&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>3&period; When you run the app it will look like this as shown below&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image"><figure class&equals;"aligncenter is-resized"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;12&sol;Screenshot&lowbar;1608202010&period;png" alt&equals;"" class&equals;"wp-image-2300" width&equals;"331" height&equals;"588"&sol;><&sol;figure><&sol;div>&NewLine;&NewLine;

Exit mobile version