6

I'm trying to get hold of the Snackbar's TextView. This is the code I'm using:

 Snackbar snackbar = Snackbar.make(mRecyclerView, message, Snackbar.LENGTH_LONG);
 View view = snackbar.getView();
 TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);

Running the above always sets tv to null. I'm using design library version 23.0.0 and in the layout resource for the snackbar, I can see the TextView with id snackbar_text. What am I missing?

hata
  • 11,633
  • 6
  • 46
  • 69
vkislicins
  • 3,331
  • 3
  • 32
  • 62

4 Answers4

12

For those who came are having trouble finding an up-to-date answer on this, I'm here to help.

Many examples show the use of this res id:

android.support.design.R.id.snackbar_text

It has since been replaced with:

com.google.android.material.R.id.snackbar_text

Example:

Snackbar snackbar = Snackbar.make( /*however you decide to make it*/ );
View view = snackbar.getView();
TextView tv = (TextView)view.findViewById(android.support.design.R.id.snackbar_text);
//make your needed changes...
NateLillie
  • 1,026
  • 7
  • 8
2

I had the same problem and was able to fix it by not using the id from android.support.design.R.id.snackbar_text.

If you look at the source of the design support library you can see that R.id.snackbar_text is set to:

public static final int snackbar_text = 0x7f0c006b;

If you look at the value of android.support.design.R.id.snackbar_text in your App, you can see that the value is:

public static final int snackbar_text = 0x7f0b006b;

For me, this issue only happens if I use the Design Support Library in a library module.

Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
BauerMitFackel
  • 372
  • 5
  • 15
  • Right... this issue only happens if I use the Design Support Library in a library module, but your solution doesn't work for me. I tried replacing android.support.design.R.id.snackbar_text with R.id.snackbar_text and code compiles fine but textview still retruns null. Read my answer below to know how i solved the issue.. – Vinay Oct 13 '16 at 13:34
  • I wrote a small lib to be able to style snackbars and it looks like using R.id.snackbar_text works using support design lib 23.4.0. The code is available at https://github.com/UlrichRaab/snackbar-builder/blob/master/snackbar-builder/src/main/java/de/ulrichraab/snackbar/SnackbarStyle.java – BauerMitFackel Oct 14 '16 at 11:55
  • Thanks! Thanks! Thanks! You VERY help me :). – Rostislav Dugin Feb 09 '17 at 22:02
1

Following code should work:

View containerView = inflater.inflate(R.layout.fragment_foo, container, false); 
Snackbar snackbar = Snackbar.make(containerView, "snackbar text", Snackbar.LENGTH_SHORT); 
View snackbarView = snackbar.getView(); 
TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
text.setText("It works"); snackbar.show();
Jeffalee
  • 1,080
  • 1
  • 7
  • 15
  • Not sure you're reading that answer correctly. They are also getting `TextView` before calling .show() method – vkislicins Apr 07 '16 at 11:40
  • this is the code that works for me (in a `Fragment`): `View containerView = inflater.inflate(R.layout.fragment_foo, container, false); Snackbar snackbar = Snackbar.make(containerView, "snackbar text", Snackbar.LENGTH_SHORT); View snackbarView = snackbar.getView(); TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text); text.setText("It works"); snackbar.show();` – Jeffalee Apr 07 '16 at 11:48
  • No, this is not correct. You definitely, 100% don't want to inflate your fragment from within a fragment. And you should be able to provide any view to Snackbar, and Snackbar will search for the root parent on its own. – vkislicins Apr 07 '16 at 12:13
  • Why wouldn't you inflate your fragment within the fragment itself?? See [this link](http://developer.android.com/intl/es/training/basics/fragments/creating.html) – Jeffalee Apr 07 '16 at 12:41
0

This issues came only after I added library module with Design support dependency and I removed the design support dependency from my main module. I added the design support gradle dependency again in my main module and it works!

That means following dependency is added in both the main module "app" and a helper library module which is added as library in my "app":

compile "com.android.support:design:$project.ext.supportLibraryVersion"

where project.ext.supportLibraryVersion is defined in my app.build.gradle like below:

allprojects {
    project.ext {
    supportLibraryVersion="23.3.0"
    }
}

But I'm still not sure why it works after adding design support dependency in both modules while it should have worked if i add the dependency only in library module which i am anyway adding as library for my "app" module? Can anyone help me understand this? Thanks in advance!

Vinay
  • 1,859
  • 22
  • 26