I'm trying to add an ImageView in certain coordinates on the screen, but for some reason the ImageView is placed below and to the side by 20-40 pixels ...

Here is the code to add:

@Override public boolean onTouchEvent(MotionEvent event) { int x = (int)event.getX(); int y = (int)event.getY(); ImageView iv = new ImageView(this); iv.setImageResource(R.drawable.black_background); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); params.leftMargin = x; params.topMargin = y; rootView.addView(iv, params); return false; } 

Here is the xml markup:

 <RelativeLayout android:id="@+id/rootView" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <TableLayout android:id="@+id/tl1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/ll" android:gravity="center"/> </RelativeLayout> 

How to place the imageview in the coordinates I need?

UPD: Experimentally, it was established that ImageView goes down about 155 pixels.

He took away the size of the action bar, LinearLayout - the distance was smaller, but still 60 pixels in height is not that.

 params.topMargin = y-actionBarHeight-(int)linearLayout.getX(); 
  • padding container account? - Eugene Krivenja
  • Maybe you need to consider the height of ActionBar? - Ruslan A
  • Padding does not take into account. But it's not about him. - researcher
  • @RuslanA is unlikely. - researcher
  • 2
    What is happening and what is expected? Do you want to place an ImageView on the finger, so that its upper left corner is in the place of the wheelbarrow? - anber

1 answer 1

 @Override public boolean onTouchEvent(MotionEvent event) { int x = (int)event.getX(); int y = (int)event.getY(); ImageView iv = new ImageView(this); iv.setImageResource(R.drawable.black_background); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); //Так можно расчитать отступы int[] rootLocation = new int[2]; rootView.getLocationOnScreen(rootLocation); int relativeLeft = rootLocation[0]; int relativeTop = rootLocation[1]; //end params.leftMargin = x - relativeLeft;//поправка на отступы params.topMargin = y - relativeTop;//поправка на отступы rootView.addView(iv, params); return false; } 
  • one
    @Andrew, yes, exactly. Corrected. - Vladyslav Matviienko
  • 2
    @Andrew, it happens when you do something on your knee :-) - Vladyslav Matviienko
  • Everyting is ok. Thanks! You have solved the problem! - researcher
  • Uraa) You solved the global problem (for me)!) - researcher