How to show Snackbar when Activity starts?

后端 未结 9 1249
你的背包
你的背包 2020-12-12 20:24

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

相关标签:
9条回答
  • 2020-12-12 20:35

    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();
    
    0 讨论(0)
  • 2020-12-12 20:39

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

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

    This contains multiple types of snackbar and even a customview intergrated snackbar

    0 讨论(0)
  • 2020-12-12 20:40

    Try this

    Snackbar.make(findViewById(android.R.id.content), "Got the Result", Snackbar.LENGTH_LONG)
                            .setAction("Submit", mOnClickListener)
                            .setActionTextColor(Color.RED)
                            .show();
    
    0 讨论(0)
  • 2020-12-12 20:40

    You can also define a super class for all your activities and find the view once in the parent activity.

    for example

    AppActivity.java :

    public class AppActivity extends AppCompatActivity {
    
        protected View content;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            changeLanguage("fa");
            content = findViewById(android.R.id.content);
        }
    }
    

    and your snacks would look like this in every activity in your app:

    Snackbar.make(content, "hello every body", Snackbar.LENGTH_SHORT).show();
    

    It is better for performance you have to find the view once for every activity.

    0 讨论(0)
  • 2020-12-12 20:45

    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();
    }
    
    0 讨论(0)
  • 2020-12-12 20:45

    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

    0 讨论(0)
提交回复
热议问题