3

I'm trying to fully justify text in Android with auto-hyphenation. I have achieved full justification using a WebView as explained here. I have read a couple of threads on auto-hyphenation in Android, but none of them apply to WebViews. I've tried using the new CSS3 hyphens:auto (including -webkit-hyphens:auto), but Android WebKit does not support it yet.

I've found a blog post here that mentions using the Hyphenator JavaScript, but I have no idea how to implement it (JavaScript and HTML are next on the to do list). Due to the size of the .js file, I don't want to simply use webView.loadUrl("javascript:someFunction()");

Here is the code I am currently using:

public class TestWebView extends Activity {
@Override
public void onCreate (Bundle savedState) {
    WebView webView;
    super.onCreate(savedState);
    webView = new WebView(this);
    setContentView(webView);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.loadUrl("file:///android_asset/WorkingExample.html");
    }
}

Here is the HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
    <head>
        <title>Hyphenator.js</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <style type="text/css">
            body {
                width:30%;
                margin-left:35%;
                margin-right:35%;
            }
            .text {
                text-align:justify;
            }
    </style>
    <script src="Hyphenator.js" type="text/javascript"></script>
    <script type="text/javascript">
        Hyphenator.config({
            displaytogglebox : true,
            minwordlength : 4
        });
        Hyphenator.run();
        </script>
    </head>
    <body>
        <h1>Example of using Hyphenator.js</h1>
        <h2>Deutsch</h2>
        <p class="hyphenate text" lang="de">Deutschsprachige Beispieltexte haben natürlicherweise längere Wortzusammensetzungen als englischsprachige. Aber auch <span lang="en">“hyphenation”</span> ist ein ziemlich langes Kompositum.</p>
        <p class="hyphenate text" lang="de">Verändern Sie die Fenstergrösse um den Effekt der Silbentrennung zu sehen.</p>
        <h2>English</h2>
        <p class="hyphenate text" lang="en">English words are shorter in the average then german words. <span lang="de">«Silbentrennungsalgorithmus»</span> for example is quite long.</p>
        <p class="hyphenate text" lang="en">Resize the window to see hyphenation in effect.</p>
        <h2>Links</h2>
        <p class="hyphenate text" lang="en">Not only words but also links like <a href="http://code.google.com/p/hyphenator/">http://code.google.com/p/hyphenator/</a> are processed. But in a special manner (using zero width space).</p>
    </body>
</html>

Stored alongside the html file is the Hyphenator.js file. Opening the HTML file in my computer browser works as expected, but on the phone I'm getting no luck:Computer browser (FF) vs WebView Eventually, I want the text to be generated dynamically, but just getting this to work would be a huge help. Thanks.

Community
  • 1
  • 1
willlma
  • 7,353
  • 2
  • 30
  • 45

2 Answers2

0

So u have WorkingExample.html and Hyphenator.js in file:///android_asset/ ? I think you forgot to download the "patterns" folder (in your case you need de.js and en-us.js under file:///android_asset/patterns/). You can get them here.

Update: Grab everything you need and put it into your assets:

enter image description here

WorkingExample.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
    <head>
        <title>Hyphenator.js</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <script src="Hyphenator.js" type="text/javascript"></script>
        <script type="text/javascript">
            Hyphenator.config({
                minwordlength : 4
            });
        </script>
    </head>
    <body>
        <p id="text" class="hyphenate text" lang="en"></p>
    </body>
</html>

MainActivity:

public class MainActivity extends Activity {

    WebView mWebView;
    String mText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mText = "English words are shorter in the average then german words. <span lang=\"de\">Silbentrennungsalgorithmus</span> for example is quite long.";

        mWebView = (WebView) findViewById(R.id.mWebView);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("file:///android_asset/WorkingExample.html");
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                mWebView.loadUrl("javascript:(function() { "
                        + "document.getElementById('text').innerHTML='" + mText
                        + "';" + "Hyphenator.run();" + "})()");
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

activity_main:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/mWebView"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true" />

</RelativeLayout>
Markus Rubey
  • 5,153
  • 2
  • 21
  • 17
  • Thanks for this. I'm not working on the project anymore, but will let you know how this works when I get back to it. – willlma Aug 12 '13 at 14:30
0

The following library supports hyphenation. It does all types of text alignment (left/right/center/justified) and hyphenation for you. Not all languages have been added but can be added as necessary. This library uses NO WEBVIEWS and SUPPORTS SPANNABLES and allows for LONG TEXT.

LIBRARY: https://github.com/bluejamesbond/TextJustify-Android

ANDROID: 2.2 to 5.X

SETUP

DocumentView documentView = addDocumentView(new StringBuilder("Your long text content"), DocumentView.PLAIN_TEXT);
documentView.getDocumentLayoutParams().setTextAlignment(TextAlignment.JUSTIFIED);
documentView.getDocumentLayoutParams().setHyphenator(new Hyphenator(HyphenPattern.PT));
documentView.getDocumentLayoutParams().setHyphenated(true);
Mathew Kurian
  • 5,949
  • 5
  • 46
  • 73