6

I have imageView in activity. How I can set position this imageView in my activity. I know how I can do this in xml file but I want to do this in activity, because I have onTouch method where I get coordinates where I clicked and I want to draw this images in this coordinates.

edi233
  • 3,511
  • 13
  • 56
  • 97
  • similar question: http://stackoverflow.com/questions/1660150/move-imageview-around-inside-relativelayout – blejzz Mar 29 '12 at 11:49

2 Answers2

10

@edi233--

I think you can get touch x & y so that you can calculate top margin & left margin. I think (0,0) will be left top corner so if you touch (100,75) you need to set margins of image view to top - 75 & left to 100

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

params.setMargins(100,75, 0,0);
// OR
params.topMargin= 100;

image.setLayoutParams(params);
Sandip Jadhav
  • 7,377
  • 8
  • 44
  • 76
  • 5
    "setMargins" is not available in all versions of LayoutParams. It is availble in RelativeLayout.LayoutParams. –  Mar 21 '13 at 03:06
3

use the View.layout(int, int, int, int) in order to set its position

Blackbelt
  • 156,034
  • 29
  • 297
  • 305