0

I've coded a Drag and drop behaviour for my Android application. So far it works perfectly on my HTC One and on my Samsung S3. Basically, it just sets the leftMargin and the topMargin on the view to move.

When I try it on a Xperia Arc S, I can set the topMargin and move the view vertically without any problem. But once I try to move it horizontaly (Setting the leftMargin), it doesn't work. I can't even set the leftMargin programatically.

I got a very simple code, but I can't find out what is going wrong on the Xperia and why it does work on the other devices I got.

params.topMargin = (int) event.getRawY() - view.getHeight();
params.leftMargin = (int) event.getRawX() - (view.getWidth() / 2);

// Set bounds
if (params.topMargin < 0) params.topMargin = 0;
if (params.topMargin + viewHeight * 3/2 > display.heightPixels) params.topMargin = display.heightPixels - viewHeight * 3/2;
if (params.leftMargin < 0) params.leftMargin = 0;
if (params.leftMargin + viewWidth > display.widthPixels) params.leftMargin = display.widthPixels - viewWidth;

view.setLayoutParams(params);

If I display the values of leftMargin, display.widthPixels and viewWidth, I got something which look good. (Eg. leftMargin = anything between -100 to 600 or so. widthPixels = 480 and viewWidth = 316 at the moment).

Is there something wrong with the Xperia or is there any error in my code ?

Thanks for your precious help.

EDIT: Solution was (Regarding Ved Prakash post)

FrameLayout.LayoutParams paramsOld = (FrameLayout.LayoutParams) view.getLayoutParams();
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(paramsOld.width, paramsOld.height, Gravity.TOP);

params.topMargin = (int) event.getRawY() - view.getHeight();
params.leftMargin = (int) event.getRawX() - (view.getWidth() / 2);

// Set bounds
if (params.topMargin < 0) params.topMargin = 0;
if (params.topMargin + viewHeight * 3/2 > display.heightPixels) params.topMargin = display.heightPixels - viewHeight * 3/2;
if (params.leftMargin < 0) params.leftMargin = 0;
if (params.leftMargin + viewWidth > display.widthPixels) params.leftMargin = display.widthPixels - viewWidth;

view.setLayoutParams(params);

view.invalidate();
Manitoba
  • 8,522
  • 11
  • 60
  • 122
  • Is padding an option over margin? If yes, you may try it. – kupsef May 11 '14 at 13:33
  • What do you mean ? The `view` is a simple `LinearLayout` with `wrap_content` for both `height` and `width`. There is no padding and I can see the element is only taking 25% of the screen width. Which is very disapointing, is that it works perfectly on my HTC but not on my Xperia. – Manitoba May 12 '14 at 14:01
  • Are you explicitly invalidating the view by calling invalidate() – C.d. May 13 '14 at 09:23

2 Answers2

2

Sony Xperia Arc S - Android 2.3

HTC One - Android 4.1

There is a problem in setting the layout with older versions of android.

Check the post: android-setting-left-right-margin-doesnt-work.

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
0

Android 4.4 has some problem with layout Margin. So better try:

view.setPadding(left, top, right, bottom);