Introduction

Somewhere we played a game of dragging objects to an appropriate location. We will develop something like this with the same functionality. I have created a mockup of our application.
DragAndroid1.jpg
So let's start with our new application. You know very well how to create a new project and which type of parameter you need to pass. If not then visit http://www.c-sharpcorner.com/UploadFile/88b6e5/first-application-in-android-studio/.
Note: This project requires a minimum SDK version of 12 or greater.
Step 1:
Open your "values/strings.xml" file. We need to create some string resources inside it.
Strings.xml
  1. <string name="intro">Place following persons in Appropriate Position.</string>
  2. <string name="option_1">Mahesh Chand</string>
  3. <string name="option_2">Praveen Kumar</string>
  4. <string name="choice_1">Founder and Editor-in-Chief</string>
  5. <string name="choice_2">Editorial Director, Product Manager</string>
Step 2:
To provide a background color effect, we need to create a custom shape and will pass this to option views. So use the following procedure.
Click on "Project" then open the "res" directory then right-click and select "New" -> "Android Resource File".
Then enter the name "option.xml" and select "drawable" from the drop-down list.
Option.xml
  1. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:dither="true" >
  3. <solid android:color="#ffffaa77" />
  4. <corners android:radius="2dp" />
  5. <stroke
  6. android:width="2dp"
  7. android:color="#ffffaa77" />
  8. <padding
  9. android:bottom="5dp"
  10. android:left="10dp"
  11. android:right="10dp"
  12. android:top="5dp" />
  13. </shape>
Step 3:
To provide a background color effect, we need to create a custom shape and pass it to choice views. So use the following procedure.
Click on "Project" then open the "res" directory then right-click and select "New" -> "Android Resource File".
Then enter the name "choice.xml" and select "drawable" from the drop-down list.
choice.xml
  1. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:dither="true" >
  3. <solid android:color="#ffffff99" />
  4. <corners android:radius="2dp" />
  5. <stroke
  6. android:dashGap="4dp"
  7. android:dashWidth="2dp"
  8. android:width="2dp"
  9. android:color="#ffffff00" />
  10. <padding
  11. android:bottom="5dp"
  12. android:left="5dp"
  13. android:right="5dp"
  14. android:top="5dp" />
  15. </shape>
Step 4:
Now, open your layout file and paste in the following code.
Activity_drag_drop.xml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="vertical"
  5. android:padding="10dp"
  6. android:paddingLeft="50dp"
  7. android:paddingRight="50dp" >
  8. <TextView
  9. android:id="@+id/option_1"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_margin="5dp"
  13. android:background="@drawable/option"
  14. android:gravity="center"
  15. android:text="@string/option_1"
  16. android:textSize="18sp"
  17. android:textStyle="bold" />
  18. <TextView
  19. android:id="@+id/option_2"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:layout_margin="5dp"
  23. android:background="@drawable/option"
  24. android:gravity="center"
  25. android:text="@string/option_2"
  26. android:textSize="18sp"
  27. android:textStyle="bold" />
  28. <TextView
  29. android:id="@+id/choice_1"
  30. android:layout_width="fill_parent"
  31. android:layout_height="wrap_content"
  32. android:layout_margin="5dp"
  33. android:background="@drawable/choice"
  34. android:gravity="center"
  35. android:textSize="18sp"
  36. android:text="@string/choice_1" />
  37. <TextView
  38. android:id="@+id/choice_2"
  39. android:layout_width="fill_parent"
  40. android:layout_height="wrap_content"
  41. android:layout_margin="5dp"
  42. android:background="@drawable/choice"
  43. android:gravity="center"
  44. android:textSize="18sp"
  45. android:text="@string/choice_2" />
  46. </LinearLayout>
The following will be the output of your layout:
DragAndroid2.jpg
Step 5:
Open your "Activity" class. First, we use a resource id and reference it to the views.
First, we define:
  1. private TextView option1, option2, choice1, choice2;
  2. Inside onCreate(), write following code.
  3. //views to drag
  4. option1 = (TextView)findViewById(R.id.option_1);
  5. option2 = (TextView)findViewById(R.id.option_2);
  6. //views to drop onto
  7. choice1 = (TextView)findViewById(R.id.choice_1);
  8. choice2 = (TextView)findViewById(R.id.choice_2);
