4

I am attempting to create a snackbar any time I check for a network connection and don't find one in a NetworkClient class with static methods. Each of these methods is passed only a Context parameter from the calling activity. Ideally I would like to create a private method within the class like this:

private void showNetworkErrorSnackbar(Context mContext) {
    Snackbar.make( whatgoeshere?, mContext.getString(R.string.network_error_message),  Snackbar.LENGTH_LONG).show();
}

that can be called from any of the NetworkClient methods. However I am having trouble figuring out how to get a view to put in for the first parameter. It would be too complicated to change the parameters of the methods to include a view as these methods are already being called in every activity.

drawinfinity
  • 315
  • 4
  • 21

2 Answers2

2

Of course after typing it out I was able to reason exactly what to search for. The answer to getting the view from only a context is here: https://stackoverflow.com/a/21146009/4888701 and it was able to solve my problem. Hopefully this can help someone else with a similar issue.

Community
  • 1
  • 1
drawinfinity
  • 315
  • 4
  • 21
-1

You can create a dummy layout and then use it as a parameter in showing Snackbar as shown below

RelativeLayout relativeLayout;
Snackbar snackbar = Snackbar
                .make(relativeLayout, "Your message here!", Snackbar.LENGTH_LONG);
        snackbar.show();
Mehul Kanzariya
  • 888
  • 3
  • 27
  • 58