3

I am working with the Material Design components in the support library version 28.0.0.

I want to show a snackbar that when the text inside the action button is too long it be shown in a different line than the message of the snack bar.

Following the Material design documentation of these components it seems to be possible using the default snackbar as shown here:

https://material.io/design/components/snackbars.html#implementation

But using this code:

var snackbar:Snackbar = Snackbar.make(root, message, Snackbar.LENGTH_SHORT)
    snackbar.setAction(action, View.OnClickListener {  })
    snackbar.show()

If the action text is long it doesn't move to the next line.

The root layout is a CoordinatorLayout.

So I do not know what I am missing in the snackbar to make it work.

Thanks!

acabezas
  • 731
  • 1
  • 7
  • 20

2 Answers2

4

The problem is actually an issue of android itself, the attribute loading from the default android dimen file is broken so the required attribute that is used to calculate the orientation of the elements of the snackbar is never set to the proper value. The issue has been informed and the solution will be pushed to the source code soon.

Workaround: set the attribute maxActionInlineWidth directly in your main theme and you can pickup the values from @dimen/design_snackbar_action_inline_max_width that is the one that should be used by android.

Example:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="maxActionInlineWidth">@dimen/design_snackbar_action_inline_max_width</item>
</style>
acabezas
  • 731
  • 1
  • 7
  • 20
1

If you go through the answer to THIS question, you can do something like below code for the action button(actually textview):

var snackbar:Snackbar = Snackbar.make(root, message, Snackbar.LENGTH_SHORT)
val snackbarView = snackbar.view
val tv = snackbarView.findViewById<TextView>(android.support.design.R.id.snackbar_action)
tv.maxLines = 2
snackbar.show()
ankuranurag2
  • 2,300
  • 15
  • 30
  • Thanks for the answer, but the action button it is a button. The implemenetation of the snackbar has this. . Also the maxLine is to allow more lines inside a text, what i want is that the style of the snackbar to change as it is in the material.io guide – acabezas Jan 11 '19 at 11:51
  • If you know from where you want to break the line, then you can add **`\n**` in the string. `btn.text= "ThisIsA\nVeryLongText"`. This more kind of a workaround. – ankuranurag2 Jan 11 '19 at 13:54