How to change background color of the snackbar?

后端 未结 16 742
温柔的废话
温柔的废话 2020-12-07 19:28

I am showing snackbar in DialogFragment Within the Positive click of alertDialog. Here is my code snippet.

Snackbar snackbar = Snackbar.make(view, \"Please e         


        
16条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 20:13

    As none of the other answers provided a custom style override (that I consider one of the safest update way to do that) I post here my solution.

    I post a solution that already address the new AndroidX (support design 28) theme.

    Provided that your application use a custom them called MyAppTheme in your AndroidManifest.xml:

    
    

    Create (if you haven't already) values/style.xml file overriding the theme used by your application:

    
    
    
    
    

    and provide your colors in your values/colors.xml file

    
    
        #008577
        #00574B
        #D81B60
        #D81B60
    
    

    UPDATE 2020

    As the above solution removes the round corner of the snacker bacause setting the background this way uses the legacy snackbar design, if you want to preserve the material design you can.

    1. If you are targeting API 21+

    replace android:background with android:backgroundTint

    
    
    
    1. If you are targeting API < 21 then if you decide to use legacy snackbar for API < 21 you could set your abouve MySnackbarStyle in res/values-21/ folder and leave the previous — legacy — style in your res/values folder.

    2. If you are targeting API < 21 and you want to have the material style of the snackbar also in this lower API levels you can change your snackbar style in your res/values/ this way:

    
    
    

    and borrow your my_snackbar_background from the official repo, this way:

    
    
        
        
    
    

    Here is a playground repo.

提交回复
热议问题