In
this android example we learn how to implement Drag and Drop object
into one to another place or Layout, by implement touch event and motion
event.
CREATE NEW PROJECT
Step 1: Write code into activity_main.xml
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/oneLayout"
android:layout_width="fill_parent"
android:layout_height="210dp"
android:background="#80d04b"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/dragtext"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/twoLayout"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_marginTop="210dp"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
Step 2: Write code into MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.View.DragShadowBuilder;
import android.widget.LinearLayout;
import android.util.Log;
public class MainActivity extends Activity implements OnTouchListener,
OnDragListener {
private static final String LOGCAT = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.textView1).setOnTouchListener(this);
findViewById(R.id.oneLayout).setOnDragListener(this);
findViewById(R.id.twoLayout).setOnDragListener(this);
}
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
public boolean onDrag(View layoutview, DragEvent dragevent) {
int action = dragevent.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d(LOGCAT, "Drag event started");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(LOGCAT, "Drag Object entered into " + layoutview.toString());
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(LOGCAT, "Drag Object exited from " + layoutview.toString());
break;
case DragEvent.ACTION_DROP:
Log.d(LOGCAT, "Dropped");
View view = (View) dragevent.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) layoutview;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(LOGCAT, "Drag Completed");
break;
default:
break;
}
return true;
}
}
NOTE:No need special permission in AndroidManifest.xml
Step 3: Run Your Project:
No comments :
Post a Comment