Display html formatting in listview android - android

I've got a list view in which i have to dispaly some text from html with proper formatting. Though i pass the html as string to Html.fromHtml method but my formatting as align="justify" don't work.
here is the code snippet:
String text = "<ul><li><p><div align="justify">as part of its growth plan, the ranchi-based central coalfields ltd (ccl) is gearing up to double the company's production in the next couple of years and also to increase the capacity of coal washeries.</div></p></li></ul>";
i pass this this string to
Spanned nText = Html.fromHtml(text);
and then i display it on the screen
When String nText displays on the emulator screen the formatting that should be there, i.e. the text should be displayed as justified, is gone.
Please help

I have found that using Html.fromHtml() can be somewhat hit and miss because some html tags are supported, and others are not. Justified text is not generally supported within Android mainly because, I suspect, that it can look pretty dreadful with relatively short lines of text. It would not surprise me if justified text is not supported in fromHtml().
Personally I would avoid justified text on a small screen anyway because it can be difficult to read.
If you really need justified text, I think that you may need to write something which implements android.text.style.AlignmentSpan and write your own text justification routine, which will not be a trivial task.

Related

Letter Spacing in EditText for Android

I am trying to have a custom EditText based on the background that i am using for. The Background image has some spaces between the entry areas so i need to have some space between the characters(kerning) to fit them right in. So for example after every character the user enters, i need to put 4 whitespace after that.
I couldn't find any solution for this on the net so far, some people suggested TextWatcher, but i couldn't manage to make it work as i want it too.
Can someone help me about it?
Thanks
I have you considered using a custom font? Some font types are made to stretch out or shrink or have empty spaces. With so many different fonts available online, you can definitely find something. You can also make your own with a software. It might be time consuming if you start the lettering from scratch. I'm not 100% sure if it'll fit exactly to your background, but it's idea that you can consider.
If it doesn't fit, I supposed you can always customized the background to fix your font too. Here's the code for those who might want to use custom fonts in their app too.
Typeface myfont = Typeface.createFromAsset(getAssets(),
"fonts/Blocks2.ttf");
myeditText.setTypeface(myfont);
The font is in the asset folder under another folder called fonts.
This question is related to How to change letter spacing in a Textview?
As shown at this issue: android format edittext to display spaces after every 4 characters a solution might be to insert spaces with Java code...
You need to use TextWatcher to achieve visual purpose spaces.
And use any simply split string by space logic to join it back or loop
through the entire string per character wise and eliminate (char) 32
from the string
As far as i know actual character spacing is not possible, though i'd love to use that myself as well.
Another option might be to use a custom font with the character spacing included.

How to get N html text that would fit a TextView

I have a big HTML string, all tags in it are supported by Html.fromHtml() ex. p, b, strong, br, i, em, a, and some other escape characters nothing fancy. I also have some images in there, but lets just skip that cause it may get very complicated. I want to be able to to splint my string in pages with fixed base font size. I have seen this question Breaking large text into pages in android text switcher or view flipper that uses Paint.breakText() and TextView.getLineHeight(), the solution is very nice and works pretty much well with plain text. How do I achieve the same with simple html?

Reader app - use WebView or Canvas?

I'm writing one of those reader applications. I would like to know if you have opinions and arguments for and against using WebView and Canvas (with drawText()) to achieve it.
What are the requirements:
format text according to a few html tags: <p>, <strong>, <h3>, <br/>, <a>,
display images within the text (they are in <img> tags),
display the text in two columns on tablet devices,
paging the text (Google currents style)
The ones in bold are absolutely required. The latter are strongly desired, but I can drop them.
So as far as my knowledge goes:
WebView will be great when it comes to displaying the html formatted text. I also don't have to take care of loading images, tey will be loaded automatically with <img> tags (will they, even if I use loadData() instead of loadUrl()?). The problems begin if I try to page the text. Is there a possibility to count the size of the text in a WebView and reflow it into multiple pages (using ViewPager)?
Canvas is great when it comes to counting the size of the text, putting it into columns and pages. But I will have to handle all the HTML tags myself, format the text myself. What is even worse, I will have to extract images' urls, handle the downloading and putting them back to the text (reflowing the whole text every time they load). Am I right?
Can you point some other advantages and disadvantages of using them? Which would you choose? Or maybe something else? Or is there some lib which does at least some of the work for me?
Why not use the Textview combined with spanned text and viewpagers for the paging.

Better HTML Formatting

EDIT: I've tried a WebView, which does render the HTML correctly, but it is also very slow, and has visual flicker when new data is loaded. I'm looking into what inside the WebView handles the HTML formatting, as it may be a little faster on its own...
I'm making a calculator. To make the input string look real pretty-like, I've been using the HTML.FromHTML() method to handle superscripts powers and subscripts for logs, to be displayed in textViews. For example, the following HTML is handled by your browser and probably looks correct (tested on Chrome and IE):
12+10log10(10log10(33))
The subscript 10s for the logs are properly positioned just slightly below their accompanying log text, and nesting does not cause a problem. Now, the HTML.FromHTML() as used in Android 4.0:
Definitely wrong. It appears that the <sub> tag, instead of implying a subscript of the wrapping text style, instead implies a vertical offset from the baseline. This meaning that having one <sup> tag will make the enclosed text offset by 1 constant amount from the baseline, two <sup> tags will cause two of these constant amounts from the baseline, etc. To make things worse, the <sub> tag merely subtracts this constant value. To add insult to injury, instead of the <small> tag scaling things in place, it also scales the offset of any text inside a <sup> or <sub> tag.
Is there any way I can make the HTML.FromHTML() method act in the same way as the text is rendered in web browsers? Assuming the answer to this is "not right now" or "you can write your own HTML parser!", does anyone have a suggestion to create similar functionality?
Thanks!

Max 9000 characters in Android TextView?

I have items with a lot of text, mostly around 8500 to 9500 characters.
I want to display that in a scrollable textview, but when put the text in a TextView, it seems to be truncated to a max of 9000 characters, while a (the original) String object can hold more. Is there a way to extend this number? Absolutely no parameters on restricting the size of the TextView has been set.
Thanks in advance.
Here is a link which basically says:
The answer is "how much memory can you allocate?" Of course the
system needs to allocate resources to measure and typeset and render
all of that text. If it's too long, consider showing the text in
pages or chunks.
So if your string has 50000 characters, show it in 9000 character chunks.
Use this on your EditText or TextView:
android:maxLength="1000000"
Some phones have an hard limit of 9000 chars by default.
For anyone looking at this in 2018, I don't think there's a hard limit. I have a pretty cheap Android phone with 1 GB of RAM and was able to both edit and display over 40,000 characters of text. There is a slight initial pause when pasting into the edit box and when rendering into a text view but otherwise it works just fine.

Categories

Resources