Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate and replace fab icon in android

Tags:

android

I have a fab button with camera icon (defined in xml) I want on click to rotate the icon and replace it by x...

I am using the following xml to make a rotation

Thank youenter image description here

like image 311
IgorKushnir Avatar asked Nov 30 '25 11:11

IgorKushnir


1 Answers

You can do that using ObjectAnimator, here:

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ObjectAnimator.ofFloat(fab, "rotation", 0f, 360f).setDuration(800).start();
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (isWhite){
                        fab.setImageDrawable(getResources().getDrawable(R.drawable.ic_add_black_48dp));
                        isWhite = false;
                    } else {
                        fab.setImageDrawable(getResources().getDrawable(R.drawable.ic_add_white_48dp));
                        isWhite = true;
                    }
                }
            }, 400);
        }
    });

Icon will change in the middle of rotation

like image 116
fairon201 Avatar answered Dec 02 '25 02:12

fairon201



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!