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 Snackbar
in Android using the Material Design guidelines allows you to make sure the Snackbar looks consistent with the rest of your app's design. Here's a guide on how to theme Material Design Snackbars in Android:
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.
Your app's theme should also inherit from a Material Components theme, such as Theme.MaterialComponents.Light.DarkActionBar
.
First, let's see how to display a basic Snackbar:
import com.google.android.material.snackbar.Snackbar Snackbar.make(view, "This is a Snackbar", Snackbar.LENGTH_LONG).show()
Replace view
with the root view of your current layout or any view inside it.
You can modify several attributes of the Snackbar to suit your app's theme. Let's go through them:
Create a style for the Snackbar in your res/values/styles.xml
:
<style name="CustomSnackbar" parent="Widget.MaterialComponents.Snackbar"> <item name="android:background">@color/snackbarBackground</item> <item name="backgroundTint">@color/snackbarBackground</item> <item name="android:textColor">@color/snackbarTextColor</item> <item name="actionTextColor">@color/snackbarActionTextColor</item> </style>
Replace the color resources (snackbarBackground
, snackbarTextColor
, and snackbarActionTextColor
) with your desired colors.
Then, set this style in your main theme:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> ... <item name="snackbarStyle">@style/CustomSnackbar</item> </style>
You can also customize the appearance of a Snackbar programmatically:
val snackbar = Snackbar.make(view, "This is a Snackbar", Snackbar.LENGTH_LONG) snackbar.setBackgroundTint(ContextCompat.getColor(this, R.color.snackbarBackground)) snackbar.setTextColor(ContextCompat.getColor(this, R.color.snackbarTextColor)) snackbar.setActionTextColor(ContextCompat.getColor(this, R.color.snackbarActionTextColor)) snackbar.setAction("Action") { // Handle action click } snackbar.show()
Replace the color resources (snackbarBackground
, snackbarTextColor
, and snackbarActionTextColor
) with your desired colors.
If you need more customization than what's available via theming, you can also create a custom layout for the Snackbar. But remember, too much deviation from the default Snackbar design might confuse users, so use custom layouts judiciously.
These are the basic theming techniques for the Material Snackbar. There are more advanced customization possibilities, but these should cover most common use cases. Always refer to the official Material Components documentation for any detailed guidance on advanced theming.
Customizing Material Design Snackbar appearance in Android:
val snackbar = Snackbar.make(view, "This is a Snackbar", Snackbar.LENGTH_SHORT) val snackbarView = snackbar.view snackbarView.setBackgroundColor(ContextCompat.getColor(this, R.color.customSnackbarColor)) snackbar.show()
Changing color and style of Material Design Snackbar in Android:
val snackbar = Snackbar.make(view, "Styled Snackbar", Snackbar.LENGTH_LONG) val snackbarView = snackbar.view snackbarView.setBackgroundColor(ContextCompat.getColor(this, R.color.customSnackbarBackground)) snackbar.setTextColor(ContextCompat.getColor(this, R.color.customSnackbarText)) snackbar.setBackgroundTint(ContextCompat.getColor(this, R.color.customSnackbarTint)) snackbar.show()
Material Design theming for Snackbars in Android:
val snackbar = Snackbar.make(view, "Themed Snackbar", Snackbar.LENGTH_SHORT) val snackbarView = snackbar.view snackbarView.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary)) snackbar.show()
Using styles and themes with Material Design Snackbar Android:
<style name="MySnackbarStyle" parent="Widget.MaterialComponents.Snackbar"> <item name="backgroundTint">@color/customSnackbarColor</item> <item name="android:textColor">@color/customSnackbarText</item> <!-- Add other style attributes as needed --> </style>
val snackbar = Snackbar.make(view, "Styled Snackbar", Snackbar.LENGTH_SHORT) snackbar.view.setBackgroundResource(R.style.MySnackbarStyle) snackbar.show()
Adding custom icons and text to Material Design Snackbar with theming:
val snackbar = Snackbar.make(view, "Customized Snackbar", Snackbar.LENGTH_SHORT) val snackbarView = snackbar.view snackbarView.setBackgroundColor(ContextCompat.getColor(this, R.color.customSnackbarBackground)) val icon = ContextCompat.getDrawable(this, R.drawable.ic_custom_icon) snackbar.setIcon(icon) snackbar.show()
Material Design Snackbar animations and theming in Android:
val snackbar = Snackbar.make(view, "Animated Snackbar", Snackbar.LENGTH_SHORT) snackbar.animationMode = BaseTransientBottomBar.ANIMATION_MODE_SLIDE snackbar.show()
Themed Material Design Snackbar with CoordinatorLayout in Android:
val snackbar = Snackbar.make(coordinatorLayout, "Themed Snackbar", Snackbar.LENGTH_SHORT) val snackbarView = snackbar.view snackbarView.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary)) snackbar.show()
Applying custom colors to Material Design Snackbar Android:
val snackbar = Snackbar.make(view, "Colored Snackbar", Snackbar.LENGTH_SHORT) snackbar.setBackgroundTint(ContextCompat.getColor(this, R.color.customSnackbarColor)) snackbar.show()
Material Design Snackbar theming in XML layout in Android:
<com.google.android.material.snackbar.Snackbar android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Themed Snackbar" app:backgroundTint="@color/customSnackbarColor" ... />
Changing duration and behavior of Material Design Snackbar with theming:
val snackbar = Snackbar.make(view, "Custom Duration Snackbar", 3000) snackbar.animationMode = BaseTransientBottomBar.ANIMATION_MODE_FADE snackbar.show()
Material Design Snackbar states and theming in Android:
<com.google.android.material.snackbar.Snackbar style="@style/Widget.MaterialComponents.Snackbar" app:backgroundTint="@color/customSnackbarColorStateList" ... />
Creating themed Material Design Snackbar with Kotlin in Android:
val snackbar = Snackbar.make(view, "Themed Snackbar", Snackbar.LENGTH_SHORT) snackbar.setBackgroundTint(ContextCompat.getColor(this, R.color.customSnackbarColor)) snackbar.show()