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 fab.setBackgroundDrawable();
but i am a newbie to android, don't understand how to do this.
Any help will be highly appreciated.
Thanks
Add the floating action button to your layout The size of the FAB, using the app:fabSize attribute or the setSize() method. The ripple color of the FAB, using the app:rippleColor attribute or the setRippleColor() method. The FAB icon, using the android:src attribute or the setImageDrawable() method.
To change the size of the Floating action button: To change the size of the floating action button, wrap it with SizedBox() widget and pass custom height and width. SizedBox( height:100, width:100, child:FloatingActionButton( child: Icon(Icons.
Changing the shape of the FAB. To change the shape of the button, we set the shape parameter in the constructor. The default value of the shape is a CircleBorder(). Let's make a square button instead of a round one.
Changing FloatingActionButton source:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { floatingActionButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_full_sad, context.getTheme())); } else { floatingActionButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_full_sad)); }
This can be replaced by following code from the support library instead:
floatingActionButton.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.ic_full_sad));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With