Elye
Elye

Reputation: 60311

How to set Bold text to Android Snackbar Action Text?

We could set the color for the Action Text of Snackbar using setActionTextColor as documented in https://developer.android.com/reference/android/support/design/widget/Snackbar.html. However, is there a way to make the Text BOLD?

Thanks!!

Upvotes: 9

Views: 8245

Answers (5)

abby
abby

Reputation: 410

The Latest way currently in 2022 is to do this by using HtmlCompat.fromHtml not Html.fromHtml:

In Java:

    Snackbar snackbar = Snackbar.make(view, 
                                      Html.fromHtml("<b>Your text here</b>"), 
                                      Snackbar.LENGTH_LONG);
    snackbar.show();

In Kotlin:

    val snackbar = Snackbar.make(requireView(),
           HtmlCompat.fromHtml("<b>Your text here</b>", HtmlCompat.FROM_HTML_MODE_LEGACY), 
           Snackbar.LENGTH_SHORT)

    snackbar.show()

Upvotes: 0

Dev_Anay
Dev_Anay

Reputation: 1

The best way to bold the text in snackbar is to given in one of the solutions here, but I will make it more simple:

Snackbar snackbar = Snackbar
                    .make(yourViewHere, Html.fromHtml("<b>Email Copied To Clipboard</b>"), Snackbar.LENGTH_LONG);

 snackbar.show();

Upvotes: 0

Joshua Pinter
Joshua Pinter

Reputation: 47611

Use snackbar_action Resource ID.

It turns you that you can use the same method to style the Snackbar's Action text that you use to style the Snackbar's Message text.

You just have to use the Resource ID snackbar_action instead of snackbar_text.

Here's an example of setting the style for both the Message text and the Action text.

Snackbar snackbar = Snackbar.make( ... ); // Create the Snackbar however you like.

TextView snackbarActionTextView = (TextView) snackbar.getView().findViewById( android.support.design.R.id.snackbar_action );
snackbarActionTextView.setTextSize( 20 );
snackbarActionTextView.setTypeface( snackbarActionTextView.getTypeface(), Typeface.BOLD );

TextView snackbarTextView = (TextView) snackbar.getView().findViewById( android.support.design.R.id.snackbar_text );
snackbarTextView.setTextSize( 16 );
snackbarTextView.setMaxLines( 3 );

In my example, I've set the Action text to have a TextSize of 20 and a bold Typeface, and the Message text to have a TextSize of 16 and allow up to 3 lines.

Upvotes: 16

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364978

With the version 1.1.0 of the Material Components you can define in your app theme the attribute snackbarButtonStyle:

<style name="AppTheme" parent="Theme.MaterialComponents.*">
  <item name="snackbarButtonStyle">@style/snackbar_button</item>
</style>

In this way you can define a custom style for the button used for the Action.
You can change text color, background colore and you can define your custom style for the textAppearance.
Something like:

  <style name="snackbar_button" parent="@style/Widget.MaterialComponents.Button.TextButton.Snackbar">
      <item name="android:textAppearance">@style/snackbar_button_textappearance</item>
  </style>

  <style name="snackbar_button_textappearance"  parent="@style/TextAppearance.MaterialComponents.Button">
    <item name="android:textStyle">bold</item>
  </style>

enter image description here

Upvotes: 1

BrentM
BrentM

Reputation: 5757

The easiest method to add BOLD text to your Snackbar text is to use the Android Html class to generate the text to pass to the Snackbar.make() function. Here is an example:

Snackbar.make(view, Html.fromHtml("Add <b>bold</b> to Snackbar text"), Snackbar.LENGTH_LONG).show();

An alternative method is to use the SpannableStringBuilder class.

SpannableStringBuilder snackbarText = new SpannableStringBuilder();
snackbarText.append("Add ");
int boldStart = snackbarText.length();
snackbarText.append("bold");
snackbarText.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), boldStart, snackbarText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
snackbarText.append(" to Snackbar text");
Snackbar.make(view, snackbarText, Snackbar.LENGTH_LONG).show();

Upvotes: 2

Related Questions