Monday, August 25, 2014

How to Set typeface for HTML String in Android

How to Set typeface for HTML String in Android
This post about how set the typeface for Html string and load to webview.
#put your font file into asset folder.

Screen Shot


Activity Code
package com.example.supportmultiplelanguge;

/*http://iamvijayakumar.blogspot.com/

 */

import android.annotation.SuppressLint;

import android.app.Activity;

import android.os.Bundle;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.Button;

//@VIJAYAKUMAR

public class MainActivity extends Activity {

       Button arabic_btn;

       String htmlStr = "<p style='text-align:right'>العذر يا بوي لو ماشلّني ساقي</p><p style='text-align:left'>الحزن ما طاقني والقلب ما طاقه</p><p style='text-align:right'> ياعيني ابكي وساعدها يا خفّاقي</p>";

       WebView webView;

       @SuppressLint("NewApi")
       @Override
       protected void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.activity_main);

              webView = (WebView) findViewById(R.id.text);

              webView.setWebViewClient(new WebViewClient() {

                     @Override
                     public boolean shouldOverrideUrlLoading(WebView view, String url) {

                           view.loadUrl(url);

                           return true;

                     }

              });

              String head = "<head><style>@font-face {font-family: 'arial';src: url('file:///android_asset/fonts/HelveticaNeueLTArabic-Bold.ttf');}body {font-family: 'verdana';}</style></head>";

              String htmlData = "<html>" + head
                           + "<body style=\"font-family: arial\">" + htmlStr
                           + "</body></html>";

              webView.loadDataWithBaseURL("", htmlData,

              "text/html", "utf-8", "");

       }

}

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...