问题
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)?
回答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();
}
回答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()
回答3:
Try this
Snackbar.make(findViewById(android.R.id.content), "Got the Result", Snackbar.LENGTH_LONG)
.setAction("Submit", mOnClickListener)
.setActionTextColor(Color.RED)
.show();
回答4:
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();
回答5:
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
回答6:
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
回答7:
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();
来源:https://stackoverflow.com/questions/30978457/how-to-show-snackbar-when-activity-starts