32

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>
John R
  • 2,078
  • 8
  • 35
  • 58

11 Answers11

64

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.

Kanwaljit Singh
  • 4,339
  • 2
  • 18
  • 21
  • 2
    The method setRenderPriority(WebSettings.RenderPriority) from the type WebSettings is deprecated – S.M_Emamian Jul 20 '14 at 21:58
  • 5
    I test your code. It seems that you shouldn't pass font size from dimen to `setDefaultFontSize()`. For example if you want to show your text with 12 sp size you should simply pass 12 to that method. – Misagh Emamverdi Oct 04 '14 at 14:36
20
WebSettings webSettings = webView.getSettings();

either setTextSize or

webSettings.setTextSize(WebSettings.TextSize.SMALLEST);

This one works too:

webSettings.setDefaultFontSize(10);

And Display text immediately:

webview.getSettings().setRenderPriority(RenderPriority.HIGH);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.setBlockNetworkImage(true);
webview.setLoadsImagesAutomatically(true);
webwebviewSettings.setGeolocationEnabled(false);
webview.setNeedInitialFocus(false);
webview.setSaveFormData(false);
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Digvesh Patel
  • 6,503
  • 1
  • 20
  • 34
9

1 - I'm using this code that gives me a good result on all my devices.

If web is tyour WebView,

web = (WebView) v.findViewById(R.id.htmlDisplay);
// Impostazioni della WebView.
final WebSettings webSettings = web.getSettings();
// Set the font size (in sp).
webSettings.setDefaultFontSize(20);

OK, this is hardcoded

Now, a more dynamic solution (as per your question):

If you put your desired size in an integers file (several, one for each screen you are supporting), you could do so to retrieve its value:

Resources res = getResources();
int fontSize = res.getInteger(R.integer.font_size);
webSettings.setDefaultFontSize(fontSize);

Assuming that your res/values/integers.xml file(s) is similar to this one:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="font_size">20</integer>
</resources>

So change this line in the above code

webSettings.setDefaultFontSize(20);

to

webSettings.setDefaultFontSize(fontSize);

2 - To load things faster, I use the following code

// Optimize loading times.
webSettings.setAppCacheEnabled(false);
webSettings.setBlockNetworkImage(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setGeolocationEnabled(false);
webSettings.setNeedInitialFocus(false);
webSettings.setSaveFormData(false);

Note that I load more than 7-8 lines of text: it's a full HTML page with JavaScript to expand / collapse sections. And it takes, say, half a second to load. So, I guess, 7-8 lines of text will be loaded in a flash.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Its taking nearly 1 sec. – John R Feb 20 '14 at 09:16
  • @AartooDetoo and where to add these lines? – John R Feb 20 '14 at 09:18
  • @JohnR: after the WebView definition and the declaration of the WebSettings object. See, I updated my answer with more settings to thinker with. – Phantômaxx Feb 20 '14 at 09:24
  • @AartooDetoo Lines you edited just now when I add these in my code. Black screen appears and text not appear. And some lines are cutted by black lines. Maybe not supported as minimum sdk version is 8 in my app. – John R Feb 20 '14 at 09:43
  • And **.setDefaultFontSize** require int value error. – John R Feb 20 '14 at 09:48
  • Changed from using dimens.xml to using integers.xml. Now the exampl it's using an integer (dynamically set in res/values/integers.xml), instead of a dimens in sp. I also removed the untested websettings and left just the ones I use (which helped me a lot in speeding up the loading of my HTML help files). – Phantômaxx Feb 20 '14 at 11:06
  • @AartooDetto I have solved this integer problem using cast in your previous answer. But text still take 1 sec to load. – John R Feb 20 '14 at 11:09
  • On the emulator or on a physical device? Because the latter is surely faster. This speed will obviously vary from device to device, since there are dual, quad, octa core (...) and with different speeds (today a 1000 MHz speed is quite common). So, I guess that on a phisical device it would run MUUUUUUUCH faster. – Phantômaxx Feb 20 '14 at 11:12
  • So... I really don't know how to squeeze any more juice :( – Phantômaxx Feb 20 '14 at 11:18
  • .setRenderPriority not supported by API level below 8? – John R Feb 20 '14 at 11:26
  • From the official site: "setRenderPriority(WebSettings.RenderPriority priority) This method was deprecated in API level 18. It is not recommended to adjust thread priorities, and this will not be supported in future versions." It was added in API Level 1. – Phantômaxx Feb 20 '14 at 11:32
8

I'm not sure if this is what you're looking for, but as long as you don't want to set the text size based on a calculation with the text width/height, this should be what you're looking for:

WebSettings settings = yourWebView.getSettings();

This way you're getting the settings of the WebView whose text size you're trying to change.

There're some predefined values you can set as size using the TextSize class, for instance:

settings.setTextSize(TextSize.SMALLEST);
settings.setTextSize(TextSize.SMALLER);
settings.setTextSize(TextSize.NORMAL);
settings.setTextSize(TextSize.BIGGER);
settings.setTextSize(TextSize.BIGGEST);

You could simply choose which one you need to set depending on the parameters of the current device that you choose.

If you, on the contrary, want to make some kind of screen width/height calculation and set the result as a particular value, you may even use this:

settings.setDefaultFontSize(an_integer_that_goes_between_1_and_72);

---- EDIT ----

To enhace your WebView rendering, try the following:

mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

You could also try adding this to your Manifest file:

android:hardwareAccelerated="true"
nKn
  • 13,691
  • 9
  • 45
  • 62
  • 1
    Are you experiencing this in Android Kit Kat devices, or others too? – nKn Feb 16 '14 at 12:11
  • Are you loading something **behind** `WebView`? I mean, is there something blocking the loading of `WebView`, like for instance an `ImageView` or some network operation? – nKn Feb 17 '14 at 08:47
  • I am setting background in xml. And in activity loading text using above coding. – John R Feb 17 '14 at 09:02
  • Please see my update, it should enhace the rendering of your WV. – nKn Feb 17 '14 at 09:22
6

There is three way to set FontSize on WebView.

1) WebView.setDefaultFontSize(int)

