MainActivity.java
package com.example.draganddropdemo;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity implements OnTouchListener {
private static final int START_DRAGGING = 0;
private static final int STOP_DRAGGING = 1;
private Button dragbtn1,dragbtn2;
private RelativeLayout layout;
private int status;
private ViewGroup.LayoutParams params;
private ImageView image;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("------------onCreate-----------------------------------");
layout = (RelativeLayout) findViewById(R.id.frameLayout1);
dragbtn1 = (Button) findViewById(R.id.drag_button);
dragbtn1.setDrawingCacheEnabled(true);
dragbtn1.setOnTouchListener(this);
//dragbtn2 = (Button) findViewById(R.id.drag_button2);
//dragbtn2.setDrawingCacheEnabled(true);
//dragbtn2.setOnTouchListener(this);
params = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
@SuppressLint("NewApi") public boolean onTouch(View v, MotionEvent event) {
System.out.println("------------onTouch-----------------------------------");
if( event.getAction() == MotionEvent.ACTION_DOWN ) {
status = START_DRAGGING;
image = new ImageView(this);
image.setImageBitmap(dragbtn1.getDrawingCache());
layout.addView(image,params);
}
if( event.getAction() == MotionEvent.ACTION_UP ) {
status = STOP_DRAGGING;
Log.i("Drag", "Stopped Dragging");
} else if( event.getAction() == MotionEvent.ACTION_MOVE ) {
if( status == START_DRAGGING ) {
System.out.println("--------------Dragging--------------");
image.setPadding((int)event.getRawX()-20,(int)event.getRawY()-40, 0, 0);
image.invalidate();
}
}
return false;
}
}
xml file - activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout1"
android:layout_width="300dp"
android:layout_height="600dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#008000" >
<Button
android:id="@+id/drag_button"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:background="#FF00FF"
android:text="DragDrop" />
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="134dp"
android:background="#A52A2A" >
</RelativeLayout>
<Button
android:id="@+id/drag_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/relativeLayout1"
android:text="DragButton2" />
</RelativeLayout>
Hey i am my very first comment on your site. , I have been reading your blog for a while and thought I would completely pop in and drop a friendly note. . It is great stuff indeed. I also wanted to ask..is there a way to subscribe to your site via email?
ReplyDeleteHR Consultancies in Chennai
just via email subscribe options is available here.and join this site. whenever i update anything, you get an mail from this blog immed..
DeleteThank you for visitin blog.. keep watching blog..
bye.............