I have file list in my android application. when the user double tap on a file I need to find out it. I could write the code to find out double tab action and X and Y coordinates of the user tap. But how can I find out the particular file or folder that the user did double tap?
This is the code what I used:
private View.OnTouchListener onTouchListener=new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
long thisTime=System.currentTimeMillis();
if(thisTime - lastTouchTime <500){
float x=event.getX();
float y=event.getY();
System.out.println("x :"+x);
System.out.println("y "+y);
}else{
lastTouchTime=thisTime;
}
}
return true;
}
};
I cannot find out it using icon sizes because they are not similar and user able to change it as they wish for icon image. Is there anyway to find out particular icon using X and Y coordinates of user tap?
Related
I have drawn piechart using canvas method of view , but now i want get click of individual pie ? how can i dot that?
I got perfect answer for this question:
get color code of click area and check if color match with your color code this will get click you want.
#Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
Logger.debug("X-->"+touchX+" Y---->"+touchY);
//get drawing cache of your view
Bitmap bitmap = getDrawingCache(true);
//Get color code of pixle where you have tap
int colorCode=bitmap.getPixel((int)touchX,(int)touchY);
if(colorCode == context.getResources().getColor(R.color.pie_blue)) {
Logger.debug("Color blue");
onPieClick.onBluePieClick(touchX,touchY);
}else if(colorCode == context.getResources().getColor(R.color.pie_green)) {
Logger.debug("Color green");
onPieClick.onGreenPieClick(touchX,touchY);
}
return super.onTouchEvent(event);
}
What you can do is,
Override onTouch event & You will get Motion event,
You will get x & y co-ordinates of the click by event.getX() &
event.getY() respectively.
identify where this x & y intersect in pie.
Sample code:
1)Simple
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP){
float xCord=event.getX();
float yCord = event.getY();
....
Write condition to identify where this x & y intersect in pie.
...
}
return true;
}
2) Another way getting touch (good way)
OnGestureListener mGestureListener=new GestureDetector.SimpleOnGestureListener(){
public boolean onSingleTapConfirmed(MotionEvent e) {
float xCord=e.getX();
float yCord = e.getY();
....
identify where this x & y intersect in pie.
...
};
};
GestureDetector gestureDetector=new GestureDetector(context, mGestureListener);
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
You can't. Well, at least not directly.
You could do the following though:
In the click handler for the view, determine the xy coordinates of the click
Compare the drawing code you wrote, thereby determining in which piece of the pie the click was
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 :))
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!
In Android tabs, how does one get the x,y coordinates of the currently selected tab? My goal is to automatically scroll to that tab (using scrollTo(x,y)) when it is selected in code.
You can find the x and y location of place where screen has been touched using this
main.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent e)
{
float x = e.getX();
float y = e.getY();
return true;
}
});
Hope it helps .
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.