1

This Should be easy and simple right?!
So, i have a Gameobject as a Button i reference it on a GamePanel that extents SurfaceView like this :

bby = new Buttons(BitmapFactory.decodeResource(getResources(), R.drawable.babybutton),123 ,114 ,1);    

bby is drawn and work fine,
my problem is How can i change the Resource (R.drawble.babybutton) to a different resource for e.g (R.drawble.babybutton2) on Touch?

just like pressing buttons animations! Thanks in advance
(If my question look stupid please don't dislike! i'm very new to this).

Ethan
  • 25
  • 4

2 Answers2

0

if you want to change the button's drawable on touch on the surfaceview. than you can override this method in your surfaceview class.

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:
        //here you can set an another drawable to your button or can do something else
        break;
    }
    return true;
}

In your activity set a touchListener on the surfaceview. after initialize the surfaceView. pass this surfaceview to the

surfaceView.setOnTouchListener(surfaceView)
Devendra Singh
  • 2,343
  • 4
  • 26
  • 47
0

I may have misunderstood you, but if you want to change the button's rescource, this is how you should do it:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                button.setBackgroundResource(R.drawable.icon);
            }
        });
    }
});
Omar Aflak
  • 2,918
  • 21
  • 39