In android can listen to the gestures and events performed by user on screen.
To listen the Gestures in Android we need to do 3 things
1: Create Class GestureListener which should extends GestureDetector.SimpleOnGestureListener
2: Override s all the callback methods of GestureDetector.SimpleOnGestureListener
3: Bind the gestureDetector to GestureListener
class GestureListener extends GestureDetector.SimpleOnGestureListener
{
static String currentGestureDetected;
// Override s all the callback methods of GestureDetector.SimpleOnGestureListener
@Override
public boolean onSingleTapUp(MotionEvent ev) {
currentGestureDetected=ev.toString();
return true;
}
@Override
public void onShowPress(MotionEvent ev) {
currentGestureDetected=ev.toString();
}
@Override
public void onLongPress(MotionEvent ev) {
currentGestureDetected=ev.toString();
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
currentGestureDetected=e1.toString()+ " "+e2.toString();
return true;
}
@Override
public boolean onDown(MotionEvent ev) {
currentGestureDetected=ev.toString();
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
currentGestureDetected=e1.toString()+ " "+e2.toString();
return true;
}
}
MainActivity.java
public class MainActivity extends Activity
{
private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Bind the gestureDetector to GestureListener
mGestureDetector = new GestureDetector(this, new GestureListener());
}
// onTouch() method gets called each time you perform any touch event with screen
@Override
public boolean onTouchEvent(MotionEvent event)
{
//method onTouchEvent of GestureDetector class Analyzes the given motion event
//and if applicable triggers the appropriate callbacks on the GestureDetector.OnGestureListener supplied.
//Returns true if the GestureDetector.OnGestureListener consumed the event, else false.
boolean eventConsumed=mGestureDetector.onTouchEvent(event);
if (eventConsumed)
{
Toast.makeText(this,GestureListener.currentGestureDetected,Toast.LENGTH_LONG).show();
return true;
}
else
return false;
}
}
To listen the Gestures in Android we need to do 3 things
1: Create Class GestureListener which should extends GestureDetector.SimpleOnGestureListener
2: Override s all the callback methods of GestureDetector.SimpleOnGestureListener
3: Bind the gestureDetector to GestureListener
GestureListener.java
class GestureListener extends GestureDetector.SimpleOnGestureListener
{
static String currentGestureDetected;
// Override s all the callback methods of GestureDetector.SimpleOnGestureListener
@Override
public boolean onSingleTapUp(MotionEvent ev) {
currentGestureDetected=ev.toString();
return true;
}
@Override
public void onShowPress(MotionEvent ev) {
currentGestureDetected=ev.toString();
}
@Override
public void onLongPress(MotionEvent ev) {
currentGestureDetected=ev.toString();
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
currentGestureDetected=e1.toString()+ " "+e2.toString();
return true;
}
@Override
public boolean onDown(MotionEvent ev) {
currentGestureDetected=ev.toString();
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
currentGestureDetected=e1.toString()+ " "+e2.toString();
return true;
}
}
MainActivity.java
public class MainActivity extends Activity
{
private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Bind the gestureDetector to GestureListener
mGestureDetector = new GestureDetector(this, new GestureListener());
}
// onTouch() method gets called each time you perform any touch event with screen
@Override
public boolean onTouchEvent(MotionEvent event)
{
//method onTouchEvent of GestureDetector class Analyzes the given motion event
//and if applicable triggers the appropriate callbacks on the GestureDetector.OnGestureListener supplied.
//Returns true if the GestureDetector.OnGestureListener consumed the event, else false.
boolean eventConsumed=mGestureDetector.onTouchEvent(event);
if (eventConsumed)
{
Toast.makeText(this,GestureListener.currentGestureDetected,Toast.LENGTH_LONG).show();
return true;
}
else
return false;
}
}
No comments:
Post a Comment