package com.example.dragdropview;
import android.app.Activity;
import android.content.ClipData;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class DragDropActivity extends Activity {
Button bikeImage, vanImage, truckImage;
LinearLayout bikeLayout, vanLayout, truckLayout;
View bikeLine = null, vanLine = null, truckLine = null;
int total, failure = 0;
private boolean showBikeLayout = false, showVanLayout = false,
showTruckLayout = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drag_drop);
bikeImage = (Button) findViewById(R.id.bikeImage);
bikeLine = (View) findViewById(R.id.bikeLine);
vanImage = (Button) findViewById(R.id.vanImage);
vanLine = (View) findViewById(R.id.vanLine);
truckImage = (Button) findViewById(R.id.truckImage);
truckLine = (View) findViewById(R.id.truckLine);
bikeLayout = (LinearLayout) findViewById(R.id.bikeLayout);
bikeLayout.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
// Handles each of the expected events
switch (event.getAction()) {
// signal for the start of a drag and drop operation.
case DragEvent.ACTION_DRAG_STARTED:
System.out.println(" ======bikeLayout ACTION_DRAG_STARTED");
// do nothing
break;
// the drag point has entered the bounding box of the View
case DragEvent.ACTION_DRAG_ENTERED:
// v.setBackground(vanShape); //change the shape of the view
System.out.println(" ======bikeLayout ACTION_DRAG_ENTERED");
vanImage.setBackground(getResources().getDrawable(
R.drawable.van));
break;
// the user has moved the drag shadow outside the bounding box
// of the View
case DragEvent.ACTION_DRAG_EXITED:
System.out.println(" ======bikeLayout ACTION_DRAG_EXITED");
// v.setBackground(normalShape); //change the shape of the
// view back to normal
break;
// drag shadow has been released,the drag point is within the
// bounding box of the View
case DragEvent.ACTION_DROP:
System.out.println(" ======bikeLayout ACTION_DROP");
showBikeLayout = true;
showVanLayout = false;
showTruckLayout = false;
showBikeVanTruckImage();
// if the view is the bottomlinear, we accept the drag item
break;
// the drag and drop operation has concluded...
case DragEvent.ACTION_DRAG_ENDED:
System.out.println(" ======bikeLayout ACTION_DRAG_ENDED");
default:
break;
}
return true;
}
});
vanLayout = (LinearLayout) findViewById(R.id.vanLayout);
vanLayout.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
// Handles each of the expected events
switch (event.getAction()) {
// signal for the start of a drag and drop operation.
case DragEvent.ACTION_DRAG_STARTED:
System.out.println(" ======vanLayout ACTION_DRAG_STARTED");
// do nothing
break;
// the drag point has entered the bounding box of the View
case DragEvent.ACTION_DRAG_ENTERED:
// v.setBackground(vanShape); //change the shape of the view
System.out.println(" ======vanLayout ACTION_DRAG_ENTERED");
vanImage.setBackground(getResources().getDrawable(
R.drawable.van));
break;
// the user has moved the drag shadow outside the bounding box
// of the View
case DragEvent.ACTION_DRAG_EXITED:
System.out.println(" ======vanLayout ACTION_DRAG_EXITED");
// v.setBackground(normalShape); //change the shape of the
// view back to normal
break;
// drag shadow has been released,the drag point is within the
// bounding box of the View
case DragEvent.ACTION_DROP:
System.out.println(" ======vanLayout ACTION_DROP");
showBikeLayout = false;
showVanLayout = true;
showTruckLayout = false;
showBikeVanTruckImage();
// if the view is the bottomlinear, we accept the drag item
break;
// the drag and drop operation has concluded...
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
});
truckLayout = (LinearLayout) findViewById(R.id.truckLayout);
truckLayout.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
// Handles each of the expected events
switch (event.getAction()) {
// signal for the start of a drag and drop operation.
case DragEvent.ACTION_DRAG_STARTED:
System.out
.println(" ======truckLayout ACTION_DRAG_STARTED");
// do nothing
break;
// the drag point has entered the bounding box of the View
case DragEvent.ACTION_DRAG_ENTERED:
// v.setBackground(vanShape); //change the shape of the view
System.out
.println(" ======truckLayout ACTION_DRAG_ENTERED");
truckImage.setBackground(getResources().getDrawable(
R.drawable.truck));
break;
// the user has moved the drag shadow outside the bounding box
// of the View
case DragEvent.ACTION_DRAG_EXITED:
// v.setBackground(normalShape); //change the shape of the
// view back to normal
System.out.println(" ======truckLayout ACTION_DRAG_EXITED");
break;
// drag shadow has been released,the drag point is within the
// bounding box of the View
case DragEvent.ACTION_DROP:
System.out.println(" ======truckLayout ACTION_DROP");
showBikeLayout = false;
showVanLayout = false;
showTruckLayout = true;
showBikeVanTruckImage();
// if the view is the bottomlinear, we accept the drag item
break;
// the drag and drop operation has concluded...
case DragEvent.ACTION_DRAG_ENDED:
System.out.println(" ======truckLayout ACTION_DRAG_ENDED");
default:
break;
}
return true;
}
});
bikeImage.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(
bikeImage);
v.startDrag(data, shadow, null, 0);
return false;
}
});
vanImage.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(
vanImage);
v.startDrag(data, shadow, null, 0);
return false;
}
});
truckImage.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(
truckImage);
v.startDrag(data, shadow, null, 0);
return false;
}
});
}
private void showBikeVanTruckImage() {
if (showBikeLayout) { // here image draged from bike position and droped
// in van...
vanImage.setVisibility(View.GONE);
vanLine.setVisibility(View.VISIBLE);
truckImage.setVisibility(View.GONE);
truckLine.setVisibility(View.VISIBLE);
bikeImage.setVisibility(View.VISIBLE);
bikeLine.setVisibility(View.GONE);
bikeImage
.setBackground(getResources().getDrawable(R.drawable.bike));
// go back to normal shape
} else
if (showVanLayout) {
bikeImage.setVisibility(View.GONE);
bikeLine.setVisibility(View.VISIBLE);
truckImage.setVisibility(View.GONE);
truckLine.setVisibility(View.VISIBLE);
vanImage.setVisibility(View.VISIBLE);
vanLine.setVisibility(View.GONE);
vanImage.setBackground(getResources().getDrawable(R.drawable.van));
// go back to normal shape
} else
if (showTruckLayout) {
// here image dragged from truck view and dropped in van view..
vanImage.setVisibility(View.GONE);
vanLine.setVisibility(View.VISIBLE);
bikeImage.setVisibility(View.GONE);
bikeLine.setVisibility(View.VISIBLE);
truckImage.setVisibility(View.VISIBLE);
truckLine.setVisibility(View.GONE);
// image moved from truck to van...so, set image for van here..
truckImage.setBackground(getResources().getDrawable(
R.drawable.truck)); // go back to normal shape
}
}
}
activity_drag_drop.xml :
<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="horizontal"
android:gravity="center"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/bikeLayout"
android:layout_width="0dp"
android:layout_height="100dp"
android:gravity="center"
android:background="#00ff00"
android:orientation="vertical"
android:layout_weight="1" >
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/bikeImage"
android:background="@drawable/bike" />
<View
android:layout_width="wrap_content"
android:layout_height="3dp"
android:id="@+id/bikeLine"
android:background="#000"
android:visibility="gone"/>
</LinearLayout>
<LinearLayout
android:id="@+id/vanLayout"
android:layout_width="0dp"
android:layout_height="100dp"
android:gravity="center"
android:background="#00ffff"
android:orientation="vertical"
android:layout_weight="1" >
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/vanImage"
android:background="@drawable/van"
android:visibility="gone" />
<View
android:layout_width="wrap_content"
android:layout_height="3dp"
android:id="@+id/vanLine"
android:background="#000" />
</LinearLayout>
<LinearLayout
android:id="@+id/truckLayout"
android:layout_width="0dp"
android:layout_height="100dp"
android:gravity="center"
android:background="#00ccbb"
android:orientation="vertical"
android:layout_weight="1" >
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/truckImage"
android:background="@drawable/truck"
android:visibility="gone" />
<View
android:layout_width="wrap_content"
android:layout_height="3dp"
android:id="@+id/truckLine"
android:background="#000" />
</LinearLayout>
</LinearLayout>
Logcat Log data after execution of this project :
drag from position bike to van:
05-11 17:26:37.680: I/System.out(26490): ======bikeLayout ACTION_DRAG_STARTED
05-11 17:26:37.680: I/System.out(26490): ======vanLayout ACTION_DRAG_STARTED
05-11 17:26:37.680: I/System.out(26490): ======truckLayout ACTION_DRAG_STARTED
05-11 17:26:37.680: I/System.out(26490): ======bikeLayout ACTION_DRAG_ENTERED
05-11 17:26:37.690: I/System.out(26490): ======bikeLayout ACTION_DRAG_EXITED
05-11 17:26:37.690: I/System.out(26490): ======vanLayout ACTION_DRAG_ENTERED
05-11 17:26:39.160: I/System.out(26490): ======vanLayout ACTION_DROP
05-11 17:26:39.160: I/System.out(26490): ======vanLayout getX148.0
05-11 17:26:39.160: I/System.out(26490): ======vanLayout getY44.0
05-11 17:26:39.160: I/ViewRootImpl(26490): Reporting drop result: true
05-11 17:26:39.170: W/WindowManager(11649): Drag is in progress but there is no drag window handle.
05-11 17:26:39.180: I/System.out(26490): ======bikeLayout ACTION_DRAG_ENDED
05-11 17:26:39.180: I/System.out(26490): ======truckLayout ACTION_DRAG_ENDED
drag from van to truck position :
05-11 17:27:02.541: I/System.out(26490): ======bikeLayout ACTION_DRAG_STARTED
05-11 17:27:02.541: I/System.out(26490): ======vanLayout ACTION_DRAG_STARTED
05-11 17:27:02.571: I/System.out(26490): ======truckLayout ACTION_DRAG_STARTED
05-11 17:27:02.641: I/System.out(26490): ======vanLayout ACTION_DRAG_ENTERED
05-11 17:27:02.971: I/System.out(26490): ======vanLayout ACTION_DRAG_EXITED
05-11 17:27:02.971: I/System.out(26490): ======truckLayout ACTION_DRAG_ENTERED
05-11 17:27:04.646: I/System.out(26490): ======truckLayout ACTION_DROP
05-11 17:27:04.646: I/System.out(26490): ======truckLayout getX149.0
05-11 17:27:04.651: I/System.out(26490): ======truckLayout getY52.0
05-11 17:27:04.651: I/ViewRootImpl(26490): Reporting drop result: true
05-11 17:27:04.661: W/WindowManager(11649): Drag is in progress but there is no drag window handle.
05-11 17:27:04.671: I/System.out(26490): ======bikeLayout ACTION_DRAG_ENDED
05-11 17:27:04.671: I/System.out(26490): ======truckLayout ACTION_DRAG_ENDED
drag from van to bike:
05-11 17:27:26.530: I/System.out(26490): ======bikeLayout ACTION_DRAG_STARTED
05-11 17:27:26.530: I/System.out(26490): ======vanLayout ACTION_DRAG_STARTED
05-11 17:27:26.530: I/System.out(26490): ======truckLayout ACTION_DRAG_STARTED
05-11 17:27:26.571: I/System.out(26490): ======vanLayout ACTION_DRAG_ENTERED
05-11 17:27:26.710: I/System.out(26490): ======vanLayout ACTION_DRAG_EXITED
05-11 17:27:26.710: I/System.out(26490): ======bikeLayout ACTION_DRAG_ENTERED
05-11 17:27:28.196: I/System.out(26490): ======bikeLayout ACTION_DROP
05-11 17:27:28.196: I/System.out(26490): ======bikeLayout getX103.0
05-11 17:27:28.200: I/System.out(26490): ======bikeLayout getY51.0
05-11 17:27:28.200: I/ViewRootImpl(26490): Reporting drop result: true
05-11 17:27:28.210: W/WindowManager(11649): Drag is in progress but there is no drag window handle.
05-11 17:27:28.220: I/System.out(26490): ======bikeLayout ACTION_DRAG_ENDED
05-11 17:27:28.220: I/System.out(26490): ======truckLayout ACTION_DRAG_ENDED
Output Will Come Like this :
Helpful Urls:
https://github.com/Grishu/MYDroid
https://www.learn2crack.com/2014/03/android-drag-and-drop-example.html
Comments
Post a Comment