Android
Floating Action Button
FAB color
UI design
Android development

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:

xml
1<android.support.design.widget.FloatingActionButton
2    android:id="@+id/fab"
3    android:layout_width="wrap_content"
4    android:layout_height="wrap_content"
5    app:backgroundTint="@color/colorPrimary"
6    android:src="@drawable/ic_add" />

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:

java
FloatingActionButton fab = findViewById(R.id.fab);
fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(this, R.color.colorPrimary)));

In Kotlin:

kotlin
val fab: FloatingActionButton = findViewById(R.id.fab)
fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(this, R.color.colorPrimary)))

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:

xml
1<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
2    <!-- Customize your theme here -->
3    <item name="colorAccent">@color/colorPrimary</item>
4</style>

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

xml
1<android.support.design.widget.FloatingActionButton
2    android:id="@+id/fab"
3    android:layout_width="wrap_content"
4    android:layout_height="wrap_content"
5    app:backgroundTint="@color/colorAccent"
6    app:tint="@android:color/white"
7    android:src="@drawable/ic_add" />

Programmatic Icon Tint

In case you want to set the icon color programmatically, the procedure is similar to setting the background color.

Best Practices

  1. Contrast: Ensure good contrast between FAB color and background to maintain usability.
  2. Consistency: Align FAB color with the overall app theme for consistency.
  3. 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

MethodDescriptionAdvantageUse Case
XML AttributeSet color using app:backgroundTintSimplicityStatic FAB color
ProgrammaticModify FAB color at runtimeFlexibilityDynamic color changes
Theme AttributeDefine color in a style/themeMaintenance and consistencyWhen FAB needs uniformity across screens
AccessibilityEnsures readability and usabilityHigher user experienceApps 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.


Course illustration
Course illustration

All Rights Reserved.