Get access to the beginning of the second line in TextView - java

There is a String, which in TextView automatically transfers words to the second line because of the large number of words in one line. I would like to access the beginning of the second line for further editing.
I tried to check for "\n", but it did not work.
What are the possible solutions?

Related

Reading lines in pdf file

I've got pdf file that looks like it:
example
It would be easy if each word of first line would have child in the next one. But sometimes the parents are empty below. I can get whole text from pdf and find those two lines. But if there is break in third column then reading the whole text I lose information if the third word was in forth column or third column. I hope I've made it clear, but I would appreciate idea how to read those two lines and connecting them, considering that not each parent has child in next line.

How to disable TextView automatic text formating for certain characters?

I have textview with droid sans mono font. Max line lenght is 10 characters. gettin it by this function.
this.getPaint().breakText(FULLTEXT, 0, 100, true, this.getWidth(), null);
I want lines always full if there is text and not the break halfway or something similar.
If I am using only normal characters like numbers and letters its working but for characters like "." or "," in certain situations the line break.
For example: if i have line:
"0123456789" or ".012345678"
tv is working as i want.. line is full..but with input:
"Y.012345678"
the line break this way:
".xxxxxxxxx"
"012345678Y"
where x are empty spaces.
i want to have multiline, i just dont want empty spaces (just in last line)
If i insert two special character like:
"01234\\.789"
its start breaking halfway if line is full. how i understand it, textview is breaking sentences which are too long for line.
Curently i am inputing different chareacter without this behaviour, but i would be happy if i didn't need too do this. (too much corections need too be done and its messy)
I noticed that descripted behaviour is not on every android version. I have devices with android 2.3.6, 4.0.4 and kindle fire version. I don't have problem on 2.3.6.
Is there a way how to disable this textview behaviour? thanks.
android:singleLine="true"
is the property of textview and edittext which will keep the text on single line. if length of string is too big then text will be truncated but will never go on next line.

Android TextView Define position of forced newline

In my application, Im having a Textview containing text in this style:
"123131 (Ø 374)"
On small devices, the text reaches the viewborder causing a new line at one of these spaces. In some cases, this makes the text look like this:
"123131 (Ø
374)"
Is there a way to force the new line to the first whitespace without using 2 TextViews? Isnt there some kind of "protected white space" or something like this?
"123131
(Ø 374)"
regards
danijoo
I solved it by recplacing all whitespaces but one with the unicode code for a nonbreaking whitespace:
"123131 (Ø\u00a0374)"
What I'd do is:
Implement a custom TextView with a listener when Ellipsis is triggered:
http://thanksmister.com/2012/10/16/android-textview-with-ellipsis-listener/
Then, when receiveing the listener, programmatically adding a '\n' character right before the '(' character.
Remember to set that TextView to be multi line.

Scrolling TextView in a ScrollView to a specific substring of text

For starters, I'm a C++/Qt Developer jumping into Android/Java development, so please don't assume I know anything. :) So, I have an application with a TextView contained with in a ScrollView. The text contained in the TextView is the results of a web query from my web server application. Depending on the query, the text can be pretty long. I want to implement a search feature on the text where the user enters some text string to search for, and if the text string is found, the ScrollView will scroll the TextView to ensure the entered substring is visible, and I want the text highlighted. I think I know how to highlight the text substring, I know how to tell the ScrollView to scroll to a specific line number in the TextView, but for the life of me, I can't figure out how to find out what line number to scroll to to guarantee my substring is visible. Ie, I want to query the text view to give me the line number that the substring first occurs at. Eventually, I want to also implement a find next that goes to the next occurrence of the substring. Kind of like the Find feature in Firefox. Any assistance is greatly appreciated. Thanks in advance.
You need to find the position of the substring in the text yourself, e.g. with String.indexOf(). Once you know that position, you can look up the correct line in the TextView using the Layout.getLineForOffset() function:
int offset=myString.indexOf("search string");
int line = myTextView.getLayout().getLineForOffset(offset);

A better algorithm to get the non-highlighted text from a semi-highlighted text

The context:
I have a text document that has some sentences in it highlighted. To locate the highlighted parts, I have a list that contains the start and stop positions of the highlighted parts. To get the highlighted parts, one can easily use Java's string.substring(start, stop) method. However, getting the non-highlighted parts is pretty tricky.
The problem:
My ultimate goal is to tokenize the text in the document and then assign a label for each token as either highlighted or not. Thus, what I ultimately need is to have two lists: 1- one list that contains the highlighted text chunks, 2- another list that contains the non-highlighted text chunks. Then, I would simply tokenize each chunk separately in each list.
The problem I'm facing is finding a clean algorithm to extract the non-highlighted text chunks. As I said, getting the highlighted chunks is easy because you already have their start and end positions and you can use Java's string.substring(start, stop) method.
For example:
This is a simple text, **this part is highlighted**, this part is not but **this is also highlighted,** but this one is also not.
The non-highlighted text chunks list should thus contain:
1 - This is a simple text,
2- , this part is not but
3- but this one is also not.
My approach:
The way I'm solving this problem is by adding the start and end positions of the highlighted parts into a hash table, where the start positions are keys and the values are the end positions. Then I start reading the text document character-by-character and check if the current character's position is a key in the hash table, if so I consider all already-read characters as a non-highlighted text chunk and add it to the non-highlighted text chunks list.
However, I find my approach kinda ugly and I was wondering if there is a better way to do this. I'm not good in String algorithms, and thought that there might be better approaches for this.
So my question: is there a better way to find the non-highlighted text chunks?
Put all your highlight locations in a list and sort them by start position. For each highlight in the list except the last, there is a non-highlighted area that starts on the end position of that highlight and ends on the start position of the subsequent highlight. Also, unless the first start position is 0, there is a non-highlighted area from 0 to the first start, and similarly for the last highlight.
(This assumes that there are no overlapping highlights - if there are, you first need go through the list and merge overlapping highlights. You can detect an overlap by checking if the end of one highlight is after the start of the subsequent highlight.)
The way I would do it is pop a pair of start and end positions of the highlighted text such that start is the smallest. Then I would construct a new pair that would represent start and end positions of unhighlighted text up to that point. So:
As this is the first iteration, first = 0 //first represents start position of unhighlighted text
Pop pair (min_start, min_end) from highlighted map
Construct pair (first, min_start - 1) and add to unhighlighted map
first = min_end //Or min_end + 1 if it's a closed interval
Loop steps 2-4 until highlighted map is empty
Add final pair (first, end_of_document) to the list

Categories