Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Snackbar textsize too big

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.

enter image description here

Why text is so big? I was expecting that it would be usual 16sp size, as it is everywhere in my application.

like image 683
c0rp Avatar asked Dec 14 '22 08:12

c0rp


1 Answers

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));
like image 50
Doug Stevenson Avatar answered Jan 05 '23 17:01

Doug Stevenson