I have set custom font in Webview using
String html = "<html><head><title></title> "
+ "<style type=\"text/css\"> "
+ "#font-face { font-family:'GEORGIA'; src: url('file:///android_asset/GEORGIA.TTF'); }"
+ "body { font-family: 'GEORGIA'; text-align: justify;}"
+ "</style> "
+ "</head><body >"
+ "<div>"
+ "<p>"
+ data + "</p></div></body></html>";
webview.loadDataWithBaseURL("",html,"text/html", "utf-8", "");
It is set font but problem is that in data i have text like heading contents when i run it then heading is not display bold.
Maybe your font does not contain bold characters.
True Type Fonts (TTF) can contain a subset of all available characters.
Did you copy the ttf-file from your systems fontpath?
If not, you can open the font with a tool like font-explorer,
Related
I have a webView in my app and I need to set the font-family from a .ttf font which is inside the assets folder, here is my String which fills the webView :
String font_size = 24;
String font_family;
String main_txt = "<html>" + "<head></head>" + "<body dir='rtl' style='font-size: " +font_size+"px; 'font-family: "+font_family+";"+">" + "<text-align: justify;' >" + book.get("content").toString() + "</body>" + "</html>";
My question is how I can refer to that .ttf font in my font_family variable in order to change the WebView font?
If you reference your fonts as fonts/mytypeface.ttf, it would work with any assets/fonts/* file.
// Font must be placed in assets/fonts folder
#font-face {
font-family: spqr; src: url('fonts/spqr.ttf');
}
the address of a file in app:\src\main\assest is "file:///android_asset/a.ttf" in android studio
I have a set of listpreferences which I am displaying in a PreferenceActivity screen. I would like to display a part of the text in the summary field with some formatting, say italics, or different colour.
What would be a good way to go about this?
I am using the following code to set the summary of a listpreference:
lp.setSummary(myText);
//E.g myText = "My name is SoAndSo";
//Required output : "My name is *SoAndSo* (in italics)
// or
// "My name is *SoAndSo* **(in green)**
You can format the text output in html, using html tags.
Here's one way:
mBox = new TextView(context);
mBox.setText(Html.fromHtml("My name is <i>SoAndSo</i>" + "<br />" +
"<small>" + description + "</small>" + "<br />" +
"<small>" + DateAdded + "</small>"));
For an unofficial list of tags supported by this method, refer to this link
I have this code for setting the text of a TextView:
TextView txt = new TextView(this);
txt.setText(Html.fromHtml("<b>" + m.getTitle() + "</b>" + "<br />" + "<small>" + m.getText() + "</small>" + "<br />");
The <small> mark is working, but I'd like to set the text size according to my dimensions defined in the dimens.xml file, which I use for all other text in my application. Adding the TextView through an xml layout is not an option since I don't know how many TextViews I'll be adding.
Dimensions in the dimens.xml file are set up like <dimen name="text_size_320dp_small">16sp</dimen>.
How can I apply these dimensions to my text formatted with Html.fromHtml?
Thanks a lot.
I have tested following code myself. You can do it like this.
txt.setText(Html.fromHtml("<b>" + m.getTitle() + "</b>" + "<br />"
+ "<font textsize="
+ getResources().getDimension(R.dimen.text_size_320dp_small) + ">" + m.getText()
+ "</font>" + "<br />"));
[Updated]:
Just came up with some references and updates :
You can store this in strings.xml
<string name="mystring"><font size = "%s"></string>
In code you can write as:
int sptopx = getResources().getDimensionPixelSize(R.dimen.text_size_320dp_small);
Spanned modified = Html.fromHtml( context.getString(R.string.mystring, sptopx) );
myTextView.setText(spanned);
TextView txt = new TextView(this);
txt.setText(
Html.fromHtml(
"<b>" + m.getTitle() + "</b>" +
"<br />" +
modified +
">" + m.getText() + "</font>" +
"<br />"
)
);
for details about html tags support in TextViews you can check this link.
You can't directly, the small tag creates a RelativeSizeSpan with a proportion of .8f, which is hardcoded into the implementation of Html.fromHtml.
Leaves two options that I can see, set the text size to 20sp (which would make small work out to 16sp). Probably not ideal.
The other option is to use a custom tag <mySmall> by replacing all occurrences of <small> and </small> with <mySmall>& </mySmall>. And then call fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler) with a TagHandler that integrates a AbsoluteSizeSpan into the output Editable.
Why don't you use txt.setSizeText(yoursize)? However you can retrieve your dimensions using this:
float yourDimen = getResources().getDimension(R.dimen.your_dimen_name);
I have a webview which has a p tag inside and the webview itself is has 90% of the device width by default, but I want that p tag to have a 100% width to fill the whole webview. I tried giving it via style:
<p style="width:100%"></p>
but this throws UnknowFormatConversionException. Here is my code to fill the webview with content:
String head = "<head><style>#font-face {font-family: 'byekan';src: url('fonts/BYekan.ttf');}body {font-family: 'Tahoma';}</style></head>";
String text = "<html>" + head + "<body style=\"background:#e4322f;min-height:" + minHeight + "\"><p style=\"width:100%;background:blue;direction:" + dir + ";font-family:byekan;color:#fff;text-align:justify;float:" + flt + "\"> %s </p></body></Html>";
wv.loadDataWithBaseURL("file:///android_asset/",String.format(text, _content), "text/html", "utf-8", null);
it works fine if I remove that "width:100%", but the p tag will takes the text width but I want it to fill the webview width, how can I achieve that? thanks in advance.
We are using Custom Arabic font in our Android App. We have used #font-face to specify the font using SVG and TrueType font files. By default the WebView loads the SVG font and we have noticed that the SVG version of the font is missing some characters. Is there any way that we can force Webview to use TrueType (ttf) font only instead of SVG.
Try this code:
webSettings.setFixedFontFamily("file:///android_asset/myfont.ttf");
Just like this and it works perfectly.
1 //load the fontType
private String getHtmlData(String bodyHTML) {
String head = "<head>" +
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " +
"<style type=\"text/css\">#font-face {font-family: MyFont;src: url(\"file:///android_asset/fonts/myfont.TTF\")}body {font-family: MyFont;font-size: medium;text-align: justify;}</style>"+
"</head>";
return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
}
2 // load the webview content
webView.loadDataWithBaseURL(null, getHtmlData(content), "text/html", "utf-8", null);
3 // end.