Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid difference in pixel postion
    primarykey
    data
    text
    <p>My MotionEvent and my layout seem to give differing location values and I cannot figure out why. I define an image as follows:</p> <pre><code>&lt;ImageView android:id="@+id/pic" android:layout_width="17dp" android:layout_height="17dp" android:layout_marginLeft="72dp" android:layout_marginTop="72dp" android:src="@drawable/pic" /&gt; </code></pre> <p>This places the image at (144, 144) in pixels on the emulator I'm testing with because it has a density of 2.0. The pixel location is confirmed with log.v and the parent of the image is simply the layout.</p> <p>I have an onTouchListener and in a MotionEvent I check the location of the touch:</p> <pre><code>Log.v("pixelsX", String.valueOf(event.getX())); Log.v("pixelsY", String.valueOf(event.getY())); </code></pre> <p>When I click well above and to the left of the image I set in the XML I get values of 160 and up, which is clearly wrong. That would correspond to a DP location of 80+ even though the click is well before the image. </p> <p>If I try and add an image at a click location image.setX(event.getX()) (tried using setX/setY and params) the image gets placed ~40 pixels below and to the right of the click. There is no extra padding or anything to the image, and I've tried using different images and it is consistent between them. The parents of the images are always the layout and the layout is connected to the ontouchlistener:</p> <pre><code>RelativeLayout layout = (RelativeLayout)findViewById(R.id.activity_mine); layout.setOnTouchListener(touch); </code></pre> <p>So I cannot figure out why there is this difference in location. Any ideas or suggestions?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload