pavan
pavan

Reputation: 1135

How to get imageView inside onTouchEvent(MotionEvent event) in android

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

Upvotes: 2

Views: 6816

Answers (3)

JRaymond
JRaymond

Reputation: 11782

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

Upvotes: 3

techi.services
techi.services

Reputation: 8533

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.

Upvotes: 0

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

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

}

Upvotes: 2

Related Questions