Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make text fit to screen (text-wrap) in WebView with KitKat

I'm having an issue in my WebView with Android 4.4. Before KitKat, text was fitting automatically with all devices resolutions in webviews. But today it's not fitting automatically anymore with 4.4. I think it's because of the WebView's update based on Chromium with KitKat.

Here are 2 screenshots of the same page :

One from Galaxy Nexus (Android 4.3)

One from Nexus 5 (Android 4.4 and WebView based on Chromium)

Do you know if there is a new option to fit text with screen in webview ?

NB: I don't think my layout as something to do with my issue but here it is anyway:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>
like image 518
Antoine Avatar asked Nov 07 '13 12:11

Antoine


1 Answers

If you can edit the html (using a simple string replace for instance), add the style word-wrap: break-word to the text container html element. e.g: If the top level element inside the body of the html is <p>, change to<p style="word-wrap: break-word;">. The webview will then wrap around the text and fit to screen.

like image 103
Arc Avatar answered Nov 05 '22 14:11

Arc