Your project structure will look like:
DragAndroid3.jpg
Step 6:
The references to layout ids are now done. We will add "TouchListener" to our activity.
  1. private final class ChoiceTouchListener implements View.OnTouchListener {
  2. @Override
  3. public boolean onTouch(View view, MotionEvent motionEvent) {
  4. if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
  5. //setup drag
  6. return true;
  7. }
  8. else {
  9. return false;
  10. }
  11. }
  12. }
We are only interested in cases where the user has touched the View to drag it, so inside the if statement we will set up the drag operation before the "return true" statement.
  1. //setup drag
  2. ClipData data = ClipData.newPlainText("", "");
  3. View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
  4. //start dragging the item touched
  5. view.startDrag(data, shadowBuilder, view, 0);
Step 7:
We have created a class that will handle the Touch Event and if the Touch Event is "Down" then we have written the code for starting the dragging of the particular view on the touch that is to be performed.
We now need to write some shortcode to handle the Drag event. So write the following code.
  1. private class ChoiceDragListener implements View.OnDragListener {
  2. @Override
  3. public boolean onDrag(View v, DragEvent dragEvent) {
  4. switch (dragEvent.getAction()) {
  5. case DragEvent.ACTION_DRAG_STARTED:
  6. //no action necessary
  7. break;
  8. case DragEvent.ACTION_DRAG_ENTERED:
  9. //no action necessary
  10. break;
  11. case DragEvent.ACTION_DRAG_EXITED:
  12. //no action necessary
  13. break;
  14. case DragEvent.ACTION_DROP:
  15. //handle the dragged view being dropped over a drop view
  16. View view = (View) dragEvent.getLocalState();
  17. //stop displaying the view where it was before it was dragged
  18. view.setVisibility(View.INVISIBLE);
  19. //view dragged item is being dropped on
  20. TextView dropTarget = (TextView) v;
  21. //view being dragged and dropped
  22. TextView dropped = (TextView) view;
  23. //update the text in the target view to reflect the data being dropped
  24. dropTarget.setText(dropped.getText());
  25. //make it bold to highlight the fact that an item has been dropped
  26. dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
  27. //if an item has already been dropped here, there will be a tag
  28. Object tag = dropTarget.getTag();
  29. //if there is already an item here, set it back visible in its original place
  30. if(tag!=null)
  31. {
  32. //the tag is the view id already dropped here
  33. int existingID = (Integer)tag;
  34. //set the original view visible again
  35. findViewById(existingID).setVisibility(View.VISIBLE);
  36. }
  37. //set the tag in the target view being dropped on - to the ID of the view being dropped
  38. dropTarget.setTag(dropped.getId());
  39. break;
  40. case DragEvent.ACTION_DRAG_ENDED:
  41. //no action necessary
  42. break;
  43. default:
  44. break;
  45. }
  46. return true;
  47. }
  48. }
Step 8:
Add listeners to views. Once again go to "onCreate()" and after the reference of the views, add the following code.
  1. //set touch listeners
  2. option1.setOnTouchListener(new ChoiceTouchListener());
  3. option2.setOnTouchListener(new ChoiceTouchListener());
  4. //set drag listeners
  5. choice1.setOnDragListener(new ChoiceDragListener());
  6. choice2.setOnDragListener(new ChoiceDragListener());
Step 9:
You are done with your coding. What you need to do is just run the application. When you see your application in an emulator or in a device:
DragAndroid4.jpg
Click on the "Mahesh Chand" text view, and drag it to "Founder and Editor-in-Chief" and Drop it there. Perform the same action for "Praveen Kumar".
DragAndroid5.jpg
DragAndroid6.jpg
DragAndroid7.jpg

Summary

Today we learned how to use Drag & Drop functionality of Android SDK. This is a very good feature provided by Android. We can also apply this functionality in other game development applications.