I'm trying to change Snackbar text size, because I want it to be easy readable for users with low vision. I write this method to create Snackbar
private Snackbar getSnackbar(int msgId) {
final Snackbar snackbar = Snackbar.make(
mView,
msgId,
Snackbar.LENGTH_LONG);
final View view = snackbar.getView();
final TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.snackbar_textsize));
return snackbar;
}
And corresponding dimens.xml
is:
<resources>
<dimen name="header_date_font_size">16sp</dimen>
<dimen name="snackbar_textsize">16sp</dimen>
</resources>
Here is screenshot of resulting Snackbar.
Why text is so big? I was expecting that it would be usual 16sp size, as it is everywhere in my application.
getDimension()
returns the value of a resource that has its dimension scaling factor already applied.
So if the resource is specified in sp
, then the return value is the number of actual pixels in size.
But you're feeding that value into setText()
and saying that the measurement is in sp
, when it is actually in pixels. So it's applying another scaling factor.
Instead, just say:
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.snackbar_textsize));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With