Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Theming the FloatingActionButton
(FAB) primarily involves changing its background color, ripple color, and icon. Here's a step-by-step guide to theme a FloatingActionButton
in Android:
Declare your FloatingActionButton
in your XML layout:
activity_main.xml:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="16dp" android:contentDescription="@string/fab_desc" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:srcCompat="@android:drawable/ic_dialog_email" /> </androidx.constraintlayout.widget.ConstraintLayout>
You can theme the FAB directly in XML:
<com.google.android.material.floatingactionbutton.FloatingActionButton ... app:backgroundTint="@color/yourColor" app:rippleColor="@color/yourRippleColor" app:tint="@color/yourIconColor" />
Replace yourColor
, yourRippleColor
, and yourIconColor
with your desired colors.
Alternatively, you can also theme the FAB programmatically:
MainActivity.java:
import android.graphics.Color; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import com.google.android.material.floatingactionbutton.FloatingActionButton; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FloatingActionButton fab = findViewById(R.id.fab); // Set background color fab.setBackgroundTintList(ColorStateList.valueOf(Color.BLUE)); // Set ripple color fab.setRippleColor(Color.RED); // Set icon color fab.setImageTintList(ColorStateList.valueOf(Color.WHITE)); } }
Don't forget to import android.content.res.ColorStateList
.
To use the FloatingActionButton
, ensure you have the Material Components library in your build.gradle
:
implementation 'com.google.android.material:material:<version>'
Replace <version>
with the appropriate library version. Always ensure you're using the latest stable version.
Finally, to leverage Material Components fully, make sure your app's theme inherits from a Material Components theme (e.g., Theme.MaterialComponents.Light.DarkActionBar
).
Customizing Floating Action Button appearance in Android:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_add" app:backgroundTint="@color/colorAccent" app:borderWidth="0dp" app:elevation="6dp" app:fabSize="normal" app:rippleColor="@color/colorAccentDark" app:layout_anchor="@id/bottomAppBar" app:layout_anchorGravity="bottom|end" />
Changing color and style of Floating Action Button in Android:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_add" app:backgroundTint="@color/customFabColor" app:borderWidth="2dp" app:elevation="8dp" app:fabSize="mini" app:rippleColor="@color/customFabRippleColor" app:layout_anchor="@id/bottomAppBar" app:layout_anchorGravity="bottom|end" />
Material Design theming for Floating Action Button Android:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" style="@style/Widget.MaterialComponents.FloatingActionButton" ... />
Using styles and themes with FloatingActionButton in Android:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" style="@style/MyFloatingActionButton" ... />
<!-- styles.xml --> <style name="MyFloatingActionButton" parent="Widget.MaterialComponents.FloatingActionButton"> <item name="backgroundTint">@color/customFabColor</item> <item name="borderWidth">2dp</item> <!-- Add other style attributes as needed --> </style>
Adding icon and text to Floating Action Button with theming:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_add" app:backgroundTint="@color/colorAccent" app:borderWidth="0dp" app:elevation="6dp" app:fabSize="normal" app:rippleColor="@color/colorAccentDark" app:layout_anchor="@id/bottomAppBar" app:layout_anchorGravity="bottom|end" app:labelText="Add" />
Floating Action Button animations and theming in Android:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" style="@style/Widget.MaterialComponents.FloatingActionButton.Extended" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_add" android:contentDescription="@string/add" android:text="@string/add" app:backgroundTint="@color/colorAccent" app:rippleColor="@color/colorAccentDark" app:layout_anchor="@id/bottomAppBar" app:layout_anchorGravity="bottom|end" />
Themed Floating Action Button with CoordinatorLayout in Android:
<androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" style="@style/Widget.MaterialComponents.FloatingActionButton" ... /> <!-- Add other layout elements as needed --> </androidx.coordinatorlayout.widget.CoordinatorLayout>
Applying custom colors to FloatingActionButton Android:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_add" app:backgroundTint="@color/customFabBackground" app:rippleColor="@color/customFabRippleColor" ... />
Floating Action Button theming in XML layout in Android:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" style="@style/Widget.MaterialComponents.FloatingActionButton" ... />
Changing size and elevation of FloatingActionButton with theming:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="56dp" android:layout_height="56dp" android:src="@drawable/ic_add" app:elevation="12dp" ... />
Floating Action Button states and theming in Android:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" style="@style/Widget.MaterialComponents.FloatingActionButton" app:backgroundTint="@color/customFabColorStateList" ... />
Custom drawable for FloatingActionButton in Android theming:
<com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/custom_fab_icon" ... />
Creating themed Floating Action Button with Kotlin in Android:
val fab: FloatingActionButton = findViewById(R.id.fab) fab.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.customFabColor)) fab.setImageResource(R.drawable.ic_add)