Android Multiline Snackbar

I am trying to use the new Snackbar in the Android Design support library to display a multi-line snack bar, 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 only displays First line... on my Nexus 7. How to display all the lines?

PS: I tried Toast and displayed all the lines.

+75
android android-support-library android-design-library
Jun 08 '15 at 9:32
source share
12 answers

Just set the maxLines attribute to Snackbars Textview

 View snackbarView = snackbar.getView(); TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text); textView.setMaxLines(5); // show multiple line 
+195
Jul 15 '15 at 11:24
source share

You can override the predefined value used for it in the application's values.xml

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

This value is used by default Snackbar.

+28
Sep 22 '15 at 15:02
source share

Here is my conclusion about this:

Android supports multi-line snackbars, but has a maximum limit of 2 lines, which corresponds to the design guide, which states that the height of the multi-line snack bar should be 80dp (almost 2 lines)

To make sure of this, I used a sample cheesesquare android samples. If I use the following line:

 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 see a multi-line snack panel with the text of the 2nd line, i.e. "When the second nook starts," but if I change this code to the following implementation:

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

I can only see "Random Text \ nWhen ... ". This means that the design library intentionally forces textview to have a maximum of 2 lines.

+10
Jun 08 '15 at 9:53 on
source share
 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(); 
+10
Aug 15 '16 at 5:48
source share

An alternative to sentences related to hard-coding a resource identifier for text content contained in a box is an iteration to find a TextView. It is more secure long-term and allows you to update the support library with minimal fear of identifier changes.

Example:

  public static Snackbar getSnackbar(View rootView, String message, int duration) { Snackbar snackbar = Snackbar.make(rootView, message, duration); ViewGroup snackbarLayout = (ViewGroup) snackbar.getView(); TextView text = null; for (int i = 0; i < snackbarLayout.getChildCount(); i++) { View child = snackbarLayout.getChildAt(i); // Since action is a button, and Button extends TextView, // Need to make sure this is the message TextView, not the // action Button view. if(child instanceof TextView && !(child instanceof Button)) { text = (TextView) child; } } if (text != null) { text.setMaxLines(3); } return snackbar; } 
+3
Jul 12 '16 at 17:34
source share

Instead of using setMaxLines, I use setSingleLine to make the textview wrap into its contents.

 String yourText = "First line\nSecond line\nThird line"; Snackbar snackbar = Snackbar.make(mView, yourText, Snackbar.LENGTH_SHORT); TextView textView = (TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text); textView.setSingleLine(false); snackbar.show(); 
+2
Jun 01 '17 at 10:51 on
source share

it works for me

 Snackbar snackbar = Snackbar.make(mView, "Your text string", Snackbar.LENGTH_INDEFINITE); ((TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text)).setSingleLine(false); snackbar.show(); 
+2
Jun 04 '17 at 11:15
source share

Late, but may be useful for someone:

 public void showSnackBar(String txt, View view){ final Snackbar snackbar = Snackbar.make(view,txt,Snackbar.LENGTH_INDEFINITE) .setAction("OK", new View.OnClickListener() { @Override public void onClick(View view) { //do something } }); View view = snackbar.getView(); TextView textView = (TextView) view.findViewById(android.support.design.R.id.snackbar_text); textView.setMaxLines(5); snackbar.show(); } 
+1
05 Oct '17 at 16:14
source share

Material Design Link - com.google.android.material.R.id.snackbar_text

 val snack = Snackbar.make(myView, R.string.myLongText, Snackbar.LENGTH_INDEFINITE).apply { view.findViewById<TextView>(com.google.android.material.R.id.snackbar_text).maxLines = 10 } snack.show() 
+1
May 28 '19 at 21:48
source share

A way to do this that won't crash if something changes in newer versions of the library:

 Snackbar.make(...).setAction(...) { ... }.apply { (view.findViewById<View?>(R.id.snackbar_text) as? TextView?)?.setSingleLine(false) }.show() 

And the way to do this without using identifiers is by setting an unlimited number of rows for all TextViews in Snackbar:

 @UiThread fun setAllTextViewsToHaveInfiniteLinesCount(view: View) { when (view) { is TextView -> view.setSingleLine(false) is ViewGroup -> for (child in view.children) setAllTextViewsToHaveInfiniteLinesCount(child) } } Snackbar.make(...).setAction(...) { ... }.apply { setAllTextViewsToHaveInfiniteLinesCount(view) }.show() 

Same function in Java:

 @UiThread public static void setAllTextViewsToHaveInfiniteLines(@Nullable final View view) { if (view == null) return; if (view instanceof TextView) ((TextView) view).setSingleLine(false); else if (view instanceof ViewGroup) for (Iterator<View> iterator = ViewGroupKt.getChildren((ViewGroup) view).iterator(); iterator.hasNext(); ) setAllTextViewsToHaveInfiniteLines(iterator.next()); } 
+1
Jun 13 '19 at 9:04 am
source share

In Kotlin you can just do

 Snackbar.make(rootView, "Yo!", Snackbar.LENGTH_LONG).apply { view.snackbar_text.setSingleLine(false) show() } 

You can replace setSingleLine(false) with maxLines = 3 if you want.

Android Studio should suggest adding

 import kotlinx.android.synthetic.main.design_layout_snackbar_include.view.* 
0
Dec 20 '18 at 9:49
source share

Just a quick comment if you use com.google.android.material:material , the prefix or package for R.id should be com.google.android.material

 val snackbarView = snackbar.view val textView = snackbarView.findViewById<TextView>(com.google.android.material.R.id.snackbar_text) textView.maxLines = 3 
0
Sep 13 '19 at 18:14
source share



All Articles