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;
}
}