Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style SnackBar in theme app

I need help. How can I change the design of the text in snackbar in styles app? The change in the code does not interest me. I found the following code. But it is not working for me. Why is that? My theme is derived from @style/Theme.AppCompat.Light.DarkActionBar". I would be very grateful for the help.

<style name="TextAppearance.Design.Snackbar.Message" parent="android:TextAppearance">         <item name="android:textSize">10sp</item>         <item name="android:textColor">#FEFEFE</item>     </style>     <style name="TextAppearance.Design.Snackbar.Action" parent="android:TextAppearance">         <item name="android:textSize">16sp</item>         <item name="android:textColor">#FEFEFE</item>     </style> 
like image 718
Konstantin Veretelnikov Avatar asked Sep 06 '15 15:09

Konstantin Veretelnikov


People also ask

How do you set a snackbar position?

Snackbars should be placed at the bottom of a UI, in front of app content.

What is snackbar in design?

Snackbars provide brief feedback about an operation through a message at the bottom of the screen. Snackbars contain a single line of text directly related to the operation performed. They may contain a text action, but no icons. Toasts (Android only) are primarily used for system messaging.

How do I change the color of my font on snackbar?

If you want to define text colors in your views that are not default, then use android:primaryTextColor and reference that attribute in your custom views. For reference: // create instance Snackbar snackbar = Snackbar. make(view, text, duration); // set action button color snackbar.

How long should a snackbar last?

Snackbars appear without warning, and don't require user interaction. They automatically disappear from the screen after a minimum of four seconds, and a maximum of ten seconds.


2 Answers

With the Material Components Library you can globally change the snackbar style in your app theme:

<style name="AppTheme" parent="Theme.MaterialComponents.*">     <!-- Style to use for Snackbars in this theme. -->     <item name="snackbarStyle">@style/Widget.MaterialComponents.Snackbar</item>     <!-- Style to use for action button within a Snackbar in this theme. -->     <item name="snackbarButtonStyle">@style/Widget.MaterialComponents.Button.TextButton.Snackbar</item>     <!-- Style to use for message text within a Snackbar in this theme. -->     <item name="snackbarTextViewStyle">@style/Widget.MaterialComponents.Snackbar.TextView</item>     .... </style> 

Note:

  • snackbarStyle and snackbarButtonStyle require the version 1.1.0
  • snackbarTextViewStyle requires the version 1.2.0.

For example:

  <style name="snackbar_style" parent="@style/Widget.MaterialComponents.Snackbar">     <item name="android:layout_margin">32dp</item>   </style>    <style name="snackbar_button" parent="@style/Widget.MaterialComponents.Button.TextButton.Snackbar">       <item name="backgroundTint">@color/secondaryLightColor</item>       <item name="android:textColor">@color/primaryDarkColor</item>   </style>    <style name="snackbar_text" parent="@style/Widget.MaterialComponents.Snackbar.TextView">     <item name="android:textColor">@color/secondaryLightColor</item>   </style> 

enter image description here

It is just an example how to change also the parent style for the action button. You can use for example a standard Widget.MaterialComponents.Button:

  <style name="snackbar_button" parent="@style/Widget.MaterialComponents.Button">       <item name="backgroundTint">@color/secondaryLightColor</item>       <item name="android:textColor">@color/primaryDarkColor</item>   </style> 

To change the background color of the SnackBar you can use:

<style name="snackbar_style" parent="@style/Widget.MaterialComponents.Snackbar">     <!-- using backgroundTint the alpha layer is ignored -->     <item name="backgroundTint">@color/....</item> </style> 

Or if you prefer:

   <style name="MySnackbar" parent="@style/Widget.MaterialComponents.Snackbar">         <item name="materialThemeOverlay">@style/snackbar_overlay</item>         <!-- If you want to avoid the alpha level for the color that is overlaid on top of the background color-->         <item name="backgroundOverlayColorAlpha">1.0</item>     </style>     <style name="snackbar_overlay">         <item name="colorOnSurface">....</item>     </style> 
like image 105
Gabriele Mariotti Avatar answered Sep 20 '22 22:09

Gabriele Mariotti


you need this: tools:override="true"

<resources xmlns:tools="http://schemas.android.com/tools">     <style name="TextAppearance.Design.Snackbar.Message" parent="android:TextAppearance" tools:override="true">         <item name="android:textColor">@color/text</item>         <item name="android:textSize">50sp</item>     </style> </resources> 
like image 36
Amos Avatar answered Sep 22 '22 22:09

Amos