Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 fab.setBackgroundDrawable(); but i am a newbie to android, don't understand how to do this.

Any help will be highly appreciated.

Thanks

like image 937
Hirdesh Vishwdewa Avatar asked Oct 15 '15 05:10

Hirdesh Vishwdewa


People also ask

How do I change the icon on my floating action button?

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.

How do I change the size of my floating action button?

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.

How do you change the shape of the floating action button in Flutter?

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.


1 Answers

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)); 
like image 162
Sunny Avatar answered Sep 28 '22 08:09

Sunny