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();