Hello
Hello

Reputation: 119

Onclick for imageview created in java, not xml

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

Upvotes: 2

Views: 3039

Answers (2)

rogermushroom
rogermushroom

Reputation: 5586

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.

Upvotes: 3

Kiran Ryali
Kiran Ryali

Reputation: 1371

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

Upvotes: 0

Related Questions