21

I build Snackbar by this code:

Snackbar sb = Snackbar.make(drawer,  "message", Snackbar.LENGTH_LONG)
       .setAction("action", new View.OnClickListener() {
       @Override
       public void onClick(View view) {

       }
});

Now I want to change the typeface of message and action button but can't find any solution, How to do that?

Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35
user5510211
  • 297
  • 1
  • 3
  • 13

10 Answers10

44

You can set TypeFace by getting view from Snack bar

TextView tv = (TextView) (mSnackBar.getView()).findViewById(android.support.design.R.id.snackbar_text);
Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/font_file.ttf");
tv.setTypeface(font);

For AndroidX, use resource ID com.google.android.material.R.id.snackbar_text

Steve Blackwell
  • 5,904
  • 32
  • 49
Ashish Agrawal
  • 1,977
  • 2
  • 18
  • 32
15

Styling Both the Snackbar Text and Action

You can use the same method to style both the snackbar_text and snackbar_action.

Once you've created a snackbar, you can use the following to get the Views associated with the text and the action and then apply whatever adjustments to the view.

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 to be font size 20 and Bold, and the Text to be size 16 and allow up to 3 lines.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
15

For AndroidX

android.support.design.R.id.snackbar_text won't be available.

Use com.google.android.material.R.id.snackbar_textinstead.

If you are using kotlin, then I prefer you to use extension function:

fun Snackbar.changeFont()
{
    val tv = view.findViewById(com.google.android.material.R.id.snackbar_text) as TextView
    val font = Typeface.createFromAsset(context.assets, "your_font.ttf")
    tv.typeface = font
}

and call it like:

mSnakeBar.changeFont()
Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
  • 2
    createFromAsset didn't work for me and the app crashed every time saying that it can't find my font i used getFont from ResourceCompat like this `val font = ResourcesCompat.getFont(context, R.font.my_font)` – tahaak67 Feb 08 '21 at 14:28
5

In addition to this answer: now package to find snackbar's textview by id is

val snackText = snackView.findViewById<TextView>(
                    com.google.android.material.R.id.snackbar_text)
STAYER
  • 451
  • 5
  • 4
4

Get the snack bar view and apply customization

TextView tv = (TextView) sb.getView().findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
tv.setTypeface(Typeface.createFromAsset(
                    getAssets(),
                    "fonts/ur_file.ttf"));

Or this

SpannableStringBuilder snackbarText = new SpannableStringBuilder();
snackbarText.append("Add ");
int boldStart = snackbarText.length();
snackbarText.append("bold color");
snackbarText.setSpan(new ForegroundColorSpan(0xFFFF0000), boldStart, snackbarText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
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();

Or you can give a look at this and this.

Thank you.

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
2

Starting from Support Library 26, fonts can be used as resources.

val mainTextView = view.findViewById(com.google.android.material.R.id.snackbar_text) as TextView
val font = ResourcesCompat.getFont(applicationContext, R.font.your_font)
mainTextView.typeface = font
Andrii Artamonov
  • 622
  • 8
  • 15
2

For those who are still reading this 7 years later, you can change the text appearance of the snackbar for the whole app by overriding its theme parent in your themes.xml file.

<style name="Theme.App" parent="Theme.MaterialComponents.*">
    ...
    <item name="snackbarTextViewStyle">@style/Widget.App.SnackbarTextView</item>
</style>


<style name="Widget.App.SnackbarTextView" parent="Widget.MaterialComponents.Snackbar.TextView">
    <item name="android:textAppearance">@style/your_snackbar_text_style</item>
</style>

And then in your styles.xml file insert the your_snackbar_text_style style there

    <style name="your_snackbar_text_style">
        <item name="android:fontFamily">@font/your_font</item>
    </style>
0

Get assets

AssetManager assets = context.getAssets();

Get Typeface

Typeface typeface = Typeface.createFromAsset(assets,PATH OF .TTF FILE);

Path: font/robotoregular.ttf (if .ttf file is stored in assets/font path)

Vibhor Chopra
  • 647
  • 4
  • 14
0

if you want to change the font of Action Button and textview use this code :

Snackbar.make(this,message,Snackbar.LENGTH_LONG).also {snackbar ->
  snackbar.setAction("ok"){
     snackbar.dismiss()
  }
  val actionButton = snackbar.view.findViewById(com.google.android.material.R.id.snackbar_action) as Button
  val textview = snackbar.view.findViewById(com.google.android.material.R.id.snackbar_text) as TextView
  val font = Typeface.createFromAsset(context.assets, "fonts/your_custom_font")
  actionButton.typeface = font
  textview.typeface = font
  ViewCompat.setLayoutDirection(snackbar.view,ViewCompat.LAYOUT_DIRECTION_RTL)

}.show()

0

For every one that crashes when calling createFromAsset() you can use

Font font = ResourcesCompat.getFont(getApplicationContext(), R.font.your_font);
Achintha Isuru
  • 2,662
  • 18
  • 24