webview.setDefaultFontSize(50);

2) WebView.getSettings().setTextSize(WebSettings.TextSize.SMALLEST)

WebView.getSettings().setTextSize(TextSize.SMALLEST); 

or

WebView.getSettings().setTextSize(TextSize.SMALLER); 

or

WebView.getSettings().setTextSize(TextSize.NORMAL); 

or

WebView.getSettings().setTextSize(TextSize.BIGGER); 

or

WebView.getSettings().setTextSize(TextSize.BIGGEST);

3) Add font-size in html formate.

String text = "<html><body style=\"color:black;font-family:Helvetica;font-size:12px;\"'background-color:transparent' >"+"<p align=\"justify\">"+getString(R.string.itext)+"</p>"+"</body></html>"; 
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • For immediate display of text not any answer working. Please tell me how to display text immediately? – John R Feb 22 '14 at 04:13
3

You can always use CSS to change the font size:

String text = "<html><style type=\"text/css\">p{text-align:justify;font-size:80%;}</style></head><body>"+"<p>"+getString(R.string.text1)+"</p>"+"</body></html>";

mWebView.loadData(text, "text/html", "utf-8");

To display fater, as Digvesh Patel said before:

webview.getSettings().setRenderPriority(RenderPriority.HIGH);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Luciano Rodríguez
  • 2,239
  • 3
  • 19
  • 32
3

webSettings.setTextSize is depricated now onwards.

You can Use setTextZoom OR getTextZoom for changing the size of webview font.

For Regular textsize you can give the integer value as 100. And If you want to increase size of font then you can use code as below :

webView.settings.textZoom = 125
2

In kotlin just add this line,

webview.settings.defaultFontSize = yourSizeInInteger
Al Walid Ashik
  • 1,545
  • 20
  • 32
1

You can use Html.fromHtml() and write your text in HTML format.

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
user8938
  • 559
  • 1
  • 4
  • 12
1
webView.getSettings().setDefaultFontSize(5); //5 or any dmen you want
Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
0

The method receives an integer value that can store the division in resources and assign it to a variable then you can include it in the user settings procedure to set the font size that suits him as a user

webSettings.setDefaultFontSize(23);

or

Resources res = getResources();
            int fontSize = res.getInteger(R.integer.font_size);
            webSettings.setDefaultFontSize(fontSize);
Procrastinator
  • 2,526
  • 30
  • 27
  • 36