Reputation: 149
I am not able to understand why this code doesnot show any Toast. I have implemented OnGestureListener interface and trying to display some toast when user swipe over the activity. it means onFling method must be called but I am not receiving any toast. Please help me understand the problem.
import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.Toast;
public class MainActivity extends Activity implements OnGestureListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(this,"onDown",Toast.LENGTH_LONG).show();
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
Toast.makeText(this,"Swipe to Explore the Happiness Path",Toast.LENGTH_LONG).show();
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(this,"onLongPress",Toast.LENGTH_LONG).show();
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub`enter code here`
return false;
}
}
Upvotes: 1
Views: 3422
Reputation: 23638
A GestureDetector is an Android class that can take motion events, do some mathematical magic to determine what they are, and then delegate calls to a GestureListener object as specific gesture or other motion callbacks. The GestureListener object, a class we implement, receives these calls for specific gestures that the GestureDetector recognizes and allows us to react to them as we see fit (in this case, to move a graphic around within our PlayAreaView). Although the GestureDetector handles the detection of certain motions, it doesn’t do anything specific with them nor does it handle all types of gestures.
Try out as below add the GestureDetector and implement onTouch
event to detect the touch of screen :
public class MainActivity extends Activity implements OnGestureListener {
private GestureDetector gDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gDetector = new GestureDetector(this);
}
@Override
public boolean onTouchEvent(MotionEvent me) {
return gDetector.onTouchEvent(me);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(this,"onDown",Toast.LENGTH_LONG).show();
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
Toast.makeText(this,"Swipe to Explore the Happiness Path",Toast.LENGTH_LONG).show();
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(this,"onLongPress",Toast.LENGTH_LONG).show();
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub`enter code here`
return false;
}
}
To understand more GestureDetector
Upvotes: 2