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
}
}
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'm new to android, and I've been trying for a while to find out how can I retrieve the coordinates of a continuous touch on the screen. for example have 2 vars (x,y) that update in real time as finger moves around. I got it how to find it when the touch is made, but I really don;t get it how to make it return the result after that , when the finger is moving.
I've been trying switch statements, while/for loops in different combination with ACTION_MOVE./ UP/ DOWN .. .still nothing.
I've found like the same question on the website, but the answers only fit for the first step(showing the coordination from the touch only)
I'd really appreciate a solution to this! Thanks!
Without seeing your code I'm just guessing, but essentially if you don't return true to the first call to onTouchEvent, you won't see any of the subsequent events in the gesture (MOVE, UP, etc).
Maybe that is your problem? Otherwise please put code sample up.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView xCoord = (TextView) findViewById(R.id.textView1);
final TextView yCoord = (TextView) findViewById(R.id.textView2);
final View touchView = findViewById(R.id.textView3);
touchView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
xCoord.setText(String.valueOf((int) event.getX()));
yCoord.setText(String.valueOf((int) event.getY()));
break;
}
case MotionEvent.ACTION_MOVE:{
xCoord.setText(String.valueOf((int) event.getX()));
yCoord.setText(String.valueOf((int) event.getY()));
break;
}
}
return true;
}
});
}
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
// you can print the x and y coordinate in a textView for exemple
}
}
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, 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.
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