How to Justify paragraph in textview setText option? I am loading data from API and need to display paragraph as justified.
textv.setText(Html.fromHtml(text));
I have tried below in adapter but nothing works
text = text + ""+paragrph +"";
Is it possible other than webview ?
You cannot set the property like gravity in TextView. You can either use WebView or other open source libraries.
Android does not support text justification yet.
You need to use WebView to justify text. Here is it an example:
WebView webView = (WebView) findViewById(R.id.webView);
webView .setBackgroundColor(Color.TRANSPARENT);
String header = "<html><head><style type=\"text/css\">#font-face {font-family: MyFont;src: url(\"file:///android_asset/fonts/Walkway-Ultrabold.ttf\")}body {font-family: MyFont;font-size: medium;text-align: justify;}</style></head><body>";
String footer = "</body></html>";
String text_web_view = header + message + footer;
webView.loadDataWithBaseURL(null, text_web_view, "text/html", "UTF-8", null);
This code set also a font placed into assets/fonts folder.
Related
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);
Does anyone know of a way to modify the visual style of how the WebView displays auto-linked text (phone numbers, addresses, etc.)? Or more specifically, can I make the links that WebView detects look like standard clickable hyperlinks? For example,
webView.loadData("My phone number is 3035555555", "text/html", "utf-8");
This loads the text into the WebView and it is clickable, but it just looks like the rest of the body text. I also tried putting the text into an HTML file in assets and doing
webView.loadUrl("file:///android_asset/Test.html");
But that yielded the same result. Is there something in WebSettings or WebViewClient that controls this behavior I'm missing?
Cheers.
You could do this to get what your looking for.
String header = "< ?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
String data = "< html>< body>< a href='tel:555-5599'>508-776-5510"
"< /body>< /html>";
mWebView.loadData(header+data, "text/html", "UTF-8");
Try this.....
String header = "< ?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
String data = "< html>< body>< a href='tel:555-5599'>508-776-5510
" "< /body>< /html>";
mWebView.loadData(header+data, "text/html", "UTF-8");
If you are loading a string of html texts into webView. Then you can use
mWebView.loadData(header+data, "text/html", "UTF-8");
If you have a html file. Then you can use
webView.loadUrl("file:///android_asset/mypage.html"):
Note: Dont forget to put your html file in your assets folder.
Cheers!!! :D