How to change the Snackbar text alignment to center ? bellow code is not working
Snackbar snack = Snackbar.make(findViewById(android.R.id.content), in
The code below does the trick for me:
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
if(tv!=null) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN)
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
}
Try this:
// make snackbar
Snackbar mSnackbar = Snackbar.make(view, R.string.intro_snackbar, Snackbar.LENGTH_LONG);
// get snackbar view
View mView = mSnackbar.getView();
// get textview inside snackbar view
TextView mTextView = (TextView) mView.findViewById(android.support.design.R.id.snackbar_text);
// set text to center
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
mTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
else
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
// show the snackbar
mSnackbar.show();
The above conditional setTextAlignment() OR setGravity()
solution didn't work for me.
For bulletproof centered text:
setGravity()
setTextAlignment()
for API >= 17And note the support library change in AndroidX. If using AndroidX, lookup the TextView by com.google.android.material.R.id.snackbar_text
instead of android.support.design.R.id.snackbar_text
.
Code:
TextView sbTextView = (TextView) findViewById(com.google.android.material.R.id.snackbar_text);
sbTextView.setGravity(Gravity.CENTER_HORIZONTAL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
sbTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
Try following code it's work for set snackbar text in center:
Snackbar snackbar = Snackbar.make(main_layout, "This is snack", Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
layout.setGravity(Gravity.CENTER);
TextView mTextView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
mTextView.setGravity(Gravity.CENTER_HORIZONTAL);
mTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
snackbar.show();
Another solution, using XML:
Theme.MaterialComponents
, and has to delcare its own styling for Snackbar
and its TextView
as follows:<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
<item name="snackbarStyle">@style/MySnackbar</item>
<item name="snackbarTextViewStyle">@style/MySnackbarTextView</item>
</style>
Theme.MaterialComponents
adds a margin to the snackbar, and it changes its animation. If you want the AppCompat
style, do as follows:<style name="MySnackbar" parent="@style/Widget.MaterialComponents.Snackbar">
...
<item name="android:layout_margin">0dp</item>
<item name="animationMode">slide</item>
</style>
TextView
:<style name="MySnackbarTextView" parent="@style/Widget.MaterialComponents.Snackbar.TextView">
...
<item name="android:gravity">center_horizontal</item>
<item name="android:textAlignment">center</item>
</style>
tv.setGravity(Gravity.CENTER_HORIZONTAL);
EDIT
The appearance of Snackbar
was changed in Support
library v23 so the correct answer now is:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
} else {
tv.setGravity(Gravity.CENTER_HORIZONTAL);
}