How to show Snackbar when Activity starts?

152,710

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();
Share:
152,710
Sudheesh Mohan
Author by

Sudheesh Mohan

Android Developer, Gamer, Birdwatcher.

Updated on July 08, 2022

Comments

  • Sudheesh Mohan
    Sudheesh Mohan almost 2 years

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

    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
    John Cummings over 8 years
    For the root view, you can also generally use findViewById(android.R.id.content) as indicated here stackoverflow.com/a/4488149/1518546
  • Anand Savjani
    Anand Savjani over 8 years
    View parentLayout = findViewById(R.id.root_view); throws nullpointer exception in 5.0 or lower version. How to solve this problem ?
  • Sudheesh Mohan
    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
    Ishaan over 7 years
    I believe it throws NullpointerException if you are not using CoordinatorLayout as the root.
  • Nikola
    Nikola over 6 years
    Don't forget to include the support design library : 'com.android.support:design:27.0.0'
  • alena_fox_spb
    alena_fox_spb over 5 years
    on 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
    Jarin Rocks over 5 years
    check the updated answer. It will solve your problem. Use android's default layout
  • alena_fox_spb
    alena_fox_spb over 5 years
    yes, when I changed from window to android.R.id.content all start work. So I wrote my comment to solve someones else issue with it in future)
  • Arpit Patel
    Arpit Patel over 3 years
    'getColor()' deprecated in api 23. Use ContextCompat.getColor(context, R.color.color_name) instead.