Android Drag and drop of normal views (not bitmaps)

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Android Drag and drop of normal views (not bitmaps)

Postby royiby » Sun Apr 25, 2010 8:46 am

After a couple of times when i was requested to make a drag and drop UI for a client.
After countless hours of searching in which i came up with 20 examples of implementation by way of surface views and bitmaps (an implementation that i really didn't like), and all the while i see countless comments of frustrated people with the same problem as me, i decided to implement this my way and then release it back so that at least once this solution will be in public domain.
so, i hope this helps someone.

[syntax="java"]
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.Toast;
import android.widget.FrameLayout.LayoutParams;

public class dnd extends Activity
{
public FrameLayout board;
public View pawn;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

board = new FrameLayout(this);
pawn = new View(this);

setContentView(R.layout.main);

board = (FrameLayout)findViewById(R.id.Board);
pawn = findViewById(R.id.Pawn);
pawn.setOnTouchListener(dragt);


}//onCreate

OnTouchListener dragt = new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
FrameLayout.LayoutParams par = (LayoutParams) v.getLayoutParams();
switch(v.getId())
{//What is being touched
case R.id.Pawn:
{//Which action is being taken
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
{
par.topMargin = (int)event.getRawY() - (v.getHeight());
par.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
v.setLayoutParams(par);
break;
}//inner case MOVE
case MotionEvent.ACTION_UP:
{
par.height = 40;
par.width = 40;
par.topMargin = (int)event.getRawY() - (v.getHeight());
par.leftMargin = (int)event.getRawX() - (v.getWidth()/2);
v.setLayoutParams(par);
break;
}//inner case UP
case MotionEvent.ACTION_DOWN:
{
par.height = 60;
par.width = 60;
v.setLayoutParams(par);
break;
}//inner case UP
}//inner switch
break;
}//case pawn
}//switch
return true;
}//onTouch
};//dragt
}

[syntax="xml"]
<?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:id="@+id/Board" android:layout_gravity="top">

<View android:background="@drawable/icon"
android:layout_height="40dp"
android:layout_width="40dp"
android:id="@+id/Pawn" android:layout_gravity="top">
</View>
</FrameLayout>
Attachments
dnd.rar
(26.08 KiB) Downloaded 1556 times
royiby
Freshman
Freshman
 
Posts: 5
Joined: Sun Apr 25, 2010 8:33 am
Location: israel

Top

Re: Android Drag and drop of normal views (not bitmaps)

Postby itjunkii » Tue Jun 28, 2011 9:56 pm

YOU JUST SAVED MY LIFE, THANK YOU!!!!!!!!!!!!!!!!!!!!!!! Seriously hours and hours and hours of solutions that did not quite fit. This is exactly what I needed, THANK YOU!
itjunkii
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jun 28, 2011 8:53 pm

Re: Android Drag and drop of normal views (not bitmaps)

Postby Car Roof Racks » Thu Jun 30, 2011 1:02 pm

Thanks for sharing, It is very useful to all programmer who is working on Android.
Car Roof Racks
Freshman
Freshman
 
Posts: 6
Joined: Fri Jun 17, 2011 12:44 pm

Re: Android Drag and drop of normal views (not bitmaps)

Postby ramesh.kec.85 » Mon Aug 01, 2011 6:12 am

Thank you for sharing...
Thanks

Regards
--R@mesh
ramesh.kec.85
Developer
Developer
 
Posts: 27
Joined: Sat Jul 30, 2011 10:05 am
Location: Ahmedabad

Re: Android Drag and drop of normal views (not bitmaps)

Postby ramprasadiv » Tue May 01, 2012 10:43 pm

Need help. The idea that u gave is great thanks for the effort.
I learnt the art of Dragging :-).

Now i got stuck into problem. I think you are the right person to help me.
Problem Space:
I need to have a toolbar which has some images in it. Once the user clicks on any of the image i need to create a new image along the dragging path of the finger and start dragging into a new layer.
Since the toolbar will have a immovable images, we cannot move them out of the layer. Now the images will only be allowed to be dragged in the layer below.

I tried using ur code with various methods i could not achieve that.
Please help.
ramprasadiv
Once Poster
Once Poster
 
Posts: 1
Joined: Tue May 01, 2012 10:24 pm

Re: Android Drag and drop of normal views (not bitmaps)

Postby daavidfischer » Fri Nov 30, 2012 1:01 pm

I am also trying to learn to make a drag and drop UI for a client. I was so frustrated with how difficult it was to create so many codes and then fix errors. I am a novice used so it was obvious. This helped me a lot for the project.



__________
David Fischer
visit
daavidfischer
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri Oct 12, 2012 8:00 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests