Android changing Floating Action Button color
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In recent years, mobile application design has become increasingly user-centric, emphasizing simplicity and intuitive interfaces. One of the key components in Android app design is the Floating Action Button (FAB). This distinct button is a vital element of Material Design, aiming to provide users with a prominent action button that stands out on the interface. A significant customization feature developers often seek is changing the color of the FAB to align with the application's theme or brand identity.
Understanding the Floating Action Button
The Floating Action Button is a circular button that floats slightly above the UI, indicating a primary action that users can take. Its design ensures it catches the user's attention and encourages interaction. The FAB is best used for primary actions on a screen, such as composing a new email, adding a new contact, or triggering an important function specific to the app's purpose.
Default Behavior and Styling
By default, the FAB inherits the "color accent" property from the theme applied to the application. This makes it align with the overall theme but sometimes might require specific customization.
Changing the FAB Color
To change the color of the FAB, developers can use several approaches that provide flexibility and adaptability depending on the requirements. Below are ways to alter the FAB's background and icon color:
XML Layout Approach
In the XML layout file, we can directly set the FAB's background color using the app:backgroundTint attribute. Here's a simple XML snippet demonstrating this approach:
In this example, the backgroundTint attribute is used to set the FAB's background color to a predefined color resource, colorPrimary.
Programmatic Approach
Developers can also change the FAB's color dynamically at runtime using Java or Kotlin. This approach is helpful when the color needs to change based on user interactions or specific app states.
In Java:
In Kotlin:
Using Themes and Style Attributes
For a more holistic and maintainable approach, especially in larger projects, defining the styling in a theme is recommended. Here is how you can define FAB color in your styles.xml:
Changing the Icon Color
Sometimes, changing the button's background alone is not enough—you might also want to ensure the icon itself is visible against the new background. This is done by setting the icon tint.
XML Icon Tint
Programmatic Icon Tint
In case you want to set the icon color programmatically, the procedure is similar to setting the background color.
Best Practices
- Contrast: Ensure good contrast between FAB color and background to maintain usability.
- Consistency: Align FAB color with the overall app theme for consistency.
- Accessibility: Choose colors that are easily distinguishable to users with color blindness or other visual impairments.
Example Use Cases
- Weather App: Use a blue FAB to match a sunny theme.
- E-commerce App: Green or red FAB could signify positive or critical actions like checkout or remove.
Summary Table
| Method | Description | Advantage | Use Case |
| XML Attribute | Set color using app:backgroundTint | Simplicity | Static FAB color |
| Programmatic | Modify FAB color at runtime | Flexibility | Dynamic color changes |
| Theme Attribute | Define color in a style/theme | Maintenance and consistency | When FAB needs uniformity across screens |
| Accessibility | Ensures readability and usability | Higher user experience | Apps with high readability requirements |
In conclusion, changing the Floating Action Button’s color is a simple yet effective way to enhance the UI/UX of an Android application. By making use of both XML and programmatic approaches, developers can customize the FAB to better fit their specific design needs, ensuring that it both stands out to users and harmonizes with the app's overall aesthetic.

