Simon
Simon

Reputation: 19948

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?

Upvotes: 6

Views: 6815

Answers (5)

Barel elbaz
Barel elbaz

Reputation: 656

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>

Upvotes: 1

Gibolt
Gibolt

Reputation: 47297

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

Upvotes: 2

Jorgesys
Jorgesys

Reputation: 126563

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); 

Upvotes: 7

Abu Ruqaiyah
Abu Ruqaiyah

Reputation: 1546

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?

Upvotes: 3

ch3tanz
ch3tanz

Reputation: 3070

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(); 

Upvotes: 2

Related Questions