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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, but i have 300-400 imageviews. Can i use the implement View.onclic.. and use a public onlick for all of the views ?
Have never stored them in a array before, can this work ? 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 } } `
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 );
}

7 Comments

The View v parameter passed into onClick should refer to the ImageView you just created.
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 ?
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.
If you have 300 to 400 imageViews you should probably consider using a listView or Gallery.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.