How to show Snackbar when starting Activity?

I want to show android Snackbar (android.support.design.widget.Snackbar) when the action starts just like we show Toast .

But the problem is that we have to specify the parent layout when creating the Snackbar as follows:

 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 provide a parent layout when we show the Snackbar at the beginning of the action without any click events (if this is a click event, we could easily pass the parent view)?

+74
android material-design android-snackbar
Jun 22 '15 at 11:10
source share
7 answers

Just specify any View inside XML Activity's . For example, you can specify id for the root group of views 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(); } 
+157
Jun 22 '15 at 11:16
source share
— -

I had problems displaying the snack bar so far. Here is the easiest way to display a snack bar. To display it as the start of the main activity, just put these two lines inside OnCreate()

  Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "Welcome To Main Activity", Snackbar.LENGTH_LONG); snackbar.show(); 

PS Just make sure you import Android Design Support. (As mentioned in the question).

For Kotlin

 Snackbar.make(findViewById(android.R.id.content), message, Snackbar.LENGTH_SHORT).show() 
+14
Aug 06 '17 at 13:37 on
source share

try it

 Snackbar.make(findViewById(android.R.id.content), "Got the Result", Snackbar.LENGTH_LONG) .setAction("Submit", mOnClickListener) .setActionTextColor(Color.RED) .show(); 
+3
Sep 24 '17 at 7:55
source share

calling this method in onCreate

 Snackbar snack = Snackbar.make( (((Activity) context).findViewById(android.R.id.content)), message + "", Snackbar.LENGTH_SHORT); snack.setDuration(Snackbar.LENGTH_INDEFINITE);//change Duration as you need //snack.setAction(actionButton, new View.OnClickListener());//add your own listener View view = snack.getView(); TextView tv = (TextView) view .findViewById(android.support.design.R.id.snackbar_text); tv.setTextColor(Color.WHITE);//change textColor TextView tvAction = (TextView) view .findViewById(android.support.design.R.id.snackbar_action); tvAction.setTextSize(16); tvAction.setTextColor(Color.WHITE); snack.show(); 
+2
Dec 28 '16 at 2:05
source share

You can try this library. This is the default wrapper for the android diner. https://github.com/ChathuraHettiarachchi/CSnackBar

 Snackbar.with(this,null) .type(Type.SUCCESS) .message("Profile updated successfully!") .duration(Duration.SHORT) .show(); 

It contains several types of bookmarks and even a built-in travel bag

+1
Feb 03 '17 at 7:29
source share

Utility feature for show 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 usage in Activity

  showSnackBar(this, "No internet") showSnackBar(this, "No internet", duration = Snackbar.LENGTH_LONG) showSnackBar(activity, "No internet", "OK", View.OnClickListener { // handle click }) 

Fragmentation example

  showSnackBar(getActivity(), "No internet") 

Hope this helps

+1
Oct 23 '18 at 7:28
source share

This can be done simply by using the following codes inside onCreate. Using the default Android layout

 Snackbar.make(findViewById(android.R.id.content),"Your Message",Snackbar.LENGTH_LONG).show(); 
0
Dec 28 '18 at 12:31
source share



All Articles