Display Snackbar without CoordinatorLayout
public static void setSnackBar(View root, String snackTitle) {
Snackbar snackbar = Snackbar.make(root, snackTitle, Snackbar.LENGTH_SHORT);
snackbar.show();
View view = snackbar.getView();
TextView txtv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
txtv.setGravity(Gravity.CENTER_HORIZONTAL);
}
Just call the above method and pass any parent layout such as LinearLayout
, RelativeLayout
or ScrollView
, for example:
setSnackBar(layout,"This is your SnackBar"); //here "layout" is your parentView in a layout
Do not forget to find your view using findViewById()
.
Try this easy piece of code.. a dummy view from the android framework itself is used
Snackbar.make(findViewById(android.R.id.content),"Your Text Here",Snackbar.LENGTH_SHORT).show();