0

I have the following design :

<LinearLayout>
  <ScrollLayout>
    <LinearLayout> .... </LinearLayout> ----> TextView and ImageView dynamically created in this LinearLayout
  </ScrollLayout>
</LinearLayout>

I need to detect touch events on the dynamically created ImageView(s). I don't know how many ImageView will be created dynamically. How can I detect which ImageView was touched by the user ?

Thanks for your help.

edtheprogrammerguy
  • 5,957
  • 6
  • 28
  • 47
user2781902
  • 63
  • 1
  • 7

2 Answers2

2

You'll want to create one [maybe more] View.OnClickListeners in your java code. Then when each ImageView is added, you can assign that OnClickListener to it using View.setOnClickListener. To determine which was clicked, you can set a "Tag" using View.setTag() and then when onClick is called, use View.getTag() to determine which was clicked. The tag should be uniquely identifiable.

Here's some rough code (untested):

private OnClickListener imageViewListener extends View.OnClickListener{
    public void onClick(View v){
        Intent i = new Intent(v.getContext(), NewActivity.class);
        i.putExtra("ImageURI", v.getTag().toString());
        startActivity(i);
    }
}

Then as you add images, you just set the tag and listener:

for(String uri: someList){
    ImageView iv = new ImageView(this);
    iv.setTag(uri);
    iv.setImageUri(uri);
    iv.setOnClickListener(imageViewListener);
}
Matt
  • 3,837
  • 26
  • 29
  • The reason I said I wanted to detect the ImageView touched was so that I can open a new Activity and pass the image using Intent. If I add the OnClickListener to wach ImageView while creating, then I don't have to use the Tag part of your answer. I was hoping if Android had any neat solutions for such things(like startActivityForResult() in place of startActivity() ). – user2781902 Dec 27 '13 at 13:42
  • You probably shouldn't pass a bitmap as an Extra... I'm guessing that you're loading this image from a URI (either web URL or maybe file path?) I'll edit the answer with some pseudo-code of waht I would do... – Matt Dec 27 '13 at 13:52
  • Yes, I'm loading the image from some URL. Why should I not pass Bitmap as Extra. Is it because of performance issue? – user2781902 Dec 27 '13 at 13:56
  • Usually it's a good idea to keep your Extras small, especially for something like a Bitmap that may be loaded from a server and maybe you're not sure exactly how big it is (lots of memory!) If you want it to load fast, you should cache it to disk. If you're positive that the image will be "small" it won't hurt too much to pass it as an extra, but you want to be careful that you don't eat up too much memory. – Matt Dec 27 '13 at 13:59
  • Here's a related question about passing Bitmaps around: http://stackoverflow.com/questions/4352172/how-do-you-pass-images-bitmaps-between-android-activities-using-bundles – Matt Dec 27 '13 at 14:00
  • I'll try to implement your suggestion in my code. Thanks for your input. – user2781902 Dec 27 '13 at 14:02
1

You should use View.OnTouchLisetener class. So, after initializing your ImageView, add this call to it:

imageView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent e) {
        //Your code here
    }
});
nstosic
  • 2,584
  • 1
  • 17
  • 21