I have a Fragment class and a normal HelpFunctionsclass. I want the Fragment class to show a Snackbar by calling a method in the HelpFunctionsclass.
In the Fragment class I call the following code after a certain button has been pressed:
HelpFunctions.showSnackBar(getActivity(), getString(R.string.message_ratingSubmittedNotSuccessfully), binding.getRoot());
And in the HelpFunctions class the method for displaying the Snackbar looks like this:
public static void showSnackBar(Activity activity, String message, View root){
Snackbar currentSnackBar = Snackbar.make( root, message, Snackbar.LENGTH_LONG);
View sbView = currentSnackBar.getView();
sbView.setBackgroundColor(ContextCompat.getColor(activity, R.color.colorBlue));
currentSnackBar.show();
}
When I execute my code I get the following error message "java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.". I looked for a solution to this problem and found this answer: java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view. Here it is stated that I should use findViewById(android.R.id.content)
for the root. Unfortunately, when I use this in my method showSnackBar
in the line Snackbar currentSnackBar = Snackbar.make( findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG);
I get the error message from the compiler "Cannot resolve method 'findViewById' in 'HelpFunctions'".
Any idea why I am getting this error message and why I can't change it with findViewById? How else can I show a Snackbar?