Android String HTML help needed - android

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.

Related

Html.fromHtml() is not able to decode font color for anchor attribute

I am trying to set simple html content to a textview, content is something like:
Click <font color="#0000ff"><u>here</u></font> for details.
Setting this simply using Html.fromHtml() results this:
Ideally, the link color should be blue(#0000ff) but it is showing in the default platform link color.
When I removed href tag from the result was expected:
I could not understand why android is not able to handle color when href is present inside tag.
I have previewed the HTML content in a web browser, it is working as expected, also I have tested the same HTML content on the iOS platform it is working as expected.
I have tried using all the available Html flag options but no luck.(tested in simulator and device as well).
I could not use textColorLink as there are multiple links with different colors in actual HTML content.
val htmlContent = "Click <a target=\"_blank\"><font color=\"#0000ff\"><u>here</u></font></a> for details."
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.text = Html.fromHtml(htmlContent, Html.FROM_HTML_MODE_COMPACT)
}
Please help me to understand if there is any flaw in my code or there is limitations/bug in android.

Webview doesn't display text with color

In my app I display some Html content in webview:
String webViewConent = 'this is some <span style="color:#2ecc71">sample</span> string'
webView.loadData(omowienie, "text/html; charset=utf-8", "UTF-8");
However after last app update, which was related with some other thing, for some users webview doesn't work correctly. They see only string which is before span tag. The problem is not related to any specific android version.
Same problem here, I found base64 encoding as a quick fix:
String base64 = android.util.Base64.encodeToString(html.getBytes("UTF-8"), android.util.Base64.DEFAULT);
mWebView.loadData(base64, "text/html; charset=utf-8", "base64");
I had the same issue today. As a simple workaround I replaced hex colors in CSS by RGB equivalents.
Before
<span style="color: #3050c0">Some text</span>
After
<span style="color: rgb(48, 80, 192)">Some text</span>
I found the approach in this answer on WebView encoding seems to fix this issue. Instead of using loadData, try loadDataWithBaseURL:
webview.loadDataWithBaseURL(null, html, "text/html; charset=utf-8", "utf8", null)
I have same problem. My app does not display text after span tag. Some devices works fine but some devices does not work. Upvoted!
<p><span style="color:#0000CD"><strong>Materials:</strong></span></p>
As a result of my research and experimentation, I have disabled the Android System Webview application on my device. As a result of this process, my application worked correctly. However, to me, this cannot be a solution. What are your ideas?
I think the problem is the new update in Android Webview System. The old version works for me.
My alternative solution here is to uninstall Android Webview System in your phone so that you can have the default version(this solution is not ideal if your app is uploaded in Google Playstore and have a lot of users)
The second option is you can delete your color and background color codes in html for the meantime and it will work(I think it is Android Webview System's bug and we just need to wait until they fix their issues)
I have same problem. It may be the Android WebView System's bug but the solution is using font tag to color the texts.
String webViewConent = "this is some <font style="color:green">sample</font> string";
Note : This code is not working for Hex format colors
I din't find a fix but I was able to do a work around changing the string with the original HTML. This might help someone else, AS LONG AS YOU ALREADY KNOW THE COLOURS YOUR ARE GOING TO DISPLAY:
String newString = originalString.replaceAll("color:#888888", "color:gray");

Android Edit content in WebView

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.

Style HTML of a WebView

I am programmatically setting the content of a WebView. I'd like to add some styling around the content, such as padding and font-size, but, with what I have, the styles aren't recognized:
WebView mWebView = (WebView)browserView.findViewById(R.id.webview);
String content = GetContent();
StringBuilder sbContent = new StringBuilder();
sbContent.append("<!doctype html>");
sbContent.append("<body style=\"padding:10em;font-size:12em;\">");
sbContent.append(content);
sbContent.append("</body>");
sbContent.append("</html>");
mWebView.loadData(content.toString(), "text/html", "utf-8");
First, try using valid HTML. For example, you have </html> with no <html>.
Second, the content in question may override your styling. You would need to look at the combined result to see whether or not this will work.

Is there any way to have WebView auto-link URLs and phone numbers in Android?

If a page has a URL or a phone number on it that isn't a link is there any way to have WebView recognize it and automatically turn it into a link like you can with TextViews?
With a TextView you would simply set the android:autoLink to the desired settings:
<TextView
android:autoLink="web|phone"
... />
but I can't find any equivalent for WebView.
If you are loading your own (web) content from a String, then you can do something like this:
final String content = "My email is: firstname#email.com ...";
Spannable sp = new SpannableString(content);
Linkify.addLinks(sp, Linkify.ALL);
final String html = "<body>" + Html.toHtml(sp) + "</body>";
myWebView.loadData(html, "text/html", "utf-8");
I don't know about any way which would make this work just by changing a setting, but a workaround would be to wait until the web page finishes loading and then do:
yourWebView.loadUrl("javascript:(function(){ /* code that creates links */ })()");
This will inject javaScript into the already loaded web page.
There's a slightly longer example available here: http://lexandera.com/2009/01/injecting-javascript-into-a-webview/.
You can find the JavaScript source for creating links if you take a look at the source of Linkify script for Greasemonkey (it's a plugin for Firefox in case you're not familiar with it). I believe it comes with the default install.

Categories

Resources