Android - SPLessons

Android MultiTouch

Chapter 24

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Android MultiTouch

Android MultiTouch

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 .

Ex:

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;
        }
        });

Motion event class provides methods to handle touch events. Some of them are:

Create MainActivity.java under src/<your packagename>

MainActivity.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;
            }
        });

    }
}

Create activity_main.xml under res/layout folder

activity_main.xml:



    

    



AndroidManifest.xml:




    

    
        
            
                

                
            
        
    


Output: