Hirdesh Vishwdewa Hirdesh Vishwdewa - 1 year ago 418
Android Question

android: dynamically change FAB(Floating Action Button) icon from code

How to change FAB icon in an Activity during runtime. I have this code ->

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabMainActivity);

I know this is possible using
but i am a newbie to android, don't understand how to do this.

Any help will be highly appreciated.


Answer Source

Changing FloatingActionButton source:

            floatingActionButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_full_sad, context.getTheme()));
        } else {

This can be replaced by following code from the support library instead:

floatingActionButton.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_full_sad));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download