package com.example.motionevent;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MotionEventActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_motion_event);
		RelativeLayout myLayout = 
                (RelativeLayout)findViewById(R.id.RelativeLayout1);
        
            myLayout.setOnTouchListener(
        		new RelativeLayout.OnTouchListener() {
        			public boolean onTouch(View v, MotionEvent m) {
        				handleTouch(m);     				
        			    return true;
        			}
        		}
        );

	}
	void handleTouch(MotionEvent m)
	{
	    	TextView textView1 = (TextView)findViewById(R.id.textView1);
	    	TextView textView2 = (TextView)findViewById(R.id.textView2);
	    	
	    	int pointerCount = m.getPointerCount();
	    	
	    	for (int i = 0; i < pointerCount; i++)
	    	{
	    		int x = (int) m.getX(i);
	    		int y = (int) m.getY(i);    		
	    		int id = m.getPointerId(i);
	    		int action = m.getActionMasked();
	    		int actionIndex = m.getActionIndex();
	    		String actionString;
	    		
	    		
	    		switch (action)
	    		{
	    			case MotionEvent.ACTION_DOWN:
	    				actionString = "DOWN";
	    				break;
	    			case MotionEvent.ACTION_UP:
	    				actionString = "UP";
	    				break;	
	    			case MotionEvent.ACTION_POINTER_DOWN:
	    				actionString = "PNTR DOWN";
	    				break;
	    			case MotionEvent.ACTION_POINTER_UP:
	        			actionString = "PNTR UP";
	        			break;
	    			case MotionEvent.ACTION_MOVE:
	    				actionString = "MOVE";
	    				break;
	    			default:
	    				actionString = "";
	    		}
	    		
	    		String touchStatus = "Action: " + actionString + " Index: " + actionIndex + " ID: " + id + " X: " + x + " Y: " + y;
	    		
	    		if (id == 0)
	    			textView1.setText(touchStatus);
	    		else 
	    			textView2.setText(touchStatus);
	    	}
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.motion_event, menu);
		return true;
	}

}
