How to show Snackbar when Activity starts?

后端 未结 9 1250
你的背包
你的背包 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:47

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

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

    Simple way to show some text:

    Snackbar.make(view, "Sample Text", Snackbar.LENGTH_SHORT).show();
    

    and to show text with button:

    Snackbar.make(view, "Sample Text", Snackbar.LENGTH_SHORT).setAction("Ok", new View.OnClickListener() {
                @Override 
                public void onClick(View view) {
    
                } 
            }).show();
    
    0 讨论(0)
提交回复
热议问题