Thursday, March 29, 2012

Simple Drag n Drop on Android


Simple Drag n Drop on Android

Till now, there is no Drag and Drop like control on the Android. This feature might come in handy in a few situations to improve the usability of your apps. Here is a simple Drag and Drop app which allows you to drag a button and drop it anywhere on the screen.
Theory: Go for a FrameLayout.
FrameLayout is designed to block out an area on the screen to display a single item. You can add multiple children to a FrameLayout, but all children are pegged to the top left of the screen. Children are drawn in a stack, with the most recently added child on top.
In our main layout, we have a single button. We write a touch listener which will track the touch events and also help us to move the button around the screen. In this example, we will not be moving the button, but it’s image which we set to a ImageView. Also, instead of actually moving the ImageView, we will change the padding of the ImageView as the mouse moves, which will give an impression of the ImageView being moved. 
12 

Home.java
--------------

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.FrameLayout.LayoutParams;

public class Home extends Activity implements OnTouchListener {

 private final static int START_DRAGGING = 0;
 private final static int STOP_DRAGGING = 1;

 private Button btn;
 private FrameLayout layout;
 private int status;
 private LayoutParams params;

 private ImageView image;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  layout = (FrameLayout) findViewById(R.id.LinearLayout01);
  // layout.setOnTouchListener(this);

  btn = (Button) findViewById(R.id.btn);
  btn.setDrawingCacheEnabled(true);
  btn.setOnTouchListener(this);

  params = new LayoutParams(LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT);

 }

 @Override
 public boolean onTouch(View view, MotionEvent me) {
  if (me.getAction() == MotionEvent.ACTION_DOWN) {
   status = START_DRAGGING;
   image = new ImageView(this);
   image.setImageBitmap(btn.getDrawingCache());
   layout.addView(image, params);
  }
  if (me.getAction() == MotionEvent.ACTION_UP) {
   status = STOP_DRAGGING;
   Log.i("Drag", "Stopped Dragging");
  } else if (me.getAction() == MotionEvent.ACTION_MOVE) {
   if (status == START_DRAGGING) {
    System.out.println("Dragging");
    image.setPadding((int) me.getRawX(), (int) me.getRawY(), 0, 0);
    image.invalidate();
   }
  }
  return false;
 }
}



mainxml
-------


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 android:gravity="center" android:id="@+id/LinearLayout01">

 <Button android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:id="@+id/btn"
  android:text="Drag Me"></Button>
</FrameLayout>

No comments: