15

Is there an onImageChangedListener() on a ImageView?

I need the event when the image is changed from the ImageView.

Naskov
  • 4,121
  • 5
  • 38
  • 62
  • The image is changed onButtonClick, choosing it from Camera or SDCard. – Naskov Nov 29 '12 at 09:04
  • I mean exactly how are you changing the image? setImageBitmap? setImageDrawable? Other? – Simon Nov 29 '12 at 09:06
  • there is not, because the change of image comes from the code, which means that you know when it changes. – njzk2 Nov 29 '12 at 09:06
  • @Simon I am using setImageBitmap() or setImageResource(), depends If I am taking it from SDCard or using the R.id.default_image – Naskov Nov 29 '12 at 09:08

3 Answers3

25

There is no default listener in Android .. but we can create the imagechange listiner .. copy the class and instead of using ImageView use MyImageView..

 public class MyImageView extends ImageView {

        private OnImageChangeListiner onImageChangeListiner;


        public MyImageView(Context context) {
            super(context);
        }

        public MyImageView(Context context, AttributeSet attributeSet) {         
            super(context, attributeSet); 
        }


        public void setImageChangeListiner(
                OnImageChangeListiner onImageChangeListiner) {
            this.onImageChangeListiner = onImageChangeListiner;
        }

        @Override
        public void setBackgroundResource(int resid) {
            super.setBackgroundResource(resid);
            if (onImageChangeListiner != null)
                onImageChangeListiner.imageChangedinView(this);
        }


        @Override
        public void setBackgroundDrawable(Drawable background) {
            super.setBackgroundDrawable(background);
            if (onImageChangeListiner != null)
                onImageChangeListiner.imageChangedinView(this);
        }


        public static interface OnImageChangeListiner {
            public void imageChangedinView(ImageView mImageView);
        }
    }
Sandeep P
  • 4,291
  • 2
  • 26
  • 45
  • And what should I import for "OnImageChangeListiner cannot be resolved to a type" ? – Naskov Nov 29 '12 at 09:19
  • copy this class in any package ... import packagename.MyImageView; – Sandeep P Nov 29 '12 at 09:22
  • You should implement the interface in your activity and override imageChangedInView(). `extends Activity implements OnImageChangeListiner` – Simon Nov 29 '12 at 09:22
  • 1
    yaaa implements MyImageView.OnImageChangeListiner – Sandeep P Nov 29 '12 at 09:23
  • 3
    Don't forget to add this constructor or it may crash when inflating view from XML: `public mImageView(Context context, AttributeSet attributeSet) { super(context, attributeSet); }` – Quentin S. Dec 31 '14 at 17:01
  • If you get "Custom view MyImageView is not using the 2- or 3-argument View constructors; XML attributes will not work" have a look here: http://stackoverflow.com/a/13797457 – Vlad Schnakovszki Feb 20 '15 at 22:08
  • 1
    You'll want to override setImageBitmap in a manner similar to what Sandy09 mentioned if you're using it to change the image. – Vlad Schnakovszki Feb 20 '15 at 22:38
4

Check the imageview code in grepcode. You don't know when it is changed or redrawn. It is because after you setImageDrawable(), imageview will invalidate. At this time, the image IS NOT CHANGED correctly until ondraw is called.

Anyway, why do you want to know the onimagechangedlistener?

e7fendy
  • 176
  • 1
  • 5
1

If you want to load the image from network and check change in imageview you can use imageView.isAttachedToWindow(). I have tried downloading the image from network and disabled the progressbar after image downloaded and attached to window. Use,

         if(imageView.isAttachedToWindow()){ 
            //your code here
          }
Vignesh R
  • 125
  • 8