2

I defined onTouchEvent(MotionEvent event) to receive touch events for imageview. I am using multiple imageviews and i have to find for which imageview I have received touch event. Is it possible to get imageview inside onTouchEvent?

public void createImageViews()
{
  int i = 0;

  imageArray[0] = (ImageView)findViewById(R.id.image1);
  imageArray[1] = (ImageView)findViewById(R.id.image2);
  imageArray[2] = (ImageView)findViewById(R.id.image3);
  imageArray[3] = (ImageView)findViewById(R.id.image4);
  imageArray[4] = (ImageView)findViewById(R.id.image5);
  imageArray[5] = (ImageView)findViewById(R.id.image6);

  for (i = 0; i < count; i++)
  {
    imageArray[i].setOnTouchListener(this);
  } 
}
 public class Touch extends Activity implements OnTouchListener, AnimationListener { }

I have tried using

 public boolean onTouch(View v, MotionEvent event) { } but not receiving the touch events.

After starting the animation on imageview I did not observe touch events in onTouch() and getting touch events in onTouchEvent() . imgArray[g_animCount].startAnimation(movArray[g_animCount]);

Charles
  • 50,943
  • 13
  • 104
  • 142
pavan
  • 1,085
  • 4
  • 22
  • 34

3 Answers3

3

You can overide onTouchListener. The code for onTouchListener is

@Override
public boolean onTouch(View v, MotionEvent event) {
/// your stuff
}

use View v to know the touched view

Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
JRaymond
  • 11,625
  • 5
  • 37
  • 40
  • I am not receiving touch events inside @Override public boolean onTouch(View v, MotionEvent event) { } so defined onTouchEvent(MotionEvent event). I have updated my imageview code in my question. – pavan Apr 20 '12 at 18:13
  • @pavan why wouldn't you receive onTouch()? if you set your activity as the onTouchListener as I think you're doing then you should be getting touch events... – JRaymond Apr 20 '12 at 18:23
  • I am not sure why touch events are not received in onTouch. I have my actvity like below, public class Touch extends Activity implements OnTouchListener, AnimationListener { } – pavan Apr 20 '12 at 18:27
2
use the below sample code snippet::::

    public class MyImageView extends Activity implements OnTouchListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lay_name);        
        int i = 0;
        imageArray[0] = (ImageView)findViewById(R.id.image1);
        imageArray[1] = (ImageView)findViewById(R.id.image2);
        imageArray[2] = (ImageView)findViewById(R.id.image3);
        imageArray[3] = (ImageView)findViewById(R.id.image4);
        imageArray[4] = (ImageView)findViewById(R.id.image5);
        imageArray[5] = (ImageView)findViewById(R.id.image6);

        for (i = 0; i < count; i++)  {
            imageArray[i].setOnTouchListener(this);
         } 


    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.image1:
        //your stuff
        break;
        case R.id.image2:
        //your stuff        
        break;
        case R.id.image3:
        //your stuff
        break;
        case R.id.image4:
         //your stuff
        break;
        case R.id.image5:
         //your stuff
        break;
        case R.id.image6:
         //your stuff
        break;
        }
    }

}

Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • I am not receiving touch events inside @Override public boolean onTouch(View v, MotionEvent event) { } so defined onTouchEvent(MotionEvent event). I have updated my imageview code in my question. – pavan Apr 20 '12 at 18:10
  • try it now must get tough events – Shankar Agarwal Apr 21 '12 at 02:10
  • After starting the animation on imageview I did not observe touch events in onTouch(); imgArray[g_animCount].startAnimation(movArray[g_animCount]); – pavan Apr 21 '12 at 13:24
0

You can set android:onClick="clickHandler" for each View in your layout xml and define a method public void clickHandler(View view) in your Activity to handle the touch/click events.

This was introduced in 1.6. Check this LINK for more detail.

Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
techi.services
  • 8,473
  • 4
  • 39
  • 42
  • I have one more problem here, I can not find touch location. I mean x, y coordinates. – pavan Apr 20 '12 at 18:24
  • For one this only handles onClick - for another its not really good design to have the xml file be tied to the code file unless absolutely necessary - which nothing in his post suggests it should be – JRaymond Apr 20 '12 at 18:24
  • @pavan. if you want x,y cords then you will need a touch listener. – techi.services Apr 20 '12 at 18:28
  • @techiServices yes I have - I know it works, I'm just saying its not something you really want to do unless you're truly desperate to get a unique click event in a place where it would otherwise be very difficult or impossible, such as in a ListItem for a favorites star, etc. – JRaymond Apr 20 '12 at 18:31
  • @JRaymond. so `For one this only handles onClick` is incorrect. Also if you wanted to get a `unique click event...` you can just use `View.setOnClickListener` or `View.setOnTouchListener`. Lastly if it is not a good idea why would google build the functionality? – techi.services Apr 20 '12 at 18:36
  • @techiServices not intending to start a flamewar here, I'm just saying that in an ideal world, your XML layouts would have no tieback to the underlying code - theres just usually no reason for it to know, and it makes maintenance more difficult if you ever change function names to reuse the same layout. It has its uses, which is why google put it in, but it isn't something to be used without knowing those tradeoffs. :) – JRaymond Apr 20 '12 at 18:42
  • After starting the animation on imageview I did not observe touch events in onTouch() and getting touch events in onTouchEvent() . imgArray[g_animCount].startAnimation(movArray[g_animCount]); – pavan Apr 21 '12 at 13:27