How to prevent my snackbar text from being truncated on Android?

arinte picture arinte · Aug 26, 2015 · Viewed 8.7k times · Source

I am displaying a snackbar with a fairly long text message and on the phone in portrait mode it looks fine.

Phone Portrait mode

But on the tablet it seems to only allow 1 line of text so it gets ellipsis-ed

Tablet Landscape mode

Is there anyway I can get it to be 2 line in tablet landscape?

Answer

Szymon Chaber picture Szymon Chaber · Jul 18, 2017

What's important and not stated in other answers is that you need to use Snackbar's view.

So:

Snackbar snackbar = Snackbar.make(rootView, R.string.message, Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
TextView snackTextView = (TextView) snackbarView.findViewById(com.google.android.material.R.id.snackbar_text);

snackTextView.setMaxLines(2);
snackbar.show();

Note: This may also help with devices with lower screen density.

P.S If you're not using AndroidX (which is recommended), and are still using the legacy support libraries, please use android.support.design.R.id.snackbar_text instead of com.google.android.material.R.id.snackbar_text for getting the Snackbar's internal TextView.