0

I have successfully set an image to an ImageButton using this code:

mGetClickTime.setImageResource(randomImageId);

But I'm trying to problematically change the image by calling a method which contains:

mGetClickTime.setImageResource(randomImageId);

(In which the randomImageId vraiable is different)

However the programmatic change is not working.

Do I need to remove the current image before setting a new one? If so, how do I do that?

EDIT

The problem seems to be that the above command stops working after another activity has been called and completed. After that happens the setImageResource just doesn't work.

I'm not sure why this could be. I've tried commenting out everything from the second activity apart from this

   public void onCreate(Bundle savedInstanceState) {         
   super.onCreate(savedInstanceState);
   }

but the problem still occurs. Why?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dicky Moore
  • 956
  • 3
  • 10
  • 32
  • That is a correct way to do it. You have some other issue in your code. – Marko Lazić May 16 '13 at 23:15
  • You are right! Thanks. I still can't work out what it is but I'll edit the question with what I do know. – Dicky Moore May 17 '13 at 20:06
  • I'd really like to help you but this so little info to proceed. If you are not able to share your code, try doing it step by step, first change the image and see whether it does what you want, then start the other activity, and resolve if the problem is in combination or in some specific step. When you do something complex try testing your code chunk by chunk. One step at the time. – Marko Lazić May 17 '13 at 21:20
  • Thanks Marko. I appreciate your willingness to help me. I'm happy to paste more of the code in, I just didn't want it to get too messy as there's a lot in there. I actually opened a new question up which was more specific about what I think the problem is, here: http://stackoverflow.com/questions/16617581/changing-activities-prevents-imagebutton-setimageresource-from-working – Dicky Moore May 17 '13 at 22:31
  • Also, I tried stepping through it in the Eclipse debugger but I just can't make sense of why the ImageButton isn't changing when it's being told to. All the variables look correct. – Dicky Moore May 17 '13 at 22:32

1 Answers1

0

Give this a try:

mGetClickTime.setBackgroundResource(randomImageId);

This will work in an onClick method. That might be your issue, but not 100% sure until I see the context of the code.

AJak
  • 3,863
  • 1
  • 19
  • 28