Android Multiline Snackbar

36,260

Solution 1

Just set the maxLines attribute of Snackbars Textview

View snackbarView = snackbar.getView();
TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setMaxLines(5);  // show multiple line

If you're using the more recent "com.google.android.material:material:1.0.0"dependency, then you will use this: com.google.android.material.R.id.snackbar_text to access the Snackbar's TextView.

You can use even R.id.snackbar_text as well. it's work for me.

Solution 2

One can override the predefined value used for that in values.xml of the app

<integer name="design_snackbar_text_max_lines">5</integer>

This value is used by Snackbar by default.

Solution 3

Snackbar snackbar =  Snackbar.make(view, "Text",Snackbar.LENGTH_LONG).setDuration(Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
TextView tv= (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
tv.setMaxLines(3); 
snackbar.show();

Solution 4

With the Material Components Library you can define it using with the snackbarTextViewStyle attribute in the app theme:

<style name="AppTheme" parent="Theme.MaterialComponents.*">
  ...
  <item name="snackbarTextViewStyle">@style/snackbar_text</item>
</style>

<style name="snackbar_text" parent="@style/Widget.MaterialComponents.Snackbar.TextView">
    ...
    <item name="android:maxLines">5</item>
</style>

enter image description here

Note: it requires the version 1.2.0 of the library.

Solution 5

Here is my finding on this :

Android does support multiline snackbars but it has a max limit of 2 lines which matches the design guideline where it says that the height of multiline snack bar should be 80dp (almost 2 lines)

To verify this, i used the cheesesquare android sample project. If i use following string:

Snackbar.make(view, "Random Text \n When a second snackbar is triggered while the first is displayed", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();

In this case, i can see the multiline snack bar with the text of 2nd line, i.e. "When a second snackbar is triggered" but if i change this code to following implementation:

Snackbar.make(view, "Random Text \n When \n a second snackbar is triggered while the first is displayed", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();

I can only see the "Random Text\nWhen ...". This means that design library is intentionally forcing the textview to be of max 2 lines.

Share:
36,260

Related videos on Youtube

Dima Kornilov
Author by

Dima Kornilov

Updated on February 28, 2022

Comments

  • Dima Kornilov
    Dima Kornilov about 2 years

    I'm trying to leverage new Snackbar from Android Design Support Library to display multiline snackbar, as shown in http://www.google.com/design/spec/components/snackbars-toasts.html#snackbars-toasts-specs:

    import android.support.design.widget.Snackbar;
    
    final String snack = "First line\nSecond line\nThird line";
    Snackbar.make(mView, snack, Snackbar.LENGTH_LONG).show();
    

    It displays only First line... on my Nexus 7. How to make it display all lines?

    PS: I tried Toast and it displayed all lines.

    • mudit
      mudit almost 9 years
      AFAIK, snackbars are meant for quick user alert/feedback and have been designed to support it i.e. "Single Line". If you want to show an alert/feedback which has multilines, i would suggest show a dialog as user can take action after reading your message.
    • Dima Kornilov
      Dima Kornilov almost 9 years
      @mudit think about internationalization. Even if English string can fit into single line, German may not. Also Google provided Material Design spec for multiline snackbar (I linked it in the question) - why if it should be avoided?
  • Dima Kornilov
    Dima Kornilov almost 9 years
    I accepted this answer however material specs mention snackbar with 112dp too: i.imgur.com/1ACCoQb.png
  • Alp
    Alp almost 9 years
    can you name the height of multiline snack bar in dimension?
  • Dan Dar3
    Dan Dar3 over 8 years
  • Oasis Feng
    Oasis Feng over 8 years
    This practice should be avoided, since it's a private integer defined by design library, which might be renamed or deleted without generating any compilation or runtime error in your app.
  • Jigar
    Jigar over 8 years
    Don't follow any guidlines if you don't want to accept that guide line. If google is that much right in its guidline and tool than why gradle build system is headache for the developer?
  • Christopher Rucinski
    Christopher Rucinski over 8 years
    @DimaKornilov it will be set to 112dp if and only if you have text that is fully 2 lines long AND you provide an action. In that case the action will cause the Snackbar to expand to 112dp.
  • devconsole
    devconsole almost 8 years
    Yes, but I think it should be clarified that this is very different from accessing private resources of the OS. This will work as long as you stick to the same version of the design library. If you update to a newer version you have to test your app thoroughly anyway, just put this on your checklist.
  • stannums
    stannums about 7 years
    text somehow always null
  • Chantell Osejo
    Chantell Osejo about 7 years
    Double check your code. Here's the AOSP source code for the layout of Snackbar You'll notice there is a TextView object there, so it isn't possible for text to be null.
  • natario
    natario about 7 years
    Might be even better to use findViewsWithText, since you already know the text. This solution might break in a couple situations (e.g. if our text view becomes a child of a child of the snackbar layout).
  • Naveen Dissanayake
    Naveen Dissanayake about 6 years
    Just letting you know that this does not work on kindle fire devices which seems to only show 1 line. @Nilesh answer did work.
  • Chris
    Chris over 5 years
    Is there a way to calculate the actual number of lines that are needed, or do we have to guess?
  • Nilesh Senta
    Nilesh Senta over 5 years
    @Chris may android have but you can specify maxLine if text is small length it will automatically becomes shrink...
  • ban-geoengineering
    ban-geoengineering over 5 years
    Downvoted as I don't like the idea of the checklist getting longer.
  • osipxd
    osipxd over 5 years
    Wat? You just assigned null to final variable and called it in listener? You want 100% NPE?
  • mtsahakis
    mtsahakis over 5 years
    For the latest androidx libs should be com.google.android.material.R.id.snackbar_text. But IMHO the ID might change in the future so this practice should be avoided.
  • android developer
    android developer almost 5 years
    It doesn't offer to use/import snackbar_text .
  • android developer
    android developer almost 5 years
    @OsipXD Fixed for him :)
  • Chapz
    Chapz over 4 years
    Since this relies on the TextView ID that can change with each version of the support lib, this answer is not really a permanent solution, but a hack. It may work, but it may also randomly break in production.
  • fobbymaster
    fobbymaster over 4 years
    Is there a recommended API for this? I agree that this method is a hack, but if there's no API exposed for this, then there's no alternative.
  • Vitor Hugo Schwaab
    Vitor Hugo Schwaab about 4 years
    This won't work for me. SnackbarLayout has another view inside of it. So snackbarLayout.getChildAt() will never return a TextView.
  • Anton Malyshev
    Anton Malyshev almost 4 years
    Good point, but the library is still in alpha state (May 2020) :)
  • Gumby The Green
    Gumby The Green almost 4 years
    @androiddeveloper I can't get it to work again either, so I've updated the answer.
  • android developer
    android developer almost 4 years
    Crashes now : java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setSingleLine(boolean)' on a null object reference
  • Gumby The Green
    Gumby The Green almost 4 years
    @androiddeveloper Do you have this dependency: implementation 'com.google.android.material:material:1.1.0' and is your Snackbar the com.google.android.material.snackbar.Snackbar one? If not, try android.support.design.R.id.snackbar_text as the resource ID.
  • android developer
    android developer almost 4 years
    Those are what you get when you create a new project (and I did), and it crashed.
  • Gumby The Green
    Gumby The Green almost 4 years
    @androiddeveloper It must be an issue with the device or OS version. I just tried it with a new "Basic Activity" project and it worked fine, and it seems to be working for others on this page.
  • Gumby The Green
    Gumby The Green almost 4 years
    @androiddeveloper I'd try another solution like this one or this one, which don't require a resource ID to access the TextView.
  • android developer
    android developer almost 4 years
    I tested on Pixel 4 with Android 10. Also tested on emulator with Android 10. And tried to update to 1.2.0-alpha06 . Still crash. At first I thought it's because "view" wasn't the correct one, but even after fixing it, and doing findViewById on MainActivity itself, it still couldn't find this view. However, using snackbar.view.findViewById works fine.
  • android developer
    android developer almost 4 years
    BTW, no need for import com.google.android.material.R as MaterialR . You can use the ID directly: com.google.android.material.R.id.snackbar_text , but it's nice to finally see a useful usage of "as" in imports. How did you find the correct ID though? The layout-inspector doesn't show any attribute of the SnackBar for some reason...
  • Gumby The Green
    Gumby The Green almost 4 years
    @androiddeveloper The apply function in my code should cause the view to belong to the snackbar, so I wonder if your code was missing it. I realize the import is unnecessary. I just don't like having full package names in the middle of my code - it's noisy. That ID can be found here and the path for referencing it can be seen here.
  • android developer
    android developer almost 4 years
    OK I know what happened. I used it in onClickListener like on a new project, so the "view" was of the FAB. Seems odd that the IDE doesn't warn about this. The "view has double possible meaning here... So this is one of the examples when Kotlin shortcuts can cause bugs quite easily... A possible fix would be to use getView() instead of view which could be an issue. As for the ID, I think the DDMS tool can get it, and also the "developer assistant" app.
  • Androidcoder
    Androidcoder almost 4 years
    Now that Toast is even more useless than usual with the deprecation of setview() and getview() as of API level 30, customizing the Snackbar has become more important. Hopefully Snackbar customization won't get deprecated too.
  • 4gus71n
    4gus71n over 2 years
    Nice and clean, I like this solution! FYI the full id is com.google.android.material.R.id.snackbar_text
  • Hitesh Sarsava
    Hitesh Sarsava over 2 years
    This is not permanent solution. Please refer below Gabriele Mariotti solution. Its permanent solution.
  • lasec0203
    lasec0203 over 2 years
    Love seeing a short and sweet kotlin extension that I can readily copy/paste into my project.
  • Dharamveer Mithilesh Gupta
    Dharamveer Mithilesh Gupta about 2 years
    It is actually the finest solution, and importantly working one 👍✔️