first time question here.
So, I am uploading text to a WebView, in order to use the <p align = "justify">.
However, I am also trying to indent every first line of every chapter using for the spaces. But the indents don't always match, some have three spaces, some four, etc... I've tried using <pre>, &xa0; but to no avail.
My code is as follows:
webView2 = (WebView) findViewById(R.id.webView);
text ="<font color=\"#2C2C2C\"><html><body><p align=\"justify\"> TextTextTextText<br>" +
" MoreTextTextText</p></body></html></font>";
text2.loadData(text, "text/html", "utf-8");
Related
I have some problem in my web view.
I want to load a string from a WordPress blog, witch have html tags such as <a> and image tag and ... .
So my problems are:
As I mention above, I want to load a local string and I want to handle user click on the links, so I load data like this into the web view:
WebView webview = (WebView) this.findViewById(R.id.mainWV);
webview.setWebViewClient(new MyWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
String s="<p>It will enable Seattle-based Alaska to expand into lucrative hubs such</p>\n<p><img class=\"aligncenter size-full wp-image-1035\" src=\"http://ichef.bbci.co.uk/news/660/cpsprodpb/D09F/production/_89070435_89069565.jpg\" alt=\"\" width=\"300\" height=\"120\" /></p>\n<p>as San Francisco and Los Angeles.</p>\n";
webview.loadDataWithBaseURL("", s, "text/html", "utf-8", "");
and another way I tried was:
String head1 = "<head></head>";
String text = "<html>" + head1
+ "<body dir=\"rtl\" >" + s
+ "</body></html>";
webview.loadData(text, "text/html", "utf-8");
and my client is :
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("USER_CLICKED", url + "USER_CLICKED");
return true;
}
}
Ok, now when I run the app, and when I click on <a> I never see 'USER_CLICKED', but the webview content change and it seems web view is empty, I mean is white as snow.
notice 1: when I try this:
webview.loadUrl("https://android-arsenal.com/");
and run the app, when I click on the links in the loaded web view, every thing is OK, and i see this Log: 'USER_CLICKED' and the related URL.
notice 2: yes i try a lots of different URL, but loading from string, nothings change in click handling.
notice 3: I test in android 5.1 and 4.1 in 4.1 clicked recognized and is see 'User.. but in the 5.1 the white page story happens.(Edit: android 6 also do not show 'USER... ')
my number 2 problem is, when I call this:
webview.loadDataWithBaseURL("", s, "text/html", "utf-8", "");
the image tag does not load! I mean it is just ignore to load the images, and I do not know why.
notice 3: when I copy text from inside the web view there is some rectangle in the text.
OK every one, after a long time, i find the problem, so as i mentioned above, i take string from WordPress rest API(json), so in my word, the string should be OK, but as i find out, there is some extra '\' in the string, and the string is like:
<p>It will enable Seattle....
, as simple it is i just use:
s=s.replaceAll("\\","");
so, thank any one who see this post.
A URL is a URL. The first item is HTML, not a URL. If you want to load the URL then you have to pass a valid URL (not HTML) and do not expect the URL to be magically parsed out of the HTML string that you are loading. In fact the first item is not even valid HTML, it is a part of HTML, potentially a snippet, but it's not even enclosed in the HTML tags that I expect a web view would need.
I need to plug an HTML formatted string into a TextView. I'm retrieving the text from a database and when I insert it using this code
WebView webview = (WebView) view.findViewById(R.id.webView);
text = "<html><body>" + {text retrieved form database} + "</body></html>"
webview.loadData(text, "text/html", "utf-8");
It works but this code
TextView output = (TextView) view.findViewById(R.id.simple_text);
output.setText(Html.fromHtml(text));
strips out all HTML formatting.
What is the right way to do this?
Start by getting rid of the <html><body> and </body></html> parts, as Html.fromHtml() does not use them.
Then, limit your HTML to use tags that are supported by Html.fromHtml().
I have pictures/movies in my webview, and i have a problem with the resize.
I have a CSS for my webview in which i set
img{width:100%;}
But how to set the just height ? Because currently I have picture which take the half of the screen.
My other problem is i have
in my content, how can I delete it ?
To modify the styling of the HTML you could load a custom css like this:
final String customCssLink = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">";
webView.loadDataWithBaseURL("file:///android_asset/", customCssLink + html, "text/html", "UTF-8", null);
And you can remove all of the from the HTML like this:
String.replace(" ", "");
But with both those things you have to be very careful. Modifying HTML is just like HTML parsing a big source of bugs and rather error prone. If at all possible I would advice against doing stuff like this, but if you have no other choice try to at least be as careful as possible.
So in Android I wanted to justify the text so I decided to use the Webview.It works fine but the only issue I have is that the background of my app is not white. So whenever I see the text, it always has a white background. So can I add a body tag to enable it to change the background color?
String text = "<html><body style=\"text-align:justify\"> %s </body> </Html> ";
// I want to include a body tag above which will make the background this color = #e6e6e6
String data = "My Text";
WebView webView = (WebView) findViewById(R.id.WebView);
webView.loadData(String.format(text, data), "text/html", "utf-8");
Take a look at the following link.
Android TextView Justify Text
It explains the justify rules in android. How to do it in webview and it also has a support library you could use to do native.
Try
webView.setBackgroundColor("#00000000")
for transparent background.
Try:
String text = "<html><body style=\"text-align:justify; background-color:#e6e6e6;\"> %s </body></html>"
The background-color CSS property should let you set the background to whatever you want it to be.
Edit: Kalel Wade's answer is better since it's done through the Android properties as opposed to the HTML.
i am receiving html from server and need to show it to user with simple styles (like bold, italic etc) and user should be able to edit it and impart similar style in it. i used webview, but it is not editable. I have seen some prev ques about rich text editor, but none of them are clear about what to do, or i could not understand as i am a newbie in this field. Thanks in advance. Here data is a string which is recieved from server, it contains html tags within. I need an editor which can add new text, edit prev one, bold/unbold selected text and some similar functions
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setEnabled(true);
webView.loadDataWithBaseURL("file:///android_asset/", data, "text/html",
"utf-8", null);