0

I have a imageButton with some pic as its src. I want to switch from that pic to an animated .gif file when i click on image button. i am using android studio. I tried to use normal method onCLick="" in layout but it's not working. I am getting an still image everytime normal static image is coming up.

newon is gif image

public void buttonClick3(View v) {
    SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
    Boolean e = sharedPreferences.getBoolean("clicked3", false);
    if (!e) {
        aButton3.setImageResource(R.drawable.newon);
        toggleSound.start();
        sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("clicked3", true);
        editor.commit();
    }
    if(e){
        aButton3.setImageResource(R.drawable.newoff);
        sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor = sharedPreferences.edit();
        editor.putBoolean("clicked3", false);
        editor.commit();
        toggleSound.start();

    }
}
Swarnveer
  • 490
  • 5
  • 23
  • Unrelated to your question: you don't really need two if statements. You could get away with just an `if... else...` statement. (In fact, a lot of that code probably doesn't even need to be in an if statement since it gets called regardless of whether `e` is `true` or `false`. Just my 2 cents.) – Charles Caldwell Mar 02 '16 at 18:27
  • your code needs a review... and i think here is the answer to your question: http://stackoverflow.com/questions/3660209/display-animated-gif – Meiko Rachimow Mar 02 '16 at 19:33
  • I just found a proper solution for this. Just visit [An answer with proper explanation of every step with code](http://stackoverflow.com/questions/35772102/usage-of-gif-in-imagebutton) – Swarnveer Mar 04 '16 at 19:39

0 Answers0