0

I want to develop a simple android web browser app that support a Unicode font I provide. I did some research but I couldn't find a successful method to add a custom font for a android WebView. There are some solutions to add custom fonts for a WebView but they are all based on adding font in the CSS. but since I don't have control over the page I load inside the WebView is there a method to add a font directly to the WebView like setTypaFace() method for TextView.?

here is the code i have used so far to add custom font for webview:

package com.example.sinhalafontrendering;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
import android.widget.TextView;

import com.example.sinhalafontrendering.R.id;

public class MainActivity extends Activity {
    WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FontUtils.setCustomFont(findViewById(id.txtHelloWorld), getAssets());
        Typeface tf = Typeface.createFromAsset(getAssets(), "iskpota.ttf");
        TextView tv = (TextView) findViewById(id.txtHelloWorld);
        tv.setText("ශ්‍රී ලංකා, \n ");
        //tv.setTypeface(tf,0);

        String html = "<html><head><style type=\"text/css\">@font-face { font-family: MyCustomFont; src: url(\"Bamini.eot\") /* EOT file for IE */}@font-face { font-family: MyCustomFont; src: url(\"file:///android_asset/iskpota.ttf\") /* TTF file for CSS3 browsers */}body { font-family: MyCustomFont, Verdana, Arial, sans-serif;font-size: medium;color: black}</style></head><body><LI><a href=\"http://www.thelastrow.lk/\">ශ්‍රී ලංකා;</a></LI></UL></div></body></html>";

        mWebView = (WebView) findViewById(R.id.webGoogle);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setSupportZoom(true);
        //mWebView.loadUrl("file:///android_asset/a.html");
        mWebView.loadDataWithBaseURL("www.thelastrow.lk", html, "text/html", "UTF-8", null);
        mWebView.setWebViewClient(new HelloWebViewClient());


    }

    @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;
    }
}
SajithRu
  • 225
  • 1
  • 8
  • 24
  • I don't have any access to the website I want to load inside `WebView`. It should act like a simple browser window. but I don't know whether I can add `CSS` for a website I load inside a `WebView`. – SajithRu Jul 16 '13 at 05:57
  • i mean who have u loaded data to webview using load method??? if yes then u can add css in inside webview as well as custom fonts – KOTIOS Jul 16 '13 at 05:58
  • I have tried to load custom font using `CSS` as mentioned here by @binary [link](http://stackoverflow.com/questions/3900658/how-to-change-font-face-of-webview-in-android?rq=1) but it didn't gave me the expected results. as mentioned I tried it with `loadBaseUrl()` method like mentioned in below but no resusts :/ `webView.loadDataWithBaseURL("https://www.google.lk/", getHtmlData(MainActivity.this,""), "text/html", "utf-8", "about:blank");` – SajithRu Jul 16 '13 at 06:10
  • Is there anyone who can give solution for my matter :( – SajithRu Jul 17 '13 at 10:32
  • Please refer this link for set custom font in the webview text ..http://stackoverflow.com/a/25692052/3921740[1] [1]: http://stackoverflow.com/a/25692052/3921740 – user3921740 Sep 05 '14 at 18:52

1 Answers1

0

It worked with loadDataWithBaseURL!

For this example I changed the CSS to:

@font-face {
    font-family: 'feast';
    src: url('fonts/feasfbrg.ttf');
}

body {font-family: 'feast';}

Then use the assets path as the base url:

loadDataWithBaseURL("www.google.com",myhtml,"text/html","utf-8",null);
KOTIOS
  • 11,177
  • 3
  • 39
  • 66
  • So where can I add a custom url like _www.google.com_ to load that site with the font I add.? – SajithRu Jul 16 '13 at 06:16
  • It still doesen't load the url I set inside the web view. can you please post the full example... – SajithRu Jul 16 '13 at 06:35
  • Its working when I'm loading locally defined html file but its not working when Im trying to load a webpage from internet. Is there any other method to add custom font for a `WebView`.? – SajithRu Jul 18 '13 at 10:10
  • @Sajirupee hi kindly provide me time for today will post a solution for you 2moro in morning surly – KOTIOS Jul 18 '13 at 10:21
  • Hi, Did you found a solution for my problem.? – SajithRu Jul 23 '13 at 03:39
  • @Sajirupee oops u still didnt find solution..sorry i forgot..i will hep u today – KOTIOS Jul 23 '13 at 04:27
  • is it possible for u to add project.rar to mailid will just debug and post u back with solution – KOTIOS Jul 26 '13 at 06:50
  • Hi, I have uploaded my project to mediafire. [here](http://www.mediafire.com/download/o5fnhlggji5q9cz/SinhalaFontRendering.rar) is the link. sorry I couldn't do it sooner. hope you can find a solution. :) – SajithRu Aug 09 '13 at 04:59
  • @Sajirupee hey kindly dont post the code publicly u can mail sorry i forgot ur issue – KOTIOS Aug 09 '13 at 05:01