4

I am implementing web view in my android application for getting dynamic text. But the problem is text size is bigger/small in different screen sizes. How to resolve this problem? I am using like this..

webSettings.setDefaultFontSize(20);

2 Answers2

1
    public static int dp2px(Context context, float dpVal){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, context.getResources().getDisplayMetrics());
    }
webSettings.setDefaultFontSize(dp2px(context,20)/webview.getScale());
or:
webSettings.setDefaultFontSize(dp2px(context,20)*webview.getScale());
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
0

You should make you're font size a px (pixels) or dp (Density-independent pixels). Just use pixels, because it will always be 20 pixels on each screen and won't change. If you use dp, the screen size will vary on the density of pixels and the text size can change. So your code will look like this

webSettings.setDefaultFontSize(20px);

Read this: What is the difference between "px", "dp", "dip" and "sp" on Android?

Community
  • 1
  • 1
Jacob V.
  • 43
  • 8