Search code examples
androidandroid-layoutandroid-viewandroid-eventandroid-screen-support

Consistency of onTouch event.getX or Y on different screen sizes - Android


I'm implementing an OnTouchListener on a custom view. In this view I'm drawing an object that basically moves along x and y axis I get form event.getX(); and event.getY(); respectively.

I want to perform some draw changes when touch events happen at particular positions in screen. The problem is, the values are different for different screen sizes. For example value of X value of a point on a WVGA device is 60 and on tablets it is 100. Since this is inconsistent i couldn't write a proper if condition.

Please can any one suggest a solution for this from your prior experience?


Solution

  • I basically got good ideas with both answers, (my respect) But the solution that worked for me is below. The idea is from the clue by Chintan Raghwani.

    So lets say a position x value on a device screen is equal to the total view width multiplied by percentage

    Example: posX = getMeasuredWidth() * 0.5 will give you touch event handled at center. Positioning objects by this way will make it consistent through all devices.

    Hope it helps some one :)

    EDIT :

    Lucky enough to find this post. This is process above is also called as the Golden Ratio this is really a great post about this.