Double tap in an activity extends view - java

i'm working on android app for painting and i want when i do double tap a bitmap appear in the view. here i do some debug but i don't know why it's not working.
Actually this class implements another class TouchEventView which contain Touchevent and motion event.
i have tried also implements double tap in the other class but it's not working as well
public class PlotActivity extends AppCompatActivity implements
GestureDetector.OnDoubleTapListener,GestureDetector.OnGestureListener{
private TouchEventView tev;
private TextView message;
private GestureDetectorCompat gestureDetector;
public static final String DEBUGTAG="JWP";
Button enreg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_plot);
tev = (TouchEventView) findViewById(R.id.canva);
this.gestureDetector = new GestureDetectorCompat(this, this);
gestureDetector.setOnDoubleTapListener(this);
}
public void clearCanvas(View v){
tev.clearCanvas();
}
public void enregInfo(View v){
tev.savetofile();
}
#Override
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
Log.i(DEBUGTAG, "onLongPress: " );
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event){
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
#Override
public boolean onDoubleTap(MotionEvent motionEvent) {
Log.i(DEBUGTAG, "onLongPress: " );
return true;
}
#Override
public boolean onDoubleTapEvent(MotionEvent motionEvent) {
Log.i(DEBUGTAG, "onLongPress: " );
return true;
}
#Override
public boolean onDown(MotionEvent motionEvent) {
return true;
}
#Override
public void onShowPress(MotionEvent motionEvent) {
}
#Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return true;
}
#Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return true;
}
#Override
public void onLongPress(MotionEvent motionEvent) {
}
#Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return true;
}
}

Related

Android recyclerview item onTouchListener

I want to be able to receive touch events (down, move and maybe up) from my recyclerview items. Now it does not fire at all. Here is my code:
FragmentMain.java
noteRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(#NonNull RecyclerView rv, #NonNull MotionEvent e) {
return false;
}
#Override
public void onTouchEvent(#NonNull RecyclerView rv, #NonNull MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
child1 = rv.findChildViewUnder(e.getX(), e.getY());
Log.e("down", "aaa");
} else if (e.getAction() == MotionEvent.ACTION_MOVE) {
Log.e("move", "aaa");
}
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
How to solve that?
You have to place touchlistener in your adapter class in the onBindViewHolder method like this:
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.itemView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
// place your code here
Toast.makeText(getApplicationContext(), "Action Down", Toast.LENGTH_SHORT).show();
}
if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
// place your code here
Toast.makeText(getApplicationContext(), "Action Move", Toast.LENGTH_SHORT).show();
}
return false;
}
});
}

Android Development - How can GestureDetectorCompat class Object used for detecting gesture methods from GestureDetector class

I was going through some tutorials for detecting common gesture and saw the code given below. Here, GestureDetectorCompat Object is made to detect the gestures using the event handler SetOnDoubleTapListener. The main activity class implements two interfaces from GestureDetector class - OnGestureListener and OnDoubleTapListener. I got few question,
1) How came GestureDetectorCompat object is used to create the event handler SetOnDoubleTapListener for the callback methods inside of GestureDetector.OnGestureListener and GestureDetector.OnDoubleTapListener interfaces since GestureDetectorCompat and GestureDetector are two different class and how is it working fine with it.
2) Is SetOnDoubleTapListener Event Handler method can be used for all the callback methods inside the GestureDetector.OnGestureListener and GestureDetector.OnDoubleTapListener interfaces? If yes why they named it SetOnDoubleTapListener?
3) Why GesutureDetector class can't have just one interface consist of all the methods defined inside of GestureDetector.OnGestureListener and GestureDetector.OnDoubleTapListener interfaces.
4) Whats the difference between GestureDetector and GestureDetectorCompat? Why GestureDetectorCompat class don't have any interface defined.
Code :
public class MainActivity extends Activity implements
GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{
private static final String DEBUG_TAG = "Gestures";
private GestureDetectorCompat mDetector;
// Called when the activity is first created.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate the gesture detector with the
// application context and an implementation of
// GestureDetector.OnGestureListener
mDetector = new GestureDetectorCompat(this,this);
// Set the gesture detector as the double tap
// listener.
mDetector.setOnDoubleTapListener(this);
}
#Override
public boolean onTouchEvent(MotionEvent event){
this.mDetector.onTouchEvent(event);
// Be sure to call the superclass implementation
return super.onTouchEvent(event);
}
#Override
public boolean onDown(MotionEvent event) {
Log.d(DEBUG_TAG,"onDown: " + event.toString());
return true;
}
#Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());
return true;
}
#Override
public void onLongPress(MotionEvent event) {
Log.d(DEBUG_TAG, "onLongPress: " + event.toString());
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString());
return true;
}
#Override
public void onShowPress(MotionEvent event) {
Log.d(DEBUG_TAG, "onShowPress: " + event.toString());
}
#Override
public boolean onSingleTapUp(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString());
return true;
}
#Override
public boolean onDoubleTap(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
return true;
}
#Override
public boolean onDoubleTapEvent(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString());
return true;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());
return true;
}
}

