2

Question I have created some ImageViews by code in android by in the onCreate() class The class implements the view.onclick()..
The ImageView doesn't register that it has been clicked. That's the problem should I use setTag(1) instead, how can I use tag in the onClick() ? :

a1 = new ImageView(this);
a1.setImageResource(R.drawable.examplepicture);
a1.setId(1);


public void onClick(View v) {   

    switch (v.getId()) {

    case (1):
        Toast.makeText(ScrollView1.this, "id200", Toast.LENGTH_LONG).show();
        break;
    }
}`
joao2fast4u
  • 6,868
  • 5
  • 28
  • 42
Hello
  • 119
  • 2
  • 11

2 Answers2

3

You need to something like this

a1.setOnClickListener(new View.OnClickListener({
    public void onClick(View v) {   

        switch (v.getId()) {

        case (1):
            Toast.makeText(ScrollView1.this, "id200", Toast.LENGTH_LONG).show();
            break;
        }
     }
}));

Update from comment

You should store all your image views in a list (or array), implement the onClickListener (rather than using it as an anonymous inner class) and add them in a for loop, something like this.

class MyOnClickListener implements View.OnClickListener{

    public void onClick(View v) {   

        // do something with your View v for example ((ImageView)v.setImageBitmap(yourImage)
        switch (v.getId()) {

        case (1):
            Toast.makeText(ScrollView1.this, "id200", Toast.LENGTH_LONG).show();
            break;
        }
     } 
 }

MyOnClickListener listener = new MyOnClickListener();

// cycle through adding listener to yuor view
for(ImageView view : imageViews) {
    view.setOnClickListener(listener)
}

If you wanted to perform a specific function on the view you are getting passed it as an argument and so can perform whatever operation on it.

// do something with your View v passed as onClick param, for example ((ImageView)v.setImageBitmap(yourImage)

Update from comment.

Code suggested by asker:

ArrayList<ImageView> imageViews = new ArrayList<ImageView>();  
imageViews.add(IM1); // add others ... 
for(ImageView imgView : imageViews){ 
    IM1.setOnClickListener(this); 
} 
public void onClick(View v){ 
    if((ImageView)v == IM1) { // do something } 
} 

This should work but what you want to be doing is defining your OnClickListener as a separate class (probably an inner class). Your ImageViews should be defined and set up in a separate class (perhaps in the activity) and then you should add your OnClickListener by calling setOnClickListener from the activity (as described in my answer above). What you are doing is mixing up your listener and the objects it is listening on which isn't very object orientated and generally quite poor practice.

rogermushroom
  • 5,486
  • 4
  • 42
  • 68
  • Ok, but i have 300-400 imageviews. Can i use the implement View.onclic.. and use a public onlick for all of the views ? – Hello Jan 10 '12 at 16:48
  • Have never stored them in a array before, can this work ? `ArrayList imageViews = new ArrayList(); imageViews.add(IM1); // add others ... for(ImageView imgView : imageViews){ IM1.setOnClickListener(this); } `public void onClick(View v){ if((ImageView)v == IM1){ // do something } } ` – Hello Jan 10 '12 at 17:05
0

If you have your activity implement OnClickListener, you should be able to do this.

a1 = new ImageView(this); 
a1.setImageResource(R.drawable.examplepicture); 
a1.setOnClickListener( this );

However, I would personally avoid having the activity implement OnClickListener. Instead, I would do this.

private OnClickListener a1Listener = new OnClickListener() {
    public void onClick(View v) {
      // do something when the button is clicked
    }
};

protected void onCreate(Bundle savedValues) {
    a1 = new ImageView(this); 
    a1.setImageResource(R.drawable.examplepicture); 
    a1.setOnClickListener( a1Listener );
}
Kiran Ryali
  • 1,371
  • 3
  • 12
  • 18
  • The `View v` parameter passed into `onClick` should refer to the `ImageView` you just created. – Kiran Ryali Jan 10 '12 at 16:48
  • Ok, but I have 300-400 views. Then i need 300-400 onlick listeners. Can i make one that works for all and use a switch in it ? – Hello Jan 10 '12 at 16:51
  • This listener should work for all the views. The `View v` that gets passed to `onClick` is the one that just received the touch event. You don't need to do a 300-400 line switch. – Kiran Ryali Jan 10 '12 at 16:53
  • 1
    If you have 300 to 400 imageViews you should probably consider using a listView or Gallery. – blindstuff Jan 10 '12 at 17:35
  • In general you don't want to have 300-400 views if only few are displayed at a time. As blindstuff sayed, you can use ListView or Gallery, they create only a small number of item views and recycle them as you scroll. Much more efficient. – user1076637 Jan 10 '12 at 17:42
  • I am using a horizontalscroll view with a linearlayout as child and the imageview in the linearlayout. Everything is working fine now – Hello Jan 10 '12 at 17:54
  • blindstuff is right. You should avoid having 300-400 views at one time, because your performance can take a big hit. You need to use a listView as suggested. – Kiran Ryali Jan 10 '12 at 18:22