Formatting long paragraphs in android TextView - java

I have a lot of text (more than 10000 words) which I would like to display on my android app. The text contains bullets, paragraphs and line breaks. I want to know how can I display on my app without manual editing. It is not possible to convert every line break to \n and every tab to \t. I have also tried parsing data in HTML and then using Html.fromHtml(getString(R.string.text)). However, the display that I get looks completely unformatted.
This is how I want the text to look like in the app:

HTML tags are easy solutions for simple problems, like making a text bold, italic, or even displaying bullet points. To style text containing HTML tags, call Html.fromHtml method. Under the hood, the HTML format is converted into spans. Please note that the Html class does not support all HTML tags and CSS styles like making the bullet points another color.
val text = "My text <ul><li>bullet one</li><li>bullet two</li></ul>"
txtview.text = Html.fromHtml(text)
Spans allow you to implement multi-style text with finer-grained customization. For example, you can define paragraphs of your text to have a bullet point by applying a BulletSpan. You can customize the gap between the text margin and the bullet and the color of the bullet. Starting with Android P, you can even set the radius of the bullet point. You can also create a custom implementation for the span. Check out the "Create custom spans" section below to find out how.
val spannable = SpannableString("My text \nbullet one\nbullet two")
spannable.setSpan(
BulletPointSpan(gapWidthPx, accentColor),/* start index */ 9, /* end index */ 18,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannable.setSpan(BulletPointSpan(gapWidthPx, accentColor),/* start index */ 20, /* end index */ spannable.length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
txtview.text = spannable

Related

Android: how can I display a string until the string reaches the end of a one-line textbox and display the rest in a different textbox?

I like to think my title question is adequate, but the details are:
I have long strings (sentences in a story) to display and want it displayed in a number of textviews, with only one line in a box. If the string is too long, I want to truncate at a space or punctuation mark, then display the rest in another textview (or views if the string is really long). My display would have many textview, each with one piece of the string.
So, the question: how can I truncate a string at the end of a textview but not before? I do not know how many characters per line.
Second question: how do I capture the remaining piece of sentence and display it in the next textview?
The idea is to have a line of a complete sentence and a translation of the sentence appearing in the line below. If the sentence is longer than a single line, the truncated first part will have a truncated, translated line below it, then the remaining part of the original sentence below that, then the remaining part of the translated sentence below that. So, alternating lines of English text and Spanish text displayed.
For the line breaks, I suggest setting up a TextView that has the width of your on-display TextViews with wrap_content for its height. This will be an internal TextView that is for processing only and not for display. You will need to get the TextView to measure itself. This can get you started. Of course, if the text is already displayed on-screen, you can just use that TextView.
Once the view is measured, you can get its internal layout which most likely is a StaticLayout. (You can also use StaticLayout directly without the TextView.) Since the StaticLayout is a representation of the text display by the view and are the same lines of text that would display in the TextView, you can move them over directly to single-line, on-screen TextViews as long as those TextViews have the same characteristics as your internal one. StaticLayout has methods to retrieve individual lines of text.
There are details that you will have to work through, but that is the gist.
Update: I took the liberty of making a small demo app. This app takes a paragraph of text and splits it into the constituent lines. Those lines are then displayed on-screen with the original paragraph as seen below:

Automatic line break in Java html text editor

I am creating a text editor in java that saves the text as html, but displays as plain text to the user. The user has the ability to change the color, alignment and style (bold and underline) of the text. The entire body of text is stored in the database as html in order to save the style adjustments. I am having an issue with newlines not being saved. So when the user is entering text, presses enter and puts text on a new line, it all gets put on one line after be saved and re-displayed. All the text is just being put inside on paragraph tag without any line breaks. I'm wondering if there's a way to tell the text editor to automatically insert line breaks for new lines?
The way I have my editor set up is a JTextPane using a HTMLEditorKit with content type set to text/html. I am using StyledEditorKit actions for changing the color and style (bold, underline) of the text and StyleConstants.setAlignment for changing the text alignment (I had some issues with the StyledEditorKit.AlignmentAction). Let me know if you need any specific source code.
try encapsulating your text inside an html < p > tag - it should maintain line breaks.
I assume there is a key down event for this kit? Assuming you have a KeyEvent handler you should be able to do something like this:
if(e.getKeyCode() == KeyCodes.KEY_ENTER){
editor.Text += "<br />";
}

open office api text size

I am using open office API with Java UNO. I need to get size of selected text in the document content (for example embedded pictures have own size in mm but text inserted via XText.insertString(...) method doesn't have any size).
In other words: I want to get size (preferably in mm) of the box which surrounds part of text (it can be whole paragraph or selected text via some type of cursor). Is there any possibility to achieve that?
After searching, I think there is no option to achieve this at the moment. For my purposes I write small method for getting height of the paragraph in 1/100 mm.
Here is how this method works:
Get XTextViewCursor of the XTextDocumment controller for going left/right.
Go to paragraph to measure.
Loop through paragraph getting each char. For each char do: check its height (CharHeight property of the paragraph); get XLineCursor from XTextViewCursor and check if there is end of the line - if is then add (to the result) biggest height of the character in line.
This is temporary solution (still wait for something better) and has number of bugs (example line-spacing different than single; paragraph should only contain text) but maybe it will be helpful for someone.

How can I present multiline data in grid like component in Java and AWT

I have a few records of data (less then 10). Each record consists of a few lines of text.
I want to present records to the user in a kind of grid, where user can select one of the records.
I was thinking about List component or jTable, but I couldn't make them displaying more then one line of text. What component should I use then, or how to approach this?
In subject I suggested AWT because size does matter, i.e. I want use this functionality in the applet and would like to avoid any extra libraries.
Thanks in advance
Thanks to maksimov's link I found examples of how to tackle this issue, and also very interesting link I missed somehow - http://docs.oracle.com/javase/tutorial/uiswing/components/html.html
To specify that a component's text has HTML formatting, just put the
tag at the beginning of the text, then use any valid HTML in
the remainder. Here is an example of using HTML in a button's text:
button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>");
In my case it was just enough to set height of the row and add tag just before string data to be displayed. HTML tagging also let me use extra formatting, colors, etc,
Brilliant,
Thank you maksimiov

Turn fonts to Monospaced

I am trying to make a bizarre text editor for people with reading problems with Netbeans. You load the text you like and the editor starts highlighting it word by word with bold letters. The change from plain to bold constantly change the word dimensions and moves the line. One solution was the Monospaced Font but I would like to add a few more fonts available for the user to choose. Is there any way to do this with Arial for example by giving some orders to the JTextPane?
You can manually split the String with <br/> by counting characters and splitting at the right spot to keep the width under your desired character width. Give some leeway so if you get a big word, it won't still go to the next line.
Alternatively, you could use a JList to display your lines (instead of using <br/>). That way, there's no way the line would split to the next line. However, if you do it that way, the user will click on the list like a list and not be able to select text like in a normal text pane.

Categories

Resources