Preserving colours while importing HTML to Android - android

I'm importing HTML to my app with this code:
MyTextView.setText(Html.fromHtml(""));
HTML is importing, but colors are not. How can I import HTML with colors?
Here is some sample HTML:
<!doctype html>
<html>
<head>
<title>MY HTML CODES</title>
</head>
<body>
<h3>MY HTML!</h3>
<p>This HTML <span style="color:#FF0000;"><strong> has a larger .</strong></span></p>
</body>
</html>

Use a spannable:
String myText = "How <i>are</i> <b>you</b>? "+
"<font color='green'>Nice</font> <font color='red'>to</font> meet you!";
MyTextView.setText(Html.fromHtml(myText), TextView.BufferType.SPANNABLE);
Edit: you can get different color using the font tag.

Html.fromHtml() does not support <span> elements or style attributes, let alone <head> or <title> and such.
You will either need to write your own parser for this, or convert this HTML into something that Html.fromHtml() will support (e.g., <font color="...">), or convert this HTML into something that my CWAC-RichTextUtils library will support.

You should display your HTML in a WebView http://developer.android.com/reference/android/webkit/WebView.html
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);

Related

Unknown character in html inside webview android studio

I view a lot of htmls with text inside, but there seems to be one html that is not displayed correctly, for some reason all the tilde characters display an unknown character, this is strictly related to that particular html document?
Since all the others htmls are viewed perfectly in webview, i guess it has nothing to do inside android studio?
Intial portion of html document
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<link href="Content/watermark.css" rel="stylesheet" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Código de Proceso Penal</title></head>
<script src="Scripts/jquery-2.1.4.min.js"></script>
CADE
This happens due to fact that Webview is not able to detect encoding.
If you are using Webview loadData() or void loadDataWithBaseURL() method, try passing "UTF-8" as encoding, you can check same here, on developer.android.com..
it this doesn't work try with "iso-8859-1" or "iso-8859-15" encoding

how to load android assets css files to android WebView URL

Actually I want to load complete site from internet link but i want to load css files from mobile (means not from internet). Because I want to load all page faster using internal local css file.
Example :
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="dist/css/font-awesome.min.css" />
</head>
</html>
So Here I want to Load "bootstrap.min.css" and "font-awesome.min.css" from mobile memory only.
My Android Code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://www.MySiteName.com/";
view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true); // Enable JavaScript Support
view.setWebViewClient(new WebViewClient()); // Links open or Navigate in same webView not in Browser
view.loadUrl(url);
}
Please put CSS in assets folder, and refer to CSS by relative path, and load HTML to WebView by loadDataWithBaseURL() method:
if your css file name is mycss.css
StringBuilder data = new StringBuilder();
data .append("<HTML><HEAD><LINK href=\"mycss.css\" type=\"text/css\" rel=\"stylesheet\"/></HEAD><body>");
data .append(tables.toString());
data .append("</body></HTML>");
webView.loadDataWithBaseURL("file:///android_asset/", data .toString(), "text/html", "utf-8", null);
thank you
Because the two other answers are just copy pastas and I happened to have the same issue I will help you with my solution:
You need to specify the file path in the head section of your html file.
Pay attention to the part after href
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="file:///android_asset/bootstrap.min.css" />
<link rel="stylesheet" href="file:///android_asset/font-awesome.min.css" />
</head>
</html>
I just can't get my head around why the other posters just copied and pasted and didn't even read what you were trying to tell them.
I hope this helps.
Be aware that your resources folder can be named differently.

Android - unicode characters aren't displayed correctly

I'm trying to load local html file:
WebView webView = (WebView)findViewById(R.id.webView1);
webView.getSettings().setDefaultTextEncodingName("utf-8");
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/www/index.html");
index.html contains this:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head >
<body>
რაღაც ტექსტი
</body>
</html>
but it doesn't work. Can anyone help?
P.S. that text is in Georgian
Edit: I've tried loading that text using almost every possible method I've found - loadDataWithBaseURL and loadData methods didn't work either.
Closed: Here is the problem: That version of Android didn't recognize Georgian characters.
You can use HTML codes of UNICODE characters to display them in WebView.
Just replace the characters with their respective html code.
Refer here for Html codes of Georgian characters.
You can use this approach;
String start = "<html><head><meta http-equiv='Content-Type' content='text/html' charset='UTF-8' /></head><body>";
String end = "</body></html>";
webView.loadData(start + YOURCONTENT + end,"text/html; charset=UTF-8", null);
Ensure that your file is actually encoded as UTF-8.
Check if your editor save it as UTF-8. It could be the problem.

Android WebView Content is Not Justified

I read all possible questions/response on stackoverflow, but i still can't do what i would like.
I created an application to show News
1. Used**Thoolika.ttf** Unicode font.
2.Now I am using **AnjaliOldLipi.ttf** font
3.Older data was in the form **c¬¥Vvp¢: d¡...**.
4.New data is in the form **&#3384 ; &#3394 ; &#3370 ; &#3405 ; &#3370 ;** .
My problem is I can't justify this news inside a webview.For this,I am using html given below.
<html>
<head>
<meta http-equiv=Content-Type content=text/html; charset=UTF-8 />
<meta name=viewport content=width=device-width; initial-scale=1.0; maximum-scale=5.0; user-scalable = yes /><style>#font-face { font-family: 'Anjali';src: url('file:///android_asset/fonts/**AnjaliOldLipi.ttf'**);}body { font-family: 'Anjali';}</style></head><body style=font-family:Anjali;margin:5px;><div style=padding:5px;**text-align:justify;** >
<span style=color:#3070AC;font-family:Anjali;font-size:20px;font-weight:bold;text-decoration:none;>NEWS TITLE</span><br>
<div style=width:100%;text-align:right;padding-top:2px;padding-right:2px;color:#BCBCBC;font-family:monospace;font-size:11px;><i>NEWS DATE</i></div></div>
<div style=padding-top:5px;padding-left:5px;padding-right:5px;color:#000000;font-family:Anjali;font-size:14px;text-decoration:none;text-align:;>
NEWS CONTENT
</div></body></html>
How can i align both left and right side of this webview(justify).

How can I set to HTML Tags to Normal View in Android?

I have like this html blog
<head>
<style>
<!--.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>
text<div>write</div>
</div></body>
</html>
text and write are the results. This Html tags must shown as
-----------
text write |
-----------
How can I do that in Android?
seems to me this should be easy to achieve from the css:
div.ltr div
{
display:inline;
}
Use TextView to present texts in your app.
use html in textviewor button as following
String htmltext = "<html><body>/*... html body here...*/</body></html>";
textview.setText(Html.fromHtml(htmltext));
if you want to use css style or javascript use webview like
webview.loadDataWithBaseURL(null, htmltext, "text/html", "UTF-8", null);

Categories

Resources