Get the Co-Ordinates of a Touch Event on Android

How to get the Touch position in android?

@Override
public boolean onTouchEvent(MotionEvent event)
{
int x = (int)event.getX();
int y = (int)event.getY();

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}

return false;
}

The three cases are so that you can react to different types of events, in this example tapping or dragging or lifting the finger again.

How to get the absolut position of a touch event?

You can use event.getRawX() and event.getRawY() to get position relative to screen.

Get real x and y coordinates of users touch

I use this code to get the actually axis of touch on screen:

PointF p = new PointF(event.getX(), event.getY());
View v = this;// your view
View root = v.getRootView();
while (v.getParent() instanceof View && v.getParent() != root) {
p.y += v.getTop();
p.x += v.getLeft();
v = (View) v.getParent();
}

Hope this works for you as well

How can I get the x/y location of a touch relative to the entire view when I touch a button?

To get the touch position relative to the view add the touch event's (x,y) to your button's (x,y); to get the button's position relative to it's parent (your view) use the getLeft() and getTop() methods.

The documentation for View has the following under Position:

It is possible to retrieve the location of a view by invoking the
methods getLeft() and getTop(). The former returns the left, or X,
coordinate of the rectangle representing the view. The latter returns
the top, or Y, coordinate of the rectangle representing the view.
These methods both return the location of the view relative to its
parent. For instance, when getLeft() returns 20, that means the view
is located 20 pixels to the right of the left edge of its direct
parent.

Example code for your onTouch method:

float screenX = v.getLeft() + event.getX();   // X in Screen Coordinates
float screenY = v.getTop() + event.getY(); // Y in Screen Coordinates

Get the co-ordinates of a touch event on Android

You can use this function : http://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener)

You will probably put it in your onCreate method roughly this way (tested this time) :

Activity onCreate code

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textView = (TextView)findViewById(R.id.textView);
// this is the view on which you will listen for touch events
final View touchView = findViewById(R.id.touchView);
touchView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
return true;
}
});
}

layout code

<?xml version="1.0" encoding="utf-8"?>                                      
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/touchView"
android:background="#f00"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, TestActivity"
/>
</LinearLayout>

Edit: I tried my code and indeed there were a few errors. Anyway, with the layout I give you here it works on my emulator. Can you provide maybe more code/context so I can see what's wrong?

How to get Coordinates on touch Event on bitmap not of Screen

You can't get the coordinates of the bitmap directly. You need to calculate to yourself.
Use the position of the ImageView, and with that you can handle it.

Of course, when you are after Activity onCreate you can acces to any inflated (active) views parameter.
Exemple.
ImageView a;
a.getPaddingBottom();
Like all coordinats (left right etc...)
After this you need the Height and Width of the ImageView.
Nah, when you know the views position, hegiht and width you can calculate.

Example:

final ImageView iv_YourImage = (ImageView) findViewById(R.id.iv_imageview);
public boolean onTouch(View v, MotionEvent event){
int topParam = iv_YourImage.getPaddingTop();
int rightParam = iv_YourImage.getPaddingRight();
int maxTopParam = topParam+iv_YourImage.getMaxHeight();
int maxRightParam = rightParam + iv_YourImage.getMaxWidth();
if(event.getX>topParam&&event.getX<maxTopParam){
//the x coordinate is in your image... do the same to Y
}
return true;
}


Related Topics



Leave a reply



Submit