Android multitouch View.OnTouchListener

Im trying to play around with some multitouch in Android.

I have two RelativeLayout on screen where I would like to get touch events.

fooLayout = (RelativeLayout)findViewById(; fooLayout.setOnTouchListener(onTouchListener); barLayout = (RelativeLayout)findViewById(; barLayout.setOnTouchListener(onTouchListener); ... View.OnTouchListener onTouchListener = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ...

I tried having a OnTouchListener for each layout but that didn't seem to work at all.

I figured since the onTouch method gets an View I would be able to make out with view the current touch event came from and save away the pointer id.

int pointerId = event.getPointerId(event.getActionIndex()); int action = event.getActionMasked(); switch (action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: switch (v.getId()) { case fooPointerId = pointerId; break; case barPointerId = pointerId; break; }

This does not work as I thought it would. When my first finger touches in one of the layouts I will get the correct id from that layout. But when I put down a second finger in the other layout it will get the id of the layout where I put down my first finger.

Am I going about this the wrong way?

Is this possible to do or do I have to put the OnTouchListener on the top view and figure out which layout Im touching by the x/y values of the event?

-------------Problems Reply------------

I think you should extend relative layout and implement onTouchEvent(MotionEvent ev) over there to figure out which pointers are touching which child views(Put foo and bar inside your main relative layout/view group that implements onTouchEvent).

IMO onTouch(View v, MotionEvent ev) only lets you register one pointer at a time so it may not be possible using this approach. This link might help out

Category:android Views:3 Time:2017-09-09

Related post

  • Implement GLSurfaceView layer under MultiTouch View in Android 2013-11-18

    I have my custom multi-touch view which is referenced by my Main Activity. Now, I want to implement motion sensors on this screen (Rotation_Vector) which uses GLSurfaceView. Rotation_Vector is able to return values if it runs under a separate activit

  • android: ZoomPicker breaks onTouchListener 2010-12-25

    I've got a webview which makes use of the built in zoom controls, as follows: wv = new WebView(this); wv.getSettings().setBuiltInZoomControls(true); Note: this activates two main functions - pinch-to-zoom and invokeZoomPicker() (the latter is only ca

  • Android Image Viewer from App 2009-10-11

    I'm trying to launch an image which is written to my application directory with the builtin Android image viewer. This image has been written in a different part of the app to the app directory. When getting the following file: super.getFilesDir() +

  • Android Custom View Constructor 2010-05-21

    I'm learning about using Custom Views from the following: The description says: Class Initialization As always, the super is called first. Furthermore, this is not a defaul

  • Geting "Window already focused, ignoring focus gain of:$Stub$Proxy" in Android 2010-06-29

    When I am going from one intent to another intent, I am getting this warning : "Window already focused, ignoring focus gain of:$Stub$Proxy" and the view stays on the old intent. the code is :

  • Text alignment line by line in android Text-view using Gravity 2010-08-31

    Can we align our text line by line(I mean whatever the text we have selected that should be aligned instead of the whole text) in android text-view dynamically!! Regards, vani. --------------Solutions------------- I think you need to draw text to you

  • Does android reuse views during a orientation change 2010-09-23

    Does android reuse views during a orientation change? I have a progress bar in my activity. I set the progress to 50%, and then do an orientation change. But for some reason, the progress bar maintain 50% after the orientation. I have call setprogres

  • What's a good way to limit the number of words that can be entered in an android EditText view? 2010-11-06

    What's a good way to limit the number of words that can be entered in an android EditText view? --------------Solutions------------- Extend a TextWatcher and register it with a EditText.addTextChangedListener(TextWatcher watcher) Edited: As an altern

  • Is it possible to use Android's View Animation package (tween/frame) within Live Wallpaper? 2011-03-04

    Greetings fellow Droidheads and the like. I am working on a Live Wallpaper project that's getting more complex. I am curious: has anyone figured out a way to use Android's View Animation package (

  • Android custom view is not drawn properly 2011-04-20

    I have a custom widget based on the Android Gallery view. It is slightly customized to show the elements from the left of the view without a gap and pass required events to my listener. On the very first view layout everything works perfect. However

  • How to apply different icons & different text in android grid view tutorial? 2011-04-23

    Please tell me about how to change or apply different icons with different text in the android grid view tutorial. My code is public class Home extends Activity { //---the images to display--- Integer[] imageIDs = { }; GridView grid_main; @Override p

  • Android web view not displaying https:\\ url 2011-05-25

    I have used web view to display html data. Now html data contains image , with src=\"Title : Android web view not displaying https:\ url . hview does not support for https url . If suppose i used with http then it works fine . IS there any solution ?

  • What is 'self' that shows up in android trace view 2011-06-05

    I've started to use the android Trace view tool to profile my game. In some of my methods im seeing a large percentage of time taken up in 'self'. I have a feeling its something obvious but does anyone know what this represents? --------------Solutio

  • How to set up intent for viewing Bitmap with default Android image viewer? 2011-06-06

    What I am trying to do is to download image from web and show it on screen with zoom/pan capability. I've successfully downloaded image into Bitmap instance, but ImageView doesnt have zooming feature. So instead I'm willing to present it with default

  • WARN/InputManagerService(59): Window already focused, ignoring focus gain of: [email protected] 2011-06-16

    i am getting this problem while eshtablishing facebook android sdk connection.. when i m running the code i getting this error message on the logcat: WARN/InputManagerService(59): Window already focused, ignoring focus gain of:

  • How onDraw method works in subclass of android-custom-view class? 2011-07-24

    I facing problem to draw rectangle at subclass of my android custom view class. Each time super class onDraw method works.But subclass onDraw method never executed. Super class will draw a rectangle and subclass will draw 4 rectangle within the super

  • View.OnTouchListener receives 2 sets of coordinates for only one pointer 2011-08-01

    I'm testing this on a Nexus S and while I'm holding and or moving my finger over this view I will receive 2 sets of coordinates. Every other one is almost as the one before but with an offset and I'm not using two fingers. class SUIJoystick extends F

  • Android XML Viewer failing 2011-08-03

    I've been having this problem for a while: Here is my header.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:layout_height="wrap_content" android:layout_width="fill_

  • Android PDF viewer app to be invoked 2011-08-04

    I need a pointer to a (preferably free) PDF viewer app that I can invoke on a file or URL link via a startActivity on an Intent. I know that there is no real standard - I have one phone that came with OpenOffice that can read local PDF files, and one

  • window already focused - ignoring focus gain of 2011-08-08

    I have seen many solutions to this but none works for me! I faced this problem in two previous occasions and I just had to revert to my backup code for it to work. I never found out why this happens. Some people say its just a warning, so you can ign

  • InputManagerService(60): Window already focused, ignoring focus gain of: 2011-08-26

    I have an activity the I opened with: Intent myIntent = new Intent(activity, MapActivity.class); activity.startActivityForResult(myIntent, 0); It opens and the user can work with it just fine, and in order to close it the user presses the back button

  • Top Border in Android Text View 2011-12-22

    How can set top border to Android Text view using xml ? Please can some one give a sample code --------------Solutions------------- Yes create 9-patch image with top border and set into TextView by using android:background

  • How to run android pdf viewer source code in eclipse? 2012-01-07

    I have downloaded android pdf viewer(apv) from . Now i want to import this source code in eclipse and i want to run it from eclipse.Please tell me step by step procedure for it. --------------Solutions------------- Copy

  • Should I call super() or call this() for android custom view constructors? 2012-02-08

    When creating a custom view, I have noticed that many people seem to do it like this: public MyView(Context context) { super(context); // this constructor used when programmatically creating view doAdditionalConstructorWork(); } public MyView(Context

Copyright (C), All Rights Reserved.

processed in 0.083 (s). 11 q(s)