How to show Snackbar when Activity starts?

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)?

166751 次浏览

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();
}

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();

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

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()

Try this

Snackbar.make(findViewById(android.R.id.content), "Got the Result", Snackbar.LENGTH_LONG)
.setAction("Submit", mOnClickListener)
.setActionTextColor(Color.RED)
.show();

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

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();

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.

Simple way to show some text:

Snackbar.make(view, "<text>", Snackbar.LENGTH_SHORT).show();
// or
Snackbar.make(view, "<text>", Snackbar.LENGTH_LONG).show();

and to show text with a button:

Snackbar.make(view, "<text>", Snackbar.LENGTH_SHORT).setAction("<button_text>", new View.OnClickListener() {
@Override
public void onClick(View view) {
// operation to perform when the button is clicked
}
}).show();

For those developers who use data binding in their project.

Snackbar.make(binding.getRoot(), "This is your text", Snackbar.LENGTH_LONG)
.setAction("CLOSE", view -> {
//close
})
.setActionTextColor(getResources().getColor(android.R.color.holo_red_light ))
.show();

if you use Snackbar in Activity use this code

View view = findViewById(android.R.id.content); Snackbar.make(view, "This is main activity", Snackbar.LENGTH_LONG).show();