In my app I draw an image covering the entire screen.
I want to now how can I know where the user touched the screen?
Thanks
Answered here: How do i get the x,y coordinate for a screen touch?
Here is a snippet of one of my games that does this. There are different ways to do it, but here I did it by subclassing View:
public class WorldView extends View {
...
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
Log.d(TAG,"touch event "+action+" x="+event.getX()+" y="+event.getY());
if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE)
{
int x = (int)event.getX();
int y = (int)event.getY();
Log.d(TAG,"setting target to "+x+","+y);
}
else
return super.onTouchEvent(event);
return true;
}
Related
Here is my Code:
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
int x=(int)event.getX();
if (action == MotionEvent.ACTION_DOWN) {
mLastX = x;
mStart = getScrollX();
} else if (action == MotionEvent.ACTION_MOVE) {
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
Log.d(TAG, "onTouchEvent: getScrollX-->"+getScrollX());
int dx=mLastX-x;
if(getScrollX()>=0&&getScrollX()<mSlideViewWidth){
scrollBy(dx,0);
}
mLastX=x;
} else if (action == MotionEvent.ACTION_UP) {
mEnd = getScrollX();
if(getScrollX()<0){
mScroller.startScroll(getScrollX(),0,-mEnd,0);
}
}
postInvalidate();
return true;
}
When I slide the screen to the left ,ScrollX is less than 0,so the scrollby method should not be called.But when I slide the screen to the left, ViewGroup can still move a short distance to the left.
I guess the solution might have something to do with the getScrollX method.
But because I've only recently learned Custom view,I hava no idea to deal with this.
I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.Thanks in advance.
Problem description gif
Its a animation problem, or you have an translate effect still going before the last scrollX OR probably your case, the dx value + getScrollX() in your last iteration is bigger than 0, your solution is to see if you near the edge and adjust dx to be the exact amount of -getScrollX() if it's bigger.
int dx=mLastX-x;
if (dx < -getScrollX()) dx = -getScrollX(); //not tested but should work
if(getScrollX()>=0&&getScrollX()<mSlideViewWidth)
scrollBy(dx,0);
I have been developing a function that allows users to draw a pattern (similar to the pattern unlock feature that comes with Android). I successfully implemented this function in an Activity using onTouchEvent():
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
// finish
}
if (event.getAction() == android.view.MotionEvent.ACTION_MOVE) {
// get drawing location
int x = (int) event.getX();
int y = (int) event.getY();
......
}
return super.onTouchEvent(event);
}
Now, I hope to do the same thing using a DialogFragment. However, a DialogFragment does not have a onTouchEvent Listener. Therefore, I cannot use the code implemented in an Activity.
Any ideas will be highly appreciated.
hey guys i try to get A popup menu when i click left click on the device i don't want open
popup menu by using button just want open when i click the left.
Like this :
http://ecee.colorado.edu/ecen3000/labs/lab8/files/javaLab1_files/image020.jpg
Thanks all...................
i tried
public void showMenu(View v) {
PopupMenu popup = new PopupMenu(this, v);
// This activity implements OnMenuItemClickListener
popup.setOnMenuItemClickListener((OnMenuItemClickListener) this);
popup.inflate(R.menu.actions);
popup.show();
}
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
default:
return false;
}
}
By touching left side of the screen you need to popup. My suggestion is first you have to add setOnTouchListener for the root layout you created in xml.
and by using the below override method u can get the touch event of x and y position where the
finger is placed on the screen, then restrict the range to corner as you need and compare the touch x and y, if it is between the range then popup your diaglog.
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
//finger placed down, this will run
}
else if (action == MotionEvent.ACTION_UP) {
//finger is up, this will run
}
else if (action == MotionEvent.ACTION_MOVE) {
//finger is moved, this will run
For clear understanding check this link (http://www.vogella.com/tutorials/AndroidTouch/article.html)
i'm using a SemiClosedSlidingDrawer (http://pastebin.com/FtVyrcEb) and i've added on content part some buttons on the top of slider which are always visibles.
The problems is that they are clickable (or click event is dispatched) only when slider is fully opened... When slider is "semi-opened" click event not seems dispached to button... I have inspected with debugger into onInterceptTouchEvent() and in both cases (opened/semi-collapsed) the following code
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (mLocked) {
return false;
}
final int action = event.getAction();
float x = event.getX();
float y = event.getY();
final Rect frame = mFrame;
final View handle = mHandle;
handle.getHitRect(frame);
//FOLLOWING THE CRITICAL CODE
if (!mTracking && !frame.contains((int) x, (int) y)) {
return false;
}
return false but only when slider is opened event was dispached...
It checks if a (x,y) relative to the click are contained in a rectangle created starting from the HandleButton view of sliding drawer...
final Rect frame = mFrame;
final View handle = mHandle;
handle.getHitRect(frame);
and this is obviously false because i'm clicking on a button contained inside the content part of slidingdrawer and that's ok...
As i said above the problem is that in semi-collapsed state, buttons contained in content part are not receiving the event...
Have you any idea how can i solve this issue?
Can be some state of slidingdrawer that avoid to click childs when collapsed?
Thanks in advance...
Right, I think I've figured out a way to do this.
First you need to modify onInterceptTouchEvent() to return true whenever the user presses the visible content during the semi-opened state. So, for instance, if your SemiClosedSlidingDrawer view is located at the very bottom of the screen, you can use a simple detection algorithm, like this:
public boolean onInterceptTouchEvent(MotionEvent event) {
...
handle.getHitRect(frame);
// NEW: Check if the user pressed on the "semi-open" content (below the handle):
if(!mTracking && (y >= frame.bottom) && action == MotionEvent.ACTION_DOWN) {
return true;
}
if (!mTracking && !frame.contains((int) x, (int) y)) {
...
}
Now the touch events during the user's interaction with the semi-opened content will be dispatched to onTouchEvent(). Now we just need to intercept these events and "manually" redirect them to the right view (note that we also need to offset the coordinates for the child view):
public boolean onTouchEvent(MotionEvent event) {
...
if (mTracking) {
...
}
else
{
// NEW: Dispatch events to the "semi-open" view:
final Rect frame = mFrame;
final View handle = mHandle;
handle.getHitRect(frame);
float x = event.getX();
float y = event.getY() - frame.bottom;
MotionEvent newEvent = MotionEvent.obtain(event);
newEvent.setLocation(x, y);
return mContent.dispatchTouchEvent(newEvent);
}
return mTracking || mAnimating || super.onTouchEvent(event);
}
It's a bit of a messy implementation, but I think the basic concept is right. Let me know how it works for you!
converted android seek bar to lock slider but how to disable progress change when user single tap on the Seekbar in Android
simply read the event type and if the event is of type drag move the slider
#Override
public boolean onTouchEvent(MotionEvent event)
{
int x = (int)event.getX(); // get the X position of the drag event
int y = (int)event.getY(); // get the Y position of the drag event
if(event.getAction() == MotionEvent.ACTION_MOVE){
slider.x = ... //move slider
}
...
return true;
}
hope this helps ! :)