Like Us on Facebook

Android Drag and Drop Example

Hi Friends,  Welcome to UandBlog.com again . Today I am going to discuss with  Android  Drag and Drop tutorial. Android drag/drop framework allows users to move data from one View to another in the current layout using a graphical drag and drop gesture. Drag and drop gesture is used across several android applications, especially in android games. When you start drag , you include both the data you are moving and metadata describing this data as part of the call to the system . let’s get started with the tutorial.

Read More
Android Image Slideshow Using ViewPager
How to Create Overflow Menus in Android


Prerequisites:
  • JDK 7.0 or Above
  • Android Studio 2.0

Steps to Follow:

Step 1: Create a New Android Studio project name DragandDrop.

Step 2: Select minimum API level 16  so that it can support maximum of Android Device available in the google play.

Step 3: Select  Blank  Activity and Give your layout xml name is activity_main and click finish.

Step  4: Inside activity_main.xml configure two Linearlayout with a button  to drag and drop like below.
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:gravity="center"
 tools:context=".MainActivity" >
 <LinearLayout
 android:id="@+id/bottomlinear"
 android:layout_width="match_parent"
 android:layout_height="400px"
 android:gravity="center"
 android:background="#E9967a"
 android:orientation="vertical" >
 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/one"
 android:textSize="30sp"
 android:text="Drag it" />
 </LinearLayout>

 <LinearLayout
 android:id="@+id/bottomlinear1"
 android:layout_width="match_parent"
 android:layout_height="400px"
 android:gravity="center"
 android:background="#00CED1"
 android:orientation="vertical" >
 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/drop"
 android:textSize="30sp"
 android:text="Drop Here" />
<TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/Total"
 android:textSize="20sp" />

<TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/Sucess"
 android:textSize="20sp" />

 </LinearLayout>

</LinearLayout>
 
Step 5: Inside MainActivity.java  write the following code.
 
public class MainActivity extends AppCompatActivity {
Button dragbutton;
LinearLayout dropbutton;
TextView tv,sucess;
int total,fail = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dragbutton = (Button) findViewById(R.id.one);
dropbutton = (LinearLayout) findViewById(R.id.bottomlinear);
tv = (TextView) findViewById(R.id.Total);
sucess = (TextView) findViewById(R.id.Sucess);
dropbutton.setOnDragListener(new View.OnDragListener(){
@Override
public boolean onDrag(View v, DragEvent event) {
// TODO Auto-generated method stub
final int action = event.getAction();
switch (action){
 case DragEvent.ACTION_DRAG_STARTED:
// Executed after startDrag() is called.
 break;
 case DragEvent.ACTION_DRAG_EXITED:
 break;
 case DragEvent.ACTION_DRAG_ENTERED:
 // Executed after the Drag Shadow enters the drop area
 break;
 case DragEvent.ACTION_DROP: {
 //Executed when user drops the data
 fail = fail + 1;
 return (true);
 }
 case DragEvent.ACTION_DRAG_ENDED: {
 total = total + 1;
int value = total - fail;
sucess.setText("Sucessful Drops:" + value);
tv.setText("Total Drops: " + total);
return (true);
}
default:
break;
}
return true;
}
});
dragbutton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(dragbutton);
v.startDrag(data, shadow, null, 0);
return false;
}
});
}
}
Now  run the application  and  your sequence of output will be like below.

Drag-and-Drop-uandblog

Posted By UandBlog

UandBlog is a Global Leading source of Finance, Health, Lifestyle, Technology, Gaming and Programing and other information, it is a fastest growing blog for all , for latest post or information like us on facebook , follow on Twitter, Google+ and Pinterest.

Comments ( 0 )

    Leave a comment..

    Click to login.

    Subscribe Latest Information

    Most Popular Posts