Skip to content

Commit b596fb9

Browse files
committed
DnD example cleaned up
1 parent 3f11477 commit b596fb9

File tree

4 files changed

+64
-77
lines changed

4 files changed

+64
-77
lines changed

de.vogella.android.draganddrop/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
android:versionCode="1"
55
android:versionName="1.0" >
66

7-
<uses-sdk android:minSdkVersion="15" />
7+
<uses-sdk android:minSdkVersion="21" />
88

99
<application
1010
android:icon="@drawable/ic_launcher"

de.vogella.android.draganddrop/project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
# project structure.
99

1010
# Project target.
11-
target=android-19
11+
target=android-21

de.vogella.android.draganddrop/res/layout/main.xml

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,44 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:layout_width="fill_parent"
4-
android:layout_height="fill_parent"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
55
android:columnCount="2"
6-
android:columnWidth="300dp"
6+
android:columnWidth="320dp"
77
android:orientation="vertical"
88
android:rowCount="2"
99
android:stretchMode="columnWidth" >
1010

1111
<LinearLayout
1212
android:id="@+id/topleft"
1313
android:layout_width="160dp"
14-
android:layout_height="200dp"
15-
android:layout_column="0"
14+
android:layout_height="160dp"
1615
android:layout_row="0"
1716
android:background="@drawable/shape" >
1817

1918
<ImageView
2019
android:id="@+id/myimage1"
2120
android:layout_width="wrap_content"
2221
android:layout_height="wrap_content"
23-
android:layout_column="0"
24-
android:layout_row="0"
2522
android:src="@drawable/ic_launcher" />
2623
</LinearLayout>
2724

2825
<LinearLayout
2926
android:id="@+id/topright"
3027
android:layout_width="160dp"
31-
android:layout_height="200dp"
32-
android:layout_column="1"
33-
android:layout_row="0"
28+
android:layout_height="160dp"
3429
android:background="@drawable/shape" >
3530

3631
<ImageView
3732
android:id="@+id/myimage2"
3833
android:layout_width="wrap_content"
3934
android:layout_height="wrap_content"
40-
android:layout_column="0"
41-
android:layout_row="0"
4235
android:src="@drawable/ic_launcher" />
4336
</LinearLayout>
4437

4538
<LinearLayout
4639
android:id="@+id/bottomleft"
4740
android:layout_width="160dp"
48-
android:layout_height="200dp"
49-
android:layout_column="0"
50-
android:layout_row="1"
41+
android:layout_height="160dp"
5142
android:background="@drawable/shape" >
5243

5344
<ImageView
@@ -60,17 +51,13 @@
6051
<LinearLayout
6152
android:id="@+id/bottomright"
6253
android:layout_width="160dp"
63-
android:layout_height="200dp"
64-
android:layout_column="1"
65-
android:layout_row="1"
54+
android:layout_height="160dp"
6655
android:background="@drawable/shape" >
6756

6857
<ImageView
6958
android:id="@+id/myimage4"
7059
android:layout_width="wrap_content"
7160
android:layout_height="wrap_content"
72-
android:layout_column="0"
73-
android:layout_row="0"
7461
android:src="@drawable/ic_launcher" />
7562
</LinearLayout>
7663

de.vogella.android.draganddrop/src/de/vogella/android/draganddrop/DragActivity.java

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,71 +13,71 @@
1313
import android.view.ViewGroup;
1414
import android.widget.LinearLayout;
1515

