Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set text size in WebView in android

I am using WebView to justify text. I want to know-

  1. Is it possible to set the text size in layout file? As I want different text size for different screen sizes.

  2. And also one problem is first background appears then after 2 second text display. Is it possible to display text immediately?

Size of text is 7-8 lines.

Code-

public class Main extends Activity {         WebView mWebView;         @Override        public void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.main);                   mWebView = (WebView) findViewById(R.id.webview);           String text = "<html><body>"+"<p align=\"justify\">"+getString(R.string.itext)+"</p>"+"</body></html>";        mWebView .loadData(text, "text/html", "utf-8");       mWebView .setBackgroundColor(Color.TRANSPARENT);        } } 

Xml-

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:orientation="vertical"         android:layout_width="fill_parent"        android:layout_height="fill_parent"         android:background="@drawable/light">  <WebView     android:id="@+id/webview"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:background="@android:color/transparent"/> </LinearLayout> 
like image 368
John R Avatar asked Feb 11 '14 06:02

John R


People also ask

How do you increase font size of WebView in react native?

How do you handle font scaling in React Native? on Android value reflects the user preference set in Settings > Display > Font size. on iOS value reflects the user preference set in Settings > Display & Brightness > Text Size, value can also be updated in Settings > Accessibility > Display & Text Size > Larger Text.


1 Answers

For setting text size from layout-

final WebSettings webSettings = web.getSettings(); Resources res = getResources(); fontSize = res.getDimension(R.dimen.txtSize); webSettings.setDefaultFontSize((int)fontSize); 

For Immediate text display-

webSettings.setRenderPriority(RenderPriority.HIGH); webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); webSettings.setAppCacheEnabled(false); webSettings.setBlockNetworkImage(true); webSettings.setLoadsImagesAutomatically(true); webSettings.setGeolocationEnabled(false); webSettings.setNeedInitialFocus(false); webSettings.setSaveFormData(false); 

In values folder-

<resources>      <dimen name="txtSize">26sp</dimen>  </resources> 

Hope it works.

like image 112
Kanwaljit Singh Avatar answered Sep 25 '22 16:09

Kanwaljit Singh