In most Android devices, touch screen is the primary interface between user and device.
Touches can be interpreted by an application as a gesture. When more than one finger touches the screen at a time, it is known as Multi-touch gesture. Android allows you to detect gestures with Multi-touch feature. In this chapter, we will explain you how Android Multi-touch handles the multiple concurrent touches.
Android MultiTouch is added in API Level 1. The Android standard View class supports touch events. You can react to touch events in your custom views and activities. Android supports multiple pointers i.e. fingers which are interacting with the screen.
The base class for touch support is the MotionEvent class, which is passed to Views via the
onTouchEvent()
method. To react to the touch events, you should call
setOnTouchListener
on your View and override
the onTouchEvent().
The
MotionEvent class contains the touch related information such as the number of pointers, X/Y coordinates, size and the pressure of each pointer.
getAction()
method returns the action that was performed. The MotionEvent class provides the following constants to determine the action that was performed .
MotionEvent.ACTION_DOWN: New touch has started.
MotionEvent.ACTION_MOVE: Finger is moving.
MotionEvent.ACTION_UP: Finger went up.
MotionEvent.ACTION_CANCEL: Current event has been cancelled, something else took control of the touch event.
MotionEvent.ACTION_POINTER_DOWN: Pointer down (multi-touch).
MotionEvent.ACTION_POINTER_UP: Pointer up (multi-touch).
Ex:
[java]
linear.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
int event=arg1.getAction();
switch(event)
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
}
return true;
}
});
[/java]
Motion event class provides methods to handle touch events. Some of them are:
• getAction(): This method returns the action that was performed.
• getPressure(): This method returns the pressure of the current event for the first index.
• getRawX(): This method returns the original X-co-ordinate of the event.
• getRawY(): This method returns the original Y-co-ordinate of the event.
• getSize(): This method returns the size for the first pointer index.
• getSource(): This method gets the source of the event.
Create MainActivity.java under src/<your packagename>
MainActivity.java:
[java]
public class MainActivity extends Activity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set activity content to external view
setContentView(R.layout.activity_main);
context=this;
//find views by Id
LinearLayout linear=(LinearLayout)findViewById(R.id.linear);
final TextView action=(TextView)findViewById(R.id.textView1);
//on touch layout(screen)
linear.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
int event=arg1.getAction();
switch(event)
{
case MotionEvent.ACTION_DOWN:
System.out.println("dowm");
action.setText("Down");
break;
case MotionEvent.ACTION_UP:
System.out.println("up");
action.setText("Up");
break;
case MotionEvent.ACTION_MOVE:
System.out.println("move");
action.setText("Move");
break;
}
return true;
}
});
}
}
[/java]
Create activity_main.xml under res/layout folder
activity_main.xml:
[xml]
[/xml]
AndroidManifest.xml:
[xml]
[/xml]
Output: