简体   繁体   中英

Changing the text of an existing snackBar

I want to display a snackBar when my token becomes invalid. The snackbar will have an action attached to "Refresh" the token.

SnackBar outerSnackBar;

outerSnackBar =  Snackbar.make(coordinatorLayout, 
"Your request is unauthorized. Please refresh your token", 
Snackbar.LENGTH_INDEFINITE).setAction("Refresh"), new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                outerSnackBar.setAction("", null);
                                outerSnackBar.setText("Refreshing");
                                outerSnackBar.show();
   }
}).show();

When I click on the snackBar's "Refresh" action button, I want the existing snackBar's text to change to "Refreshing" and the action to display hence this is the code that I have written in the onClickListener.

However, when I click on the "Refresh" action button, the snackbar just dismisses itself.

Is there anyway to modify the text and action of an existing snackbar?

To only change the text, get the textview of the snackbar:

TextView tvSnackbarText =  snackbar.getView().findViewById(android.support.design.R.id.snackbar_text); 
tvSnackbarText.setText("Any text");

You mentioned "... and the action to display", so these shouldn't be necessary:

outerSnackBar.setAction("", null);
outerSnackBar.show();

Other useful ways are mentioned here How to set support library snackbar text color to something other than android:textColor?

I used to get the view and set the text with this code:

TextView snackBarText =  snackbar.getView().findViewById(android.support.design.R.id.snackbar_text); 
snackBarText.setText("Stackoverflow is cool!");

but now using AndroidX this is the correct way to get the view:

TextView snackBarText =  snackbar.getView().findViewById(com.google.android.material.R.id.snackbar_text); 

Better to show new SnackBar with message "Refreshing"

 SnackBar outerSnackBar =  Snackbar.make(coordinatorLayout, 
    "Your request is unauthorized. Please refresh your token", 
    Snackbar.LENGTH_INDEFINITE).setAction("Refresh"), new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Snackbar refreshingSnackBar = Snackbar
                                           .make(coordinatorLayout, "Refreshing...",
                                            Snackbar.LENGTH_SHORT);
                                    refreshingSnackBar.show();
       }
    }).show(); 

Just use Snackbar.setText

There is no need to manually search for the internal text view, which is super hacky.

snackbar.setText("New Snackbar Message")

It does not seem to dismiss the SnackBar as you mentioned

This is a sample solution for changing text and color.

Snackbar snack = Snakebar.make(parent, "first text", Snackbar.LENGTH_INDEFINITE);

//change text when snackbar is showing

snack.setText("new different text")

//change color when snackbar is showing

snack.getView().setBackgroundColor(ContextCompat.getColor(context, R.color.green);

notice, 'green' color must be define in colors.xml file for example:

<color name="green">#4CAF50</color>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM