Android multitouch?
Asked Answered
T

3

6

As a developer, I tend to program first then research later. I was trying to implement a screen that would handle multiple user inputs. Basically map more than just one finger. I tried two things..

I had an Activity class that implemented OnTouchListener. Here I had two separate child views that added the Activity as their ontouch listeners. And then I had a print line that printed out the event.getPointerCount() and printed out the location of each of the pointers..

Downloaded it to the phone and tested it. I noticed that the pointers never grew greater than two. I even noticed if one finger was on one child and another finger on the other child view that the events only made it to the first child component. Next I tried to put separate listeners on each of the child components to see if that would work but it acted the same.

So, it seems that the android only supports a total of two user inputs when dealing with inputs from the user touching the screen.

Is this true? It seems from testing and research that this is the case.

Has anyone ran into this limitation. Any clever work arounds?

Thanks for you time and responses.

dk

Taxdeductible answered 17/1, 2011 at 3:31 Comment(0)
A
7

The number of touch points supported is dependent on the device. In all currently available versions of Android MotionEvents are sent to a single target view and describe the state of all currently active touch points. The target view is determined by where the first MotionEvent.ACTION_DOWN event occurs.

See the post Making Sense of Multitouch on the Android Developers blog for a quick overview of working with multitouch events on Android.

Apostle answered 17/1, 2011 at 4:30 Comment(1)
Thanks for the URL, it helped clear some things up. But I had to see it for myself. So, I wrote a simple app to test what was said in the article. Will add the code to self reply to this question.Taxdeductible
T
3

I wrote a very simple application to test multiple touch on my HTC Incredible phone. I behave pretty much like I thought and pretty much like the URL given by adamp. My results is that the phone could only handle two finger inputs. I have for box that each has its own OnTouchListener. When I click each box I print out a text for each. Clicking on each get touch events but with my index finger I clicked anywhere on the screen I got two pointers. From the MotionEvent event.getPointerCount method. Did not matter where the second finger landed. Could have been outside of the original view that received the first finger down event. Anyways here is the code if you like to try it yourself.

I have a 7z of the entire project but I do not think I can attach it.

So, here is the main activity and the xml for the layout.

package com.multitouch;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MultiTouch extends Activity {
    protected LinearLayout sq1;
    protected LinearLayout sq2;
    protected LinearLayout sq3;
    protected LinearLayout sq4;

    protected String sqText = new String();
    protected String sq1Text = new String();
    protected String sq2Text = new String();
    protected String sq3Text = new String();

    private Handler handler = new Handler();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setViews();
        handler.postDelayed(mUpdateUITimerTask, 5000);
    }

    private final Runnable mUpdateUITimerTask = new Runnable() {
        public void run() {
            // do whatever you want to change here, like:
            updateTextField();
        }
    };

    private void updateTextField() {
        TextView view1 = (TextView) this.findViewById(R.id.logView);
        TextView view2 = (TextView) this.findViewById(R.id.logView1);
        TextView view3 = (TextView) this.findViewById(R.id.logView2);
        TextView view4 = (TextView) this.findViewById(R.id.logView3);


        //view1.append(sqText + "\n");
        view1.setText(sqText);
        view2.setText(sq1Text);
        view3.setText(sq2Text);
        view4.setText(sq3Text);

        handler.post(mUpdateUITimerTask);
    }

    private void setViews() {
         sq1 = (LinearLayout) this.findViewById(R.id.square1);
         sq2 = (LinearLayout) this.findViewById(R.id.square2);
         sq3 = (LinearLayout) this.findViewById(R.id.square3);
         sq4 = (LinearLayout) this.findViewById(R.id.square4);

         sq1.setOnTouchListener(new TouchListener());
         sq2.setOnTouchListener(new TouchListener());
         sq3.setOnTouchListener(new TouchListener());
         sq4.setOnTouchListener(new TouchListener());        
    }

    private class TouchListener implements OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

             if(event.getPointerCount()>2) {
                sq1Text = "GOT THREE POINTERS!!";
                sq2Text = "GOT THREE POINTERS!!";
                sq3Text = "GOT THREE POINTERS!!";
                sqText = "GOT THREE POINTERS!!";
             }

             if(event.getAction() == MotionEvent.ACTION_DOWN){
                switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 Pressed::Pointer count = " + String.valueOf(event.getPointerCount()); 
                   break;
                   case R.id.square2:
                       sq1Text = "Square 2 Pressed::Pointer count = " + String.valueOf(event.getPointerCount()); 
                   break;
                   case R.id.square3:
                       sq2Text = "Square 3 Pressed::Pointer count = " + String.valueOf(event.getPointerCount()); 
                   break;
                   case R.id.square4:
                       sq3Text =  "Square 4 Pressed::Pointer count = " + String.valueOf(event.getPointerCount()); 
                   break;
                }
             }  else if(event.getAction() == MotionEvent.ACTION_POINTER_DOWN ||
                        event.getAction() == MotionEvent.ACTION_POINTER_UP) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 Pointer Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 Pointer Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 Pointer Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text =  "Square 4 Pointer Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             } else if(event.getAction() == MotionEvent.ACTION_POINTER_1_DOWN) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 Pointer1Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 Pointer1Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 Pointer1Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text = "Square 4 Pointer1Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             } else if(event.getAction() == MotionEvent.ACTION_POINTER_2_DOWN) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 Pointer2Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 Pointer2Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 Pointer2Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text =  "Square 4 Pointer2Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             } else if(event.getAction() == MotionEvent.ACTION_POINTER_3_DOWN) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText =  "Square 1 Pointer3Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 Pointer3Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 Pointer3Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text = "Square 4 Pointer3Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             } else if(event.getAction() == MotionEvent.ACTION_UP) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 UP::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 UP::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 UP::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text =  "Square 4 UP::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             } else if(event.getAction() == MotionEvent.ACTION_MOVE) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 MOVE::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 MOVE::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 MOVE::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text =  "Square 4 MOVE::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             }


            // TODO Auto-generated method stub
            return true;
        }

    }
}

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/MainLay" >

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal" android:padding="10dip">


        <TableRow android:gravity="center_horizontal" android:padding="10dip">
            <LinearLayout android:background="#FFFF00" android:id="@+id/square1" android:padding="10px" android:layout_width="125px" android:layout_height="125px"/>
            <LinearLayout android:background="#FF00FF" android:id="@+id/square2" android:padding="10px" android:layout_width="125px" android:layout_height="125px"/>
        </TableRow>

        <TableRow android:gravity="center_horizontal" android:padding="10dip">
            <LinearLayout android:background="#00FFFF" android:id="@+id/square3" android:padding="10px" android:layout_width="125px" android:layout_height="125px"/>
            <LinearLayout android:background="#eeeeee" android:id="@+id/square4" android:padding="10px" android:layout_width="125px" android:layout_height="125px"/>
        </TableRow>     


          <TextView android:id="@+id/logView" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
          <TextView android:id="@+id/logView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
          <TextView android:id="@+id/logView2" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
          <TextView android:id="@+id/logView3" android:layout_width="wrap_content" android:layout_height="wrap_content"/>



    </TableLayout>

