rogcg
rogcg

Reputation: 10471

Changing an Image Buttons Image when depressed and released

I have an ImageButton. I'd like that when I clicked it the image change. I tryied calling setBackground inside onCLick method, but it doesnst accept an R.drawable. What should I use please.

Upvotes: 0

Views: 276

Answers (2)

Austin Hanson
Austin Hanson

Reputation: 22040

Create a State List drawable: they let you define different drawables for various button states.

See http://developer.android.com/guide/topics/resources/drawable-resource.html

Upvotes: 1

Ashterothi
Ashterothi

Reputation: 3282

use the

button.setImageResource(resId)

As in:

public boolean onTouch(View v, MotionEvent me) {
    int action = me.getAction();
    if(action == MotionEvent.ACTION_DOWN) {
        button.setImageResource(R.id.resId1)
        //other stuff
        return true;
    } else if (action == MotionEvent.ACTION_UP) {
        button.setImageResource(R.id.resId2)
        //other stuff
        return true;

    }
    return false;
}

Upvotes: 1

Related Questions