How can I drag and drop Views within Android RelativeLayout
android, android-layout, drag-and-drop
Solution
I faced the same trouble a while ago, if you do not want to drag other views (either below or above or beside your view) along with the view being dragged, then use `ViewGroup.LayoutParams` instead of `RelativeLayout.LayoutParams`, the latter is imported if you hit Ctrl-Shift-O, so change it manually to the former.
Edit: Since you wanted the description of how I achieved that, here is the actual code
MarginLayoutParams marginParams = new MarginLayoutParams(image.getLayoutParams());
int left = (int) event.getRawX() - (v.getWidth() / 2);
int top = (int) event.getRawY() - (v.getHeight());
marginParams.setMargins(left, top, 0, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
image.setLayoutParams(layoutParams);
I had to do a lot of research to achieve the same and it took me 2 days to figure out that a thing called MarginLayoutParams exists...
Make sure that you have
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
as your imports and not `RelativeLayout.LayoutParams`.
Also make sure that you cast marginparams to your relative layout Best of luck...
Problem
I made my own drag and drop functionality. I wanted to do this with a `RelativeLayout`, but this seems not to work very well. At the beginning i can tell each `View` I want to drag on which position it should be by setting it below the `View` before. But when I want to drag a View, all the other Views beneath it are moved too, because they want to keep the order in which I set them. But what other layout can I use for this? I have to use this drag and drop functionality on different screen sizes, so I cant' give them fixed `x` and `y` values.