Intercept click touch but not scroll in Android

I've searching through web and doc to intercept a click event and let super manage scroll in an Android application.
Eg : by default when I scroll on my view an another view moved with it. I'm able to get click event but at the moment of I get onDown returning true; the other view is not able to scroll with it. I've tried to return false on onScroll by it didn't make it.
I've both code with basic onTouchListener or GestureDetectorCompat doing the same stuff.
WIth Gesture Detector :
mGestureDetector = new GestureDetectorCompat(mContext, new ClickListener());
mRootFrameLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
});
class ClickListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onDown(MotionEvent e) {
Log.d(LOG_TAG, "onDown");
return true;
}
#Override
public void onShowPress(MotionEvent e) {
Log.d(LOG_TAG, "onShowPress");
}
#Override
public boolean onSingleTapUp(MotionEvent event) {
Log.d(LOG_TAG, "click");
return true;
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d(LOG_TAG, "onScroll");
return false;
}
#Override
public void onLongPress(MotionEvent e) {
Log.d(LOG_TAG, "onLongPress");
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d(LOG_TAG, "onFling");
return false;
}
}
Is there a way to defer event on a special view with DispatchTouchEvent or anything else ?

Java Android, attach guesture listener

I wrote Gesture Detector class and no try to add the gesture listener, it works fine when i call it in the onCreate() function for the activity_main view but i can't attach it to the invisible view with i create by button click.. Here is my code,
I receive no errors, it just don't listen, i can also set a onTouchListener() in the invisibleView class..
main
public class MainActivity extends Activity {
String TAG = "myTAG";
SocketClass mySocketClass;
View viewToWatch;
InvisibleView myInvisibleView;
Context myContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
savedInstanceState = new Bundle();
myInvisibleView = new InvisibleView();
/*
-----------------------------------------------------
this works, when i add the listener to the main view:
-----------------------------------------------------
viewToWatch= (View) findViewById(R.id.mainViewId);
viewToWatch.setOnTouchListener(new ClassGesturesDetection() {
public void returnSingleTapUp() {
Log.d ("Gesture from main View", "single tab");
}
});
-----------------------------------------------------
*/
}
public void startApp(View activity_main_view){
myInvisibleView.onCreate(this.getApplicationContext());
putOnTochListenerToInvsibleView();
}
public void putOnTochListenerToInvsibleView( ) {
/*
-----------------------------------------------------
there i want to add the listener:
-----------------------------------------------------
*/
setContentView(R.layout.invisibleviewxml);
viewToWatch= (View) findViewById(R.id.invisibleViewId);
viewToWatch.setOnTouchListener(new ClassGesturesDetection() {
public void returnSingleTapUp() {
Log.d ("Gesture from transparent view", "single tab");
}
});
}
}
here the class where i create the system overlay view:
public class InvisibleView extends Service {
View myView;
#Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate(Context myContext) {
super.onCreate();
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = new View(myContext);
myView = inflater.inflate(R.layout.invisibleviewxml, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) myContext.getSystemService(WINDOW_SERVICE);
wm.addView(myView, params);
/*
-----------------------------------------------------
this also works,the system overlay catches clicks
-----------------------------------------------------
myView.setOnTouchListener( new OnTouchListener() {
#Override
public boolean onTouch(View inviView, MotionEvent event) {
Log.d("Gesture", "simple touch from InvisibleView Class");
return true;
}
});
-----------------------------------------------------
this does not work
-----------------------------------------------------
myView.setOnTouchListener(new ClassGesturesDetection() {
public void returnSingleTapUp() {
Log.d ("Gesture from transparent", "single tab");
}
});
----------------------------------------------------- */
}
#Override
public void onDestroy() {
super.onDestroy();
if(myView != null)
{
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(myView);
myView = null;
}
}
}
here the my Class of Gesture Dedection:
public class ClassGesturesDetection implements OnTouchListener {
#SuppressWarnings("deprecation")
private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
public boolean onTouch(final View view, final MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
#Override
public boolean onDown(MotionEvent e) {
//Log.d("class Gesture", "on Down");
return true;
}
#Override
public void onLongPress(MotionEvent e) {
returnOnLongPress();
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
returnSingleTapUp();
return false;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
public void returnSingleTapUp() {
}
public void returnOnLongPress() {
}
}
you must add this permission
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
You can follow this
I had answered there for your question

How to use touch and double click on the image..?

I have one imageview, if i touch the imageview it to do some action else if i double click that imageview need to do some action.
How it is possible.?
First create a GestureDetector and a listener. Then bind it to your class that extends GestureDetector.SimpleOnGestureListener.
private GestureDetector detector;
private ImageView mImageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
detector = new GestureDetector(this, new MyGesturesListener());
// TODO find your image view
mImageView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});
}
Then you can implement the methods of different gestures:
class MyGesturesListener extends GestureDetector.SimpleOnGestureListener{
#Override
public boolean onSingleTapUp(MotionEvent ev) {
// TODO handle single tap
return true;
}
#Override
public boolean onDoubleTap(MotionEvent ev) {
// TODO handle double tap
return true;
}
}

Categories