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.