Hi , I am new in android development , I have created a button and trying
to drag and drop to any position on the tablet screen , but the view drop
option is never called .Whenever the i drop the button any position it go
back to it's original position.
*activity_main.xml*
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start" />
</AbsoluteLayout>
*MainActivity.java*
public class MainActivity extends Activity {
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnTouchListener(new MyTouchListener());
button1.setOnDragListener(new MyDragListener());
}
private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("DDDDDD", "FFFFFF");
DragShadowBuilder shadowBuilder = new
View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.VISIBLE);
return true;
} else {
return false;
}
}
}
class MyDragListener implements OnDragListener {
Drawable enterShape =
getResources().getDrawable(R.drawable.shape_droptarget);
Drawable normalShape =
getResources().getDrawable(R.drawable.shape);
@SuppressWarnings("deprecation")
public boolean onDrag(View v, DragEvent event) {
Log.v("ABHRA"," With in onDrag");
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
v.setBackgroundColor(Color.YELLOW);
break;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackgroundColor(Color.GREEN);
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackgroundColor(Color.MAGENTA);
break;
case DragEvent.ACTION_DROP:
Float x = event.getX(); Float y = event.getY();
v.setX(x);v.setY(y);
v.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
v.setBackgroundDrawable(normalShape);
default:
break;
}
return true;
}
}
}
Please Help
Thanks Abhra
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en