Skip to content

Instantly share code, notes, and snippets.

@sourcerebels
Created March 10, 2017 21:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sourcerebels/d3356e129d976a3480d6e7489a5faff7 to your computer and use it in GitHub Desktop.
Save sourcerebels/d3356e129d976a3480d6e7489a5faff7 to your computer and use it in GitHub Desktop.
Android Drag & Drop Sample
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#292a2e"
tools:context="com.sourcerebels.dragdrop.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:layout_centerInParent="true"
android:text="Mew Mew"/>
<ImageView
android:id="@+id/paper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/paper"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="16dp"/>
<ImageView
android:id="@+id/trash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/trash_closed"
android:layout_margin="16dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
package com.sourcerebels.dragdrop;
import android.content.ClipData;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView paper;
private ImageView trash;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setFullScreen();
setContentView(R.layout.activity_main);
paper = (ImageView) findViewById(R.id.paper);
paper.setTag("paper");
trash = (ImageView) findViewById(R.id.trash);
trash.setTag("trash");
trash.setOnDragListener(new TrashDragListener(
R.drawable.trash_open,
R.drawable.trash_closed));
trash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
paper.setVisibility(View.VISIBLE);
}
});
paper.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
}
});
}
private static class TrashDragListener implements View.OnDragListener {
private static final String TAG = "TrashDragListener";
private int enterShape;
private int normalShape;
private boolean hit;
public TrashDragListener(int enterShape, int normalShape) {
this.enterShape = enterShape;
this.normalShape = normalShape;
}
@Override
public boolean onDrag(View v, DragEvent event) {
final ImageView containerView = (ImageView) v;
final ImageView draggedView = (ImageView) event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d(TAG, "onDrag: ACTION_DRAG_STARTED");
hit = false;
return true;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(TAG, "onDrag: ACTION_DRAG_ENTERED");
containerView.setImageResource(enterShape);
return true;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(TAG, "onDrag: ACTION_DRAG_EXITED");
containerView.setImageResource(normalShape);
return true;
case DragEvent.ACTION_DROP:
Log.d(TAG, "onDrag: ACTION_DROP");
hit = true;
draggedView.post(new Runnable() {
@Override
public void run() {
draggedView.setVisibility(View.GONE);
}
});
return true;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(TAG, "onDrag: ACTION_DRAG_ENDED");
containerView.setImageResource(normalShape);
v.setVisibility(View.VISIBLE);
if (!hit) {
draggedView.post(new Runnable() {
@Override
public void run() {
draggedView.setVisibility(View.VISIBLE);
}
});
}
return true;
default:
return true;
}
}
}
private void setFullScreen() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
@real-ss
Copy link

real-ss commented Apr 9, 2018

nice

@SEOJAEHWA
Copy link

thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment