How to change the Snackbar text alignment to center ? bellow code is not working
Snackbar snack = Snackbar.make(findViewById(android.R.id.content), intent.getStringExtra(KEY_ERROR_MESSAGE), Snackbar.LENGTH_LONG);
View view = snack.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(ContextCompat.getColor(LoginActivity.this, R.color.red_EC1C24));
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
snack.show();
Try following code it's work for set snackbar text in center:
EDIT
The appearance of
Snackbar
was changed inSupport
library v23 so the correct answer now is:Another solution, using XML:
Theme.MaterialComponents
, and has to delcare its own styling forSnackbar
and itsTextView
as follows:Theme.MaterialComponents
adds a margin to the snackbar, and it changes its animation. If you want theAppCompat
style, do as follows:TextView
:The code below does the trick for me:
Try this:
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 ofandroid.support.design.R.id.snackbar_text
.Code: