Search code examples
androidandroid-coordinatorlayout

Is it possible to add some buttons or other different View's to Snackbar?


If it posible could you tell me how can I do it? Else, please, tell me what can be used instead of Snackbar?


Solution

  • You can do it like this. BUT, I don't recommend to use this workaround. Usually you don't need to put custom layout in snackbar. Snackbar was developed as a unified way for all apps to notify about something.

     CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.main_content);//coordimator layout where you use snackbar
                snackbar = Snackbar.make(coordinatorLayout, "", Snackbar.LENGTH_INDEFINITE);
                Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();//get the snackbar's view
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View snackView = inflater.inflate(R.layout.custom_layout, null);
                snackView.setBackgroundColor(Color.WHITE);
                layout.addView(snackView, 0);//add our custom view to snackbar
                snackbar.show();
    

    example custom snackbar