How to show Snackbar when Activity starts?
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();
}
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();
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();