Get the co-ordinates of the screen in android application - android

I am developing an Android application and I need to handle various touch events.
Consider the following requirements:
If the user touches on the right side of the screen I want to display next screen.
If user touches on the left side of the screen I want to display previous screen.
On the whole I have about 25 screens.
How can I get the logical coordinates of the screen and trigger the required event?

You can use this method for getting screen X and Y coordinated:
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float xcoordinated =event.getX();
float ycoordinated =event.getY();
return super.onTouchEvent(event);
}

Override
public boolean onTouchEvent(MotionEvent event) {
float x =event.getRawX();
float y =event.getRawY();
return super.onTouchEvent(event);
}
Note : getX() and getY(), returns the position(x,y) of touch in relation with the View, getRawX() and getRawY() returns the position ( x,y ) in relation of the screen of your Android phone.

Related

MotionEvent.ACTION_DOWN in Android is too sensitive. This event is received even if the screen is simply touched for a moment

I have a layout(A table layout). Whenever the user performs a "down" gesture, a specific function needs to be called. However,the ontouchlistener captures the event immediately instead of waiting until the user performs a proper "pull down" gesture.
My code is :
homePageTableLayout.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if ((event.getAction() == MotionEvent.ACTION_DOWN)) {
startSyncData();
}
return true;
}
});
Currently, what's happening is that the startSyncData() function gets called immediately.
The end result should be somewhat similar to the commonly used "pulltorefresh" library for android but in this usecase, I don't have to update the view. I just have to post some data to the server.
I can't use the "pulltorefresh" library because it does not support "pulling" a tablelayout. I tried putting the tablelayout inside a scrollview but that only spoiled the UI.
You can try doing it in ACTION_MOVE or ACTION_UP - if you need it to be done exclusively on swipe down gesture, this should help, introduce four global floats: x, y, x1, y1, then in ACTION_DOWN:
x = event.getX();
y = event.getY();
ACTION_MOVE or ACTION_UP:
x1 = event.getX();
y1 = event.getY();
and then in ACTION_UP:
if (y1 > y) { //pointer moved down (y = 0 is at the top of the screen)
startSyncData();
}
Its not that easy. It works properly since you are catching MotionEvent.ACTION_DOWN. It doesn't mean a gesture like swipe down it means that user touched the screen (finger down, the touch or gesture just started). Now you need to catch MotionEvent.ACTION_UP (finger up, gesture or touch ends here) and decide if there was a gesture you need. http://developer.android.com/training/gestures/detector.html
http://developer.android.com/training/gestures/index.html
MotionEvent.ACTION_MOVE repeatedly gets called. So if you want startSyncData() to run after moving a certain distance, calculate how much you want the user to move before running the method.
The key is that ACTION_DOWN is not a downwards movement on the screen, but it means the user pressed down onto the screen.
homePageTableLayout.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if ((event.getAction() == MotionEvent.ACTION_DOWN)) {
startFlg = 1;
} else if ((event.getAction() == MotionEvent.ACTION_MOVE)) {
if (startFlg==1){
// Calculate distance moved by using event.getRawX() and event.getRawX() and THEN run your method
startSyncData();
}
}
return true;
}
});

Android gestures

I am new to android.I am making an app in which I want to get all the dynamic coordinates of scroll gesture. e.g if I draw a line I want to get all the coordinates of that line.How to obtain that?
I have tried to take the values in array like this:
#Override
public boolean onScroll(MotionEvent me1, MotionEvent me2, float distanceX,float distanceY) {
x =me1.getRawX()+me2.getRawX();
xval.add((int) x);
return true;
}
#Override
public boolean onTouchEvent(MotionEvent me) {
this.gDetector.onTouchEvent(me);
onScroll(me,me,x,y);
Intent i=new Intent();
i.putIntegerArrayListExtra("key0", xval );
setResult(RESULT_OK,i);
finish();
}
return gDetector.onTouchEvent(me);
This code is able to fetch only the first coordinate of the scroll gesture
You don't get all the coordinates of the line, its inefficient. Instead you use the mathematical formula for distance from a point to a line, and see if you're close. If you're close enough (never expect the user to tap exactly on a line), then that point counts as on the line.
http://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line

How to disable onTouch of some potion in view

I'm drawing an rectangle surface by using canvas and i want to disable onTouchEvent() of some part of area, example if you tap(Touch) right side of the rectangle it should not perform onTouchEvent(). can any one tell me How to do this. Thanks in advance.
You have to use Hit and Trial method and generate a formula to run your app on different screen size of devices. And check where the user clicked and if he clicked in the restricted region do nothing.
Like
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int x = (int)event.getX();
int y = (int)event.getY();
if (x<height-100 && y>width-100) {
// Do nothing
}
return super.onTouchEvent(event);
}else{
//Do what you want here
}

Setting onDragListener for my Map?

so I am using osmdroid map and am trying to set a listener when a user drags the map around like this.
mapView.setOnDragListener(new View.OnDragListener() {
public boolean onDrag(View v, DragEvent event) {
Log.i(PREFS_NAME, "X:" + String.valueOf(event.getX()));
Log.i(PREFS_NAME, "Y:" + String.valueOf(event.getY()));
return false;
}
});
Now, everything works fine, but when I put this code in program exits with an error.
Is this the right thing to use anyway as the reason for this is that I want to get notified whenever a user moves around the map. I want to be able to check where user moves the map, hence the getX and getY I am watching. The reason for this is that I can stop the map movement if it goes out of bounds I set.
Is this the right way to do this?
MapViews don't respond to touch events like onClick() and onLongPress(). What I would do is before onCreate() add this:
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
float x = ev.getX();
float y = ev.getY();
return super.dispatchTouchEvent(ev);
}
x and y will now contain the screen coordinates that the user pressed. Hope this helps!

Android: Measure/detect covered area by a finger touch on screen (NOT only touch coordinates)

I would like to get access to the area covered by a finger for each touch event on an Android.
Every touch event will result in a coordinate pair X and Y independent of how big the finger and consequently the touch area is that triggered the event.
I was wondering if there is a way to get the area data which triggered the touch event e.g. size or coordinates NOT
Any help is much appreciated.
Thanks in advance for you answer or redirects,
Christian
The method motionEvent.getSize() should give you what you want (but the level of accuracy may vary depending on the device's screen).
You need to implement OnGestureListener.
First of all, you need to register GestureDetector in onTouchEvent
#Override
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return true;
}
In onShowPress you will get starting points
#Override
public void onShowPress(MotionEvent e) {
startX = e.getX();
startY = e.getY();
}
In onScroll you will get the end points.
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
endX = e2.getX();
endY = e2.getY();
}

Categories

Resources