16-
public class DragActivity extends Activity {
17-
@Override
18-
public void onCreate(Bundle savedInstanceState) {
19-
super.onCreate(savedInstanceState);
20-
setContentView(R.layout.main);
21-
findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
22-
findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
23-
findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
24-
findViewById(R.id.myimage4).setOnTouchListener(new MyTouchListener());
25-
26-
findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
27-
findViewById(R.id.topright).setOnDragListener(new MyDragListener());
28-
findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener());
29-
findViewById(R.id.bottomright).setOnDragListener(new MyDragListener());
30-
31-
}
32-
33-
private final class MyTouchListener implements OnTouchListener {
16+
public class DragActivity extends Activity implements OnDragListener {
17+
private Drawable enterShape;
18+
private Drawable normalShape;
19+
OnTouchListener dragListener = new OnTouchListener() {
20+
@Override
3421
public boolean onTouch(View view, MotionEvent motionEvent) {
22+
// start move on a touch event
3523
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
3624
ClipData data = ClipData.newPlainText("", "");
37-
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
38-
view);
25+
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
3926
view.startDrag(data, shadowBuilder, view, 0);
4027
view.setVisibility(View.INVISIBLE);
4128
return true;
42-
} else {
43-
return false;
4429
}
30+
return false;
31+
4532
}
46-
}
33+
};
4734

48-
class MyDragListener implements OnDragListener {
49-
Drawable enterShape = getResources().getDrawable(
50-
R.drawable.shape_droptarget);
51-
Drawable normalShape = getResources().getDrawable(R.drawable.shape);
35+
@Override
36+
public void onCreate(Bundle savedInstanceState) {
37+
super.onCreate(savedInstanceState);
38+
setContentView(R.layout.main);
5239

53-
@Override
54-
public boolean onDrag(View v, DragEvent event) {
55-
int action = event.getAction();
56-
switch (event.getAction()) {
57-
case DragEvent.ACTION_DRAG_STARTED:
58-
// Do nothing
59-
break;
60-
case DragEvent.ACTION_DRAG_ENTERED:
61-
v.setBackgroundDrawable(enterShape);
62-
break;
63-
case DragEvent.ACTION_DRAG_EXITED:
64-
v.setBackgroundDrawable(normalShape);
65-
break;
66-
case DragEvent.ACTION_DROP:
67-
// Dropped, reassign View to ViewGroup
68-
View view = (View) event.getLocalState();
69-
ViewGroup owner = (ViewGroup) view.getParent();
70-
owner.removeView(view);
71-
LinearLayout container = (LinearLayout) v;
72-
container.addView(view);
73-
view.setVisibility(View.VISIBLE);
74-
break;
75-
case DragEvent.ACTION_DRAG_ENDED:
76-
v.setBackgroundDrawable(normalShape);
77-
default:
78-
break;
79-
}
80-
return true;
40+
enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
41+
normalShape = getResources().getDrawable(R.drawable.shape);
42+
43+
findViewById(R.id.myimage1).setOnTouchListener(dragListener);
44+
findViewById(R.id.myimage2).setOnTouchListener(dragListener);
45+
findViewById(R.id.myimage3).setOnTouchListener(dragListener);
46+
findViewById(R.id.myimage4).setOnTouchListener(dragListener);
47+
48+
findViewById(R.id.topleft).setOnDragListener(this);
49+
findViewById(R.id.topright).setOnDragListener(this);
50+
findViewById(R.id.bottomleft).setOnDragListener(this);
51+
findViewById(R.id.bottomright).setOnDragListener(this);
52+
53+
}
54+
55+
@Override
56+
public boolean onDrag(View v, DragEvent event) {
57+
switch (event.getAction()) {
58+
case DragEvent.ACTION_DRAG_STARTED:
59+
// Do nothing
60+
break;
61+
case DragEvent.ACTION_DRAG_ENTERED:
62+
v.setBackground(enterShape);
63+
break;
64+
case DragEvent.ACTION_DRAG_EXITED:
65+
v.setBackground(normalShape);
66+
break;
67+
case DragEvent.ACTION_DROP:
68+
// view dropped, reassign the view to the new ViewGroup
69+
View view = (View) event.getLocalState();
70+
ViewGroup owner = (ViewGroup) view.getParent();
71+
owner.removeView(view);
72+
LinearLayout container = (LinearLayout) v;
73+
container.addView(view);
74+
view.setVisibility(View.VISIBLE);
75+
break;
76+
case DragEvent.ACTION_DRAG_ENDED:
77+
v.setBackground(normalShape);
78+
default:
79+
break;
8180
}
81+
return true;
8282
}
8383
}

0 commit comments

Comments
 (0)