</LinearLayout>

Good luck, and if you try it let me know if you get the GOT THREE POINTERS!! print statements. I wonder what phones supports it.

thanks, dk

Taxdeductible answered 18/1, 2011 at 3:14 Comment(2)
Galaxy S seems to support up to five counters. Demonstrated by someone from a different forum using the software above.Taxdeductible
Using an LG optimus. Detects only 1 counter.Organist
L
0

I edited @dakira answer. Here is the my sample code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainLay"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tvHeader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="Place Your Finger on Squire"
            android:textColor="#000"
            android:textSize="20sp" />

        <LinearLayout
            android:id="@+id/llStatusHolder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvHeader"
            android:orientation="vertical"
            android:weightSum="2">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="8dp"
                android:weightSum="2">

                <TextView
                    android:id="@+id/tvBox1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#FFFF00"
                    android:gravity="center"
                    android:padding="4dp"
                    android:text="Finger : 0"
                    android:textColor="#000"
                    android:textSize="28sp" />

            </LinearLayout>

        </LinearLayout>


        <LinearLayout
            android:id="@+id/square1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="4dp"
            android:layout_below="@+id/llStatusHolder"
            android:layout_weight="1"
            android:background="#FFFF00"
            android:orientation="horizontal"
            android:padding="10dp">

        </LinearLayout>

        </RelativeLayout>
    </LinearLayout>

Here is JAVA code:

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MultiTouchTestActivity extends Activity {
    protected LinearLayout sq1;

    TextView tvBox1;
    int counter1 = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_touch_test);
        setViews();
    }

    private void updateTextField() {

        tvBox1.setText("Finger : " + counter1);

    }

    private void setViews() {
        tvBox1 = (TextView) findViewById(R.id.tvBox1);
        sq1 = (LinearLayout) this.findViewById(R.id.square1);

        sq1.setOnTouchListener(new TouchListener());

    }

    private class TouchListener implements OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == 1) {
                setCounterValue(1, 0);
            }else if (event.getAction() == MotionEvent.ACTION_DOWN) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_POINTER_DOWN ||
                    event.getAction() == MotionEvent.ACTION_POINTER_UP) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_POINTER_1_DOWN) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_POINTER_2_DOWN) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_POINTER_3_DOWN) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                setCounterValue(1, event.getPointerCount());
            }else if (event.getAction() == MotionEvent.ACTION_BUTTON_RELEASE) {
                setCounterValue(1, 0);
            }
            updateTextField();
            return true;
        }
    }

    void setCounterValue(int hasvalue, int value) {
        switch (hasvalue) {
            case 1:
                counter1 = value;
                break;
        }
    }
}

Hope this will solve many persons problem :)

enter image description here

Lysozyme answered 13/5, 2017 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.