Change the background color of action button in snackbar
Asked Answered
Q

5

3

how can I change the background color of action button in snackbar or make it dissapear (grey background)?

enter image description here

i use this code:

        Snackbar mysnack = Snackbar.make(main_layout, getResources().getString(R.string.snack_1), 5000);
            View view = mysnack.getView();
            TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
            tv.setTextColor(getResources().getColor(R.color.text_light));
            mysnack.setActionTextColor(getResources().getColor(R.color.text_light));
            mysnack.setAction("RATE", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Uri uri = Uri.parse(getResources().getString(R.string.snack_url));
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }
            });
            TypedValue typedValue = new TypedValue();
            getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
            final int color = typedValue.data;
            mysnack.getView().setBackgroundColor(color);
            mysnack.show();

and my question is not duplicate. I ask for BACKGROUND color and not the text color. First we read, then we understand, then we think and then we decide to write that someone's question is a duplicate.

Quartz answered 27/9, 2016 at 6:51 Comment(4)
Possible duplicate of Snackbar action text color not changingLumumba
Can you please add code ?Aglet
you can try making a custom snackbar. https://mcmap.net/q/183568/-how-to-customize-snackbar-39-s-layoutAccredit
#32454446 try this outSultanate
H
4

I had the same issue. Found out it can be a bug of new Material theme. Main theme of my application is:

<style name="AppTheme" parent="@style/Theme.MaterialComponents.Light.NoActionBar">

If I change it to AppCompat snack button's gray background dissapears. Eventually I found that it was because of the bug.

My solution was (I needed Material theme and can not simply change it to AppCompat): Found button id for the snack. It is "@id/snackbar_action":

val snackButton: Button = yourSnackbar.getView().findViewById(R.id.snackbar_action)

and then changed it background to null:

snackButton.setBackground(null)
Hodden answered 8/8, 2019 at 17:3 Comment(1)
nice solution 👍Nubbly
A
1

This code snippet may help you out:

Snackbar snackbar = Snackbar.make(
                    coordinatorLayout,
                    "Snackbar: floatingActionButton1 (normal) clicked",
                    Snackbar.LENGTH_LONG);
            snackbar.setActionTextColor(Color.RED);
            View snackbarView = snackbar.getView();
            snackbarView.setBackgroundColor(Color.WHITE);
            TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
            textView.setTextColor(Color.BLUE);

for Reference Click Here

Amplifier answered 27/9, 2016 at 7:6 Comment(0)
L
1

With the Material Components Library you can use the snackbarButtonStyle attribute in your app theme.
Something like:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
    <!-- Style to use for action button within a Snackbar in this theme. -->
    <item name="snackbarButtonStyle">@style/Widget.App.Button.TextButton.Snackbar</item>
</style>


<style name="Widget.App.Button.TextButton.Snackbar" parent="Widget.MaterialComponents.Button.TextButton.Snackbar">
    <item name="backgroundTint">@color/colorSecondary</item>
</style>

enter image description here

Lynellelynett answered 13/9, 2020 at 8:26 Comment(0)
L
0

For Xamarin you can try this one

 snackbar.View.SetBackgroundColor(Android.Graphics.Color.ParseColor("#32CD32"));
Lejeune answered 11/8, 2020 at 21:42 Comment(0)
B
0

If you want to change action button background color..

View sbView = snackbar.getView();
Button button=
(Button) sbView.findViewById(com.google.android.material.R.id.snackbar_action);
button.setBackgroundColor(getResources().getColor(R.color.white));

If you want to change action button text color..

snackbar.setActionTextColor(getResources().getColor(R.color.colorAccent));
Bellew answered 30/8, 2020 at 0:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.