Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change background on FloatingActionButton?

I am trying to set the background of a FAB to a different color in XML. I know I could do this in code, but that would also require a lot of inconvenient refactoring on my part.

<android.support.design.widget.FloatingActionButton
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:background="#F38E1B"
    android:id="@+id/fab"/>

enter image description here

For some reason, the background stays blue when it should be orange. I have tried setting the background to a drawable as well but that yields the same results.

Is this a bug or am I just missing something?

like image 314
Kevin Cronly Avatar asked Jan 24 '26 22:01

Kevin Cronly


2 Answers

I also tried to use the same approach as you and was getting the same result. My solution was to was to this:

<item name="colorAccent">@color/yourColor</item>

in my application theme.

However, this color will also be used in all your widgets.

Hope this helps!

like image 150
harry perry Avatar answered Jan 26 '26 13:01

harry perry


I had the same problem. This is how I solve my issue: I just create a new theme to my button.

My FAB xml:

<android.support.design.widget.FloatingActionButton
      ...
      android:theme="@style/MapButton"/>

My new theme in style.xml:

<style name="MapButton" parent="AppTheme">
        <item name="colorAccent">#FFFFFF</item></style>

I created a new theme based on my App's theme and changed only the color I wanted.

Hope it helps.

like image 36
Pedro Acácio Avatar answered Jan 26 '26 14:01

Pedro Acácio