1

I wanted to justify the text of a TextView but I could not find any way to do that on the TextView so I have created a WebView.

The code to set the text on my WebView is the following:

WebView webview = (WebView) view.findViewById(R.id.webview);
webview.loadData(getString(R.string.webview), "text/html; charset=utf-8", "utf-8");
webview.setBackgroundColor(Color.TRANSPARENT);

And it works well, the text is being show justified (because I have created a body tag with style="text-align:justify;).

The problem is that, as I have loaded the text into the WebView, it spends some seconds to charge the text. Therefore, the rest of the layout is being shown before the text have appeared, making a strange visual effect.

I have tried to wait until the WebView is fully loaded (How to listen for a WebView finishing loading a URL?) but the text is never shown. Here is the code that I have by the moment:

webview.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
       webview.loadData(getString(R.string.webview), "text/html; charset=utf-8", "utf-8");
       webview.setBackgroundColor(Color.TRANSPARENT);
   }
});

So, how can I show a justify text at the same time as the rest of the layout?

Thanks in advance!

Community
  • 1
  • 1
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
  • 1
    https://github.com/programingjd/justified – CommonsWare Dec 24 '16 at 16:45
  • Duplicate Question of : http://stackoverflow.com/questions/1292575/android-textview-justify-text – iDeveloper Dec 24 '16 at 17:14
  • @tahaDev I do not think it is a duplicate of that question. I know how to justify the text. I am asking about a concrete issue. – Francisco Romero Dec 24 '16 at 17:31
  • @CommonsWare It gives to me the following error when I am trying to use compile on gradle: _Error:(9, 0) Could not find method compile() for arguments [com.uncopt:android.justified:1.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler._ – Francisco Romero Dec 25 '16 at 00:30
  • You may have made some mistakes in your `build.gradle` file. You may wish to ask a separate Stack Overflow question, where you show the file that you modified, along with the error message. – CommonsWare Dec 25 '16 at 00:41

1 Answers1

0

this is the picture: enter image description here

this is the code that I used in my project it doesnt take so much to load

 void initVebView(WebView wvContent_Detail, String body) {
        String HTML1 = "<html><head><style type=\"text/css\">@font-face      {font-family: MyFont;src: url(\"file:///android_asset/%s\")}body {font-family: MyFont;font-size: medium;text-align: justify; line-height: %spx;}</style></head><body dir='rtl'>";
    String HTML2 = "</body></html>";
    String HTML3 = "<span style=\"font-weight:bold\">%s<br/><br/>%s</span>";

    String HTML4 = "<style>img{display: inline;height: auto;max-width: 100%;</style>";

    String str = String.format(HTML1, "IRANSansMobile_UltraLight.ttf", 25);


    String content = body.replace("text-align:", "");
    content = content.replace("font-family:", "");
    content = content.replace("line-height:", "");
    content = content.replace("dir=", "");
    content = content.replace("width=", "width=\"100%;\"");

    String myHtmlString = str + content + HTML2;

    wvContent_Detail.loadDataWithBaseURL(null, HTML4 + myHtmlString, "text/html", "UTF-8", null);

    WebSettings webSettings = wvContent_Detail.getSettings();
    webSettings.setDefaultFontSize(20);
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
}

this is the xml :

  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   tools:context="ir.tenthwindow.BaseModule.ui.FragmentVideo">

   <ScrollView
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <WebView 
           android:id="@+id/webview_product_details"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_marginLeft="10dp"
           android:layout_marginRight="10dp" />
   </ScrollView>
  </FrameLayout>

you can use your font instead. Hope this helped .

iDeveloper
  • 1,699
  • 22
  • 47