Android and location of touch event - android

In Android, we can set OnClickListeners for views.
But how do we intercept the location of the touch event?

there is method in Activity
public boolean onTouchEvent(MotionEvent event){
int action = event.getAction();
int x = event.getX()
int y = event.getY();
return yourBoolean;
}
Edit: Or you can define OnTouchListener for any View and can use its method
public abstract boolean onTouch(View v, MotionEvent event);
Edit2: the x and y values depends upon the method call. If you use onTouchEvent() of Activity then it indicates that no View consumes the touch event see documentation and if you handle it on any View then the x and y will be according to the View's area.

You can set the onTouchListener for the same view. The MotionEvent will have the x and y.

Related

How to get onTouch position on a Button using a top View?

I have a custom View, which inherits from GestureOverlayView, and I want to log all the MotionEvent passed to this view.
It works well, but I can't get the MotionEvent when my gesture starts on an interactive layout widget (Button, TextEdit....)
Is there a way to bypass this behaviour?
Since you didn't mention any snipped code of yours,I make 2 different solution hopefully is help you
One way will be passing your widget to as View to method and call OnTouchListener
button.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
break;
}
case MotionEvent.ACTION_UP: {
break;
}
case MotionEvent.ACTION_CANCEL:{
break;
}
}
return false;
}
});
Another way could be manually creating a MotionEvent using custom constructor
like this:
static public CustomMotionEvent obtain(long downTime, long eventTime, int action,
float x, float y, int State) {
}
According to API
Create a new MotionEvent, filling in a subset of the basic motion
values. Those not specified here are: device id (always 0), pressure
and size (always 1), x and y precision (always 1), and edgeFlags
(always 0).
API

ImageView click best practice

I am a newbie in Andoird.
In my case, I have a scenario that when click certain part of an image it will trigger onclick events. I tried to detecte the position when the onTouch is fired, it works, but I think it's not a standard implementation, so what is the best practice for such case?
thanks.
here is codes like:
imgView.setOnTouchListener((OnTouchListener) new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
if(isIn(event.getX(), event.getY(), 124,3,221,36)){
ShowMemberInfo(R.string.app_m01);
} else if(isIn(event.getX(), event.getY(), 8,155,72,181)){
..
}
return true;
}
private boolean isIn(float x, float y, int fx, int fy, int tx, int ty) {
return x<tx && x > fx && y<ty && y>fy;
}
Try to use ImageButton
Simply implement onClickListener() for your ImageView.
Easiest way to implement onClick event is to include android:onClickMe="methodName" inside your <ImageView> in XML layout and define that method inside your activity file.
For example:
public void methodName(View v)
{
....
....
// do whatever you want for click even ton imageview
}
If you want to detect the position where user touched(Relative to the ImageView user touched), you can get the touch point from MotionEvent object.
Try to register a touch event listener for the ImageView and get the touched point position from MotionEvent object's getX() and getY() methods when touch event is triggered. And then define a rectangular area and using contains() to check whether the touch point is inside the area or not.
ImageView img = new ImageView(this);
img.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.w("Hit test", "is hit? "+isIn(event.getX(),event.getY(),0,0,120,120));
return false;
}
});
private boolean isIn(float x, float y, int fx, int fy, int tx, int ty){
return new Rect(fx,fy,tx,ty).contains((int)x,(int)y);
}
Hope this will be helpful to you. :)

Android touch in predefined position

I have an android code which starts vibrating for a random amount of time when the user touch a screen. Now I want to change it a bit. I want to ask the user to touch a predefined position, for example in the middle of screen. what should I do that only in some special coordinates touching become effective?
You can use a onTouchListener. The Listener gets a MotionEvent from which you can get th coordinates and check the position:
something.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// get coordinates from touch event
int x = (int) event.getX();
int y = (int) event.getY();
// check if the coordinates are in a specific area
return true;
}
});
To get the width and height you can use the following code:
Display d = getWindowManager().getDefaultDisplay();
width = d.getWidth();
height = d.getHeight();
onTouch(View v, MotionEvent event)
onTouch give you a MotionEvent, with this, you can simply do
event.getX();
event.getY();
for getting coordinates and do something with it (or not :))

Gesture - Show coordinates when finger motion

I need to create an Activity that while drag your finger across the screen, display the XY coordinates (where the finger goes). Could anyone help me?
OnTouch
You need to implement an OnTouchListener for whatever view you want to recognize the drag.
Then in the onTouchListener you need to display the X and Y coordinates. I believe you can get those via MotionEvent.getRawX() and MotionEvent.getRawY()
You can use the MotionEvent.getAction() method to find out when a drag is occurring. I believe the constant is MotionEvent.ACTION_MOVE. Here is some psuedo-code:
Add OnTouchListener interface
public class XYZ extends Activity implements OnTouchListener
Register the listener in the onCreate method
public void onCreate(Bundle savedInstanceState)
{
//other code
View onTouchView = findViewById(R.id.whatever_id);
onTouchView.setOnTouchListener(this);
}
Implement the onTouch method
public boolean onTouch(View view, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_MOVE)
{
float x = event.getRawX();
float y = event.getRawY();
// Code to display x and y go here
}
}

Android - Problem detecting onTouchListener

So I have a customdrawableview applied to my activity.
I'm trying to implement a motion listen to the view so that I can detect different touch events in different locations. However, I don't seem to even get a response from Touch Down.
Here's the relevant part of my code:
public class CustomDrawableView extends View implements OnTouchListener
{
public CustomDrawableView(Context context)
{
super(context);
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
mDrawBackGround(canvas);
mDrawHexPanel(canvas);
mDrawHuePanel(canvas);
mDrawGreyScaleHexPanel(canvas);
mDrawHuePointer(canvas);
}
#Override
public boolean onTouch(View CustomDrawableView, MotionEvent event)
{
float touchX = event.getX();
float touchY = event.getY();
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
pointerTouch=true;
cpRed=255;
cpGreen=108;
cpBlue=0;
invalidate();
break;
}
return true;
}
So what am I doing wrong?
To get multi-touch events, you should use the methods getX(int pointer) and getY(int pointer) which returns the position of each touch point.
You can know how many fingers are on screen with the method getPointerCount().
(Methods from the MotionEvent)
Also, the ACTION_DOWN are fired only when the finger touch for the first time, if it's drag, the next events are going to be ACTION_MOVE.
You are overriding onTouch(View arg0, MotionEvent arg1), but to listen the touch events from the View you are creating, you should override onTouchEvent(MotionEvent evt).
At the moment your class only implements the interface. You have to register the OnTouchListener to your view by calling this.setOnLongClickListener(this).
Add the listener registration in the constructor of the CustomDrawableView class

Categories

Resources