0

An English font is working in HTML in webview, but the non-English font (Hindi) is not displaying in HTML in webview.

Here is my source code.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="in.wptrafficanalyzer.webviewcustomfont"
          android:versionCode="1"
          android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="in.wptrafficanalyzer.webviewcustomfont.MainActivity"
            android:label="@string/app_name" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

demo.html

<html>
    <head>
        <meta http-equiv="content-type" content="text/html;" charset="UTF-8">
        <style>
            /** Specify a font named "MyFont",
                and specify the URL where it can be found: */
            @font-face {
                font-family: "MyFont";
                src: url('file:///android_asset/fonts/BLKCHCRY.TTF');
            }
            h2 { font-family:"MyFont"}
            @font-face {
                font-family: "MyFontSD";
                src: url('file:///android_asset/fonts/SHREE-DEV7-1076H.TTF');
            }
            h1 { font-family:"MyFontSD"}
        </style>
    </head>

    <body>
        <h2>
            Welcome to BLACK CHANCERY FONT
        </h2>
        <h1>
            1234567890
            Am{X
        </h1>
    </body>
</html>

activity_main.xml

<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"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_world" />
</RelativeLayout>

MainActivity.java

package in.wptrafficanalyzer.webviewcustomfont;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {

    WebView mWebView;

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

            // Getting reference to WebView of the activity_main layout
            mWebView = (WebView) findViewById(R.id.webview);

            // Loading an HTML page into webview
            mWebView.loadUrl("file:///android_asset/demo.html");
        }

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

Note:

  1. HTML file demo.html is copied in the assets folder

  2. BLKCHCRY.TTF font file is copied in fonts folder

  3. SHREE-DEV7-1076H.TTF font file is copied in fonts folder

  4. Link to download fonts (SHREE-DEV7-1076H.TTF and BLKCHCRY.TTF)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Samuel
  • 1
  • 3
  • You don't need to use `file:///android_asset/fonts/SHREE-DEV7-1076H.TTF` in your css because the HTML doesn't know that try this instead `fonts/SHREE-DEV7-1076H.TTF` – Steven Oct 16 '17 at 16:09
  • thank you for reply. i tried the suggestion but still font SHREE-DEV7-1076H.TTF is not working. can you please try my uploaded sources code in your android studio and rectify the issue and give me the solution please. – Samuel Oct 23 '17 at 17:27
  • Okey I will give a try and let you know if I have the same problem. Can you upload the thing you tried? – Steven Oct 23 '17 at 17:31
  • you can use the below link to download android studio project files in rar format https://drive.google.com/file/d/0B3fN9YVlzIHZZFJzWUsxRmRoVWM/view?usp=sharing_eil&ts=59ee42fa – Samuel Oct 23 '17 at 19:34
  • any updates please – Samuel Oct 24 '17 at 15:45

0 Answers0