How can I change the images on an ImageButton in Android when using a OnTouchListener?

Posted by Cody on Stack Overflow See other posts from Stack Overflow or by Cody
Published on 2010-04-11T17:32:35Z Indexed on 2010/04/11 17:33 UTC
Read the original article Hit count: 924

Filed under:
|
|

I have the following code which creates an ImageButton and plays a sound when clicked:

ImageButton SoundButton1 = (ImageButton)findViewById(R.id.sound1); SoundButton1.setImageResource(R.drawable.my_button);

    SoundButton1.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN ) {
                mSoundManager.playSound(1);
                return true;
            }

            return false;
        }
    });

The problem is that I want the image on the ImageButton to change when you press it. The OnTouchListener appears to be overriding the touch and not allowing the images to change. As soon as I remove the OnTouchListener, the ImageButton swaps to a different image when pressed. Any ideas on how I can have the images change on the ImageButton while still using the OnTouchListener? Thank you very much!

© Stack Overflow or respective owner

Related posts about android

Related posts about android-sdk