How to show Snackbar when Activity starts?
Solution 1
Just point to any View
inside the Activity's
XML. You can give an id to the root viewGroup, for example, and use:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
View parentLayout = findViewById(android.R.id.content);
Snackbar.make(parentLayout, "This is main activity", Snackbar.LENGTH_LONG)
.setAction("CLOSE", new View.OnClickListener() {
@Override
public void onClick(View view) {
}
})
.setActionTextColor(getResources().getColor(android.R.color.holo_red_light ))
.show();
//Other stuff in OnCreate();
}
Solution 2
I have had trouble myself displaying Snackbar until now.
Here is the simplest way to display a Snackbar. To display it as your Main Activity Starts, just put these two lines inside your OnCreate()
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "Welcome To Main Activity", Snackbar.LENGTH_LONG);
snackbar.show();
P.S. Just make sure you have imported the Android Design Support.(As mentioned in the question).
For Kotlin,
Snackbar.make(findViewById(android.R.id.content), message, Snackbar.LENGTH_SHORT).show()
Solution 3
Try this
Snackbar.make(findViewById(android.R.id.content), "Got the Result", Snackbar.LENGTH_LONG)
.setAction("Submit", mOnClickListener)
.setActionTextColor(Color.RED)
.show();
Solution 4
A utils function for show snack bar
fun showSnackBar(activity: Activity, message: String, action: String? = null,
actionListener: View.OnClickListener? = null, duration: Int = Snackbar.LENGTH_SHORT) {
val snackBar = Snackbar.make(activity.findViewById(android.R.id.content), message, duration)
.setBackgroundColor(Color.parseColor("#CC000000")) // todo update your color
.setTextColor(Color.WHITE)
if (action != null && actionListener!=null) {
snackBar.setAction(action, actionListener)
}
snackBar.show()
}
Example using in Activity
showSnackBar(this, "No internet")
showSnackBar(this, "No internet", duration = Snackbar.LENGTH_LONG)
showSnackBar(activity, "No internet", "OK", View.OnClickListener {
// handle click
})
Example using in Fragment
showSnackBar(getActivity(), "No internet")
Hope it help
Solution 5
It can be done simply by using the following codes inside onCreate. By using android's default layout
Snackbar.make(findViewById(android.R.id.content),"Your Message",Snackbar.LENGTH_LONG).show();
Comments
-
Sudheesh Mohan almost 2 years
I want to show android
Snackbar
(android.support.design.widget.Snackbar)
when the activity starts just like we show aToast
.But the problem is we have to specify the parent layout when creating
Snackbar
like this:Snackbar.make(parentlayout, "This is main activity", Snackbar.LENGTH_LONG) .setAction("CLOSE", new View.OnClickListener() { @Override public void onClick(View view) { } }) .setActionTextColor(getResources().getColor(android.R.color.holo_red_light )) .show();
How to give parent layout when we show
Snackbar
at the start of the activity without any click events (If it was a click event we could've easily pass the parent view)? -
John Cummings over 8 yearsFor the root view, you can also generally use
findViewById(android.R.id.content)
as indicated here stackoverflow.com/a/4488149/1518546 -
Anand Savjani over 8 yearsView parentLayout = findViewById(R.id.root_view); throws nullpointer exception in 5.0 or lower version. How to solve this problem ?
-
Sudheesh Mohan over 8 years@AnandSavjani The code is working perfectly with me for 5.0 and below 5.0, there must be some error in your layout(root_view). If you are using fragment you should write rootview.findViewById(R.id.your_parent_view); And also dont give same id as layout name.
-
Ishaan over 7 yearsI believe it throws NullpointerException if you are not using CoordinatorLayout as the root.
-
Nikola over 6 yearsDon't forget to include the support design library : 'com.android.support:design:27.0.0'
-
alena_fox_spb over 5 yearson android version 5.x - suddenly
getWindow().getDecorView()
return ALL screen, include 'bottom buttons' (such home, back etc). So my snack was showing down to this buttons, not in app -
Jarin Rocks over 5 yearscheck the updated answer. It will solve your problem. Use android's default layout
-
alena_fox_spb over 5 yearsyes, when I changed from
window
toandroid.R.id.content
all start work. So I wrote my comment to solve someones else issue with it in future) -
Arpit Patel over 3 years'getColor()' deprecated in api 23. Use ContextCompat.getColor(context, R.color.color_name) instead.