onTouch method does not work - android

I am using onTouch method for doing some works. At first step, I want to show currentX value. However, my code does not work at all. How I resolve this issue?
public boolean onTouch(View view, MotionEvent event) {
int currentX = (int) event.getX();
if(event.getAction() == MotionEvent.ACTION_MOVE) {
Log.d("mesage:",currentX+"");
}
return true;
}

check out this link:
Detecting Common Gestures
You have there snippet like this:
View myView = findViewById(R.id.my_view);
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// your stuff
return true;
}
});
do you register your listener for a view? is it separated listener or implemented in Activity or other? is it MotionEventCompat or built in? we need more code...
but you said that you need to recognize "fingers to the right side". look in my link for gestures and "Detecting All Supported Gestures" or "Detecting a Subset of Supported Gestures". onFling method is what you want (right/left you can regonize by velocityX>0 or <0)
edit:
in your activity_main you should have more views. you also may set id for main viewGroup (LinearLayout/RelativeLayout?), which is in whole Activity (Screen) and get this view in findViewById, then setOnTouchLister for this particular view. but like I said above probably you need Gesture

Related

How to prevent multi-taps in android apps

I use onTouch as it is the best and more modifiable than for an instance onClick. It is a custom view that is being touched that I want to respond with action, and I have allready limited so you can't just touch and it does it over and over. Now I need it to limit the amount of simountanious taps.
EDIT:
#Override
public boolean onTouch(View v, MotionEvent event) {
lines++;
return false;
}
I have prevented constant action, but not the possibility to press with more than 1 finger, so that if you tap with more the rest of the fingers get ignored
There are several ways to do it.
best for you may be limit it inside in your overrided onTouch event.
if(event.getPointerCount() > 1) {
System.out.println("Multitouch detected!");
return true;
}
else
return super.onTouchEvent(event);
Another option is set attribute android:splitMotionEvents = false in your xml file.

Android remove onInterceptTouchEvent when tapping

Hello right now I'm developing a custom view where there'll be 2 listview that the header can be stacked each other like this picture :
||
||
\/
I already succeed to create this view, by overriding overscrolled and intercept all touch and playing with the margin of the header contents. but the problem now is because I intercept all touch now I can't tap the content of the listview, I know the flow of the touch is from ACTION_DOWN -> ACTION_MOVE -> ACTION_UP, and then when I dispatch the event to the child the tap is working but the scroll isn't working at all.
So for now the flow for my view is like this onInterceptTouchEvent -> onTouch -> gestureListener (or with overscroll)
this is some part of my code
#Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
onTouchEvent(ev);
return true;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
topListViewHeight = lvFirstListView.getTotalHeight() - rlFirstHeader.getHeight();
if(isBottomScrollable) {
listGestureDetector.onTouchEvent(event);
lvSecondListView.dispatchTouchEvent(event);
} else {
gestureDetector.onTouchEvent(event);
}
return true;
}
Is there anybody here have an idea about this? Any suggestion is very appreciated and if you need more question about this question please ask me.
Thanks before

Android onTouch view returning null

in my onTouch(View view, Motionevent event) method, the view.getY() always returns the same (0.0). I want to check what view is being touched and compare it with int Y = event.getRawY(). Would something like this be possible?
The reason is that I have a couple of animated fields in my layout, and I would like to animate each view differently depending on which one is being touched. I will appreciate any advice or ideas. Thank you in advance
check api reference first. see View.getY and Motionevent.getRawY.
I did this to solve my problem:
LinearLayout animLayout = new LinearLayout(this);
animLayout.addView(//add some view);
animLayout.setId(1)
animLayout.setOnTouchListener(this);
#Override
public boolean onTouch(View view, MotionEvent event){
case MotionEvent.ACTION_UP
switch (view.getId()}
case 1:
//My code
break;
}
break;
}
}
Basically, give my view an ID and in my onTouch get the ID in a switch case and do what I want with it.

How to unsubscribe from onTouch() event

When application is started I run a custom pop-up till a user touches the screen. When screen is touched I catch it with event onTouch() and cancel the pop-up. From this point I don't need the event anymore.
The problem is the event is alive and continues to jump up every time a user touches the screen.
Is there any way to unsubscribe from this event? Something like in c# -= eventName.
The code is below:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!_stopToast)
{
_hintToast.cancel();
_stopToast = true;
}
return false;
}
There's no such method (lets say removeTouchListener or similar) which will help you to remove an already defined touch listener from a view. Setting null to setOnTouchListener won't help too. What you can do is to create a new object reference of OnTouchListener class which does nothing and set it in setOnTouchListener. For example:
public final OnTouchListener dummyOnTouchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent rawEvent) {
return false;
}
};
And simply use it as below:
yourView.setOnTouchListener(dummyOnTouchListener);

two views ontouchlistener same area how to?

I have a two ImageViews.
1.ImageView zoomImageMoveable : should zoom in,out and moveable.
2.ImageView zoomImageFixed : show zoom this is guide line.
I want run different functions from one touch.
{
FrameLayout myFrame = new FrameLayout(this.getContext());
myFrame.addView(zoomImageMoveable);
myFrame.addView(zoomImageFixed);
}
Case 1. I tried to attach a listener to each of the view, as they are overlapping only one works.
Case 2. I also tried to wrap the two view with a FrameLayout and added an OnTouchListener to that layout to have it forward the calls to each of the assigned methods. Yet this method also didn't work.
myFrame.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
zoomImageMoveable.onTouchEvent(event);
zoomImageFixed.onTouchEvent(event);
return false;
}
});
How should I approach this issue?

Categories

Resources