0

I have the following piece of code...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ImageView i = (ImageView) findViewById(R.id.imageView1);
    i.setBackgroundResource(R.anim.animation);

    i.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            AnimationDrawable anim = (AnimationDrawable) i.getBackground();
            anim.start(); 
        }
    });
}

I want to add another listener on the background/layout so that when someone clicks on the image, the animation gets started and when someone clicks on the background it stops looping.

Any help is appreciated.

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
Gurjit
  • 564
  • 1
  • 9
  • 19

1 Answers1

1

Move your animation drawable to a class variable. Then, start and stop it from onClickListeners hooked up to the ImageView and the background.

AnimationDrawable anim;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final ImageView i = (ImageView) findViewById(R.id.imageView1);
    i.setBackgroundResource(R.anim.animation); 
    anim = (AnimationDrawable) i.getBackground();
    i.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            anim.start(); 

        }
    });

    // Or whatever type of layout it is
    LinearLayout myLayout = (LinearLayout) findViewById(R.id.my_layout);
    myLayout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            anim.stop(); 

        }
    });

}
Mike Ortiz
  • 4,031
  • 4
  • 27
  • 54
  • Thanks for the efforts bro :) but not getting it going..!! – Gurjit Jan 03 '14 at 19:07
  • Plz try again if u can,,,i got some hint from ur code,,,first of all the code R.id.mylayout is not working..????? – Gurjit Jan 03 '14 at 19:09
  • hmm,,, there is error i have tried to edit it someway,,although dont know,,this is code which i have added in addition – Gurjit Jan 03 '14 at 19:11
  • RelativeLayout rl = (RelativeLayout) findViewById(R.layout.activity_main); rl.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { AnimationDrawable anim1 = (AnimationDrawable) i.getBackground(); anim1.stop(); } }); } – Gurjit Jan 03 '14 at 19:12
  • Sorry, that's a placeholder for your actual layout's id. You will need to give the layout an id the same way your ImageView has the id imageView1. – Mike Ortiz Jan 03 '14 at 19:13
  • Awesome work ,,,thanks thanks for that bro :))))))....one thing more i wana ask how could i reset its state,,means i want when i click there it not just stops but gets to initial stage..?? – Gurjit Jan 03 '14 at 19:26
  • You cannot use findViewById(R.layout.activity_main) because it is a layout, not an id. Use my code. Then, in your activity_main.xml, set the parent layout's id as android:id="@+id/my_layout" – Mike Ortiz Jan 03 '14 at 19:29
  • Check this out: http://stackoverflow.com/questions/7685327/how-to-reset-animationdrawable – Mike Ortiz Jan 03 '14 at 19:33
  • Yahh i got ur point and It has been solved ...!! Thanks for the best efforts..!! one thing more i wana ask how could i reset its state,,means i want when i click there it not just stops but gets to initial stage..?? – Gurjit Jan 03 '14 at 19:33