Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 557
arinte Avatar asked Aug 26 '15 13:08

arinte


People also ask

How do I get rid of snackbar on Android?

OnClickListener() { @Override public void onClick(View v) { } }). show(); The snackbar can be dismissed by a swipe.

How do I use snackbar?

Snackbar in android is a new widget introduced with the Material Design library as a replacement of a Toast. Android Snackbar is light-weight widget and they are used to show messages in the bottom of the application with swiping enabled. Snackbar android widget may contain an optional action button.


4 Answers

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.

like image 155
Szymon Chaber Avatar answered Oct 17 '22 11:10

Szymon Chaber


You can reference the SnackBar's TextView like this:

TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);

And then operate on the TextView itself by changing its max lines:

tv.setMaxLines(3)
like image 28
Mateusz Jablonski Avatar answered Oct 17 '22 10:10

Mateusz Jablonski


Based on Snackbar source code you can see that on devices with width more or equal than 600 dp they change max lines to 1. So You can also just add:

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

into your res\values\*.xml values

like image 21
wrozwad Avatar answered Oct 17 '22 10:10

wrozwad


A more elaborate and thorough example with context would be this:

// Create on click listener
final  OnClickListener positiveButtonClickListener = new OnClickListener()
{
  @Override
  public void onClick(View v)
  {
    // Do your action - e.g. call GooglePlay  to update app
    openGooglePlayAppUpdate();
  }
};

// Create snack bar instance
Snackbar sBar = Snackbar.make(findViewById(R.id.some_view_to_bind),  // You bind here e.g. layout, or form view
                              R.string.snack_bar_message,
                              Snackbar.LENGTH_INDEFINITE)
                        // Set text and action to the snack bar
                        .setAction(android.R.string.ok, positiveButtonClickListener);

// Now get text view of your snack bar ...
TextView snckBarTv = (TextView) offerUpdate.getView().findViewById(android.support.design.R.id.snackbar_text);
snckBarTv.setMaxLines(5); // ... and set max lines
sBar.show(); // and finally display snack bar !
like image 4
Sold Out Avatar answered Oct 17 '22 11:10

Sold Out