Android get focused word from edittext - java

Lets say I have an Android edittext with sentence: "I like Stackoverflow very much". Then I use my keyboard to be focused on the word Stackoverflow and now I click the button in my app that adds / removes for example bold effect for this specific word.
Is it possible to do that?
Thank you in advance.

if you are talking about position of cursor then use
int cursorPosition = et.getSelectionStart();
after taking position of cursor you should take whole text and apply desired effect. for example split text by white characters (spaces, enters), calculate on which word cursor is pointing and add/remove bold spannable
note that et.getText() returns Editable, not just usual String. So you can do some work on this class, e.g. check if word with cursor inside is already bold or other spannable applied

Related

Highlighting specific instances of a string for output to TextArea

I have a search function that scrolls through a String and finds specific instances of a substring and then notes the index for the location of the substring.
For example, if I'm trying to find the String
"AATACG"
in the string
"TACGATCAATACGACGATCAGT",
it will return 7 as the index of the substring. What I need is a way to color the substring. So, the return text will be
"TACGATCAATACGACGATCAGT",
but with the substring colored. The text is outputted to a JavaFX TextArea.
I've tried using ANSI codes (which didn't work); I've also tried changing the string into a Text object and setting the fill color/applying a CSS id, but I can't find a way to change the Text back into a String with the color change remaining.
Is there a way to do this? Any help is much appreciated.
How about using javafx.scene.text.TextFlow? Seems like you want to highlight text without editing it. In this case TextFlow is appropriate component.

Divide string in lines

I am currently trying to fix my problem with string length at buttons, in android application. So i parse string and then set it as button text. But if one text is bigger then 9 characters or 10 it gets displayed out of the button overlay. I know i could make text smaller but this is not good solution because i already have small text.
So what you guys recommend for example if i have:
String text = "ThisIsSomeRealyLongText";
How can i split this string in 2 lines or when i reach character number 9 just use /n (to break text)?
Using too much text on a button is never a good idea. You should think about using an icon, or shorter text with (if needed) more explanation text somewhere else. Remember,the best UX will be the one with the least amount of reading involved.
Using regex you can do this. It will break the string into 9 characters per line.
text = text.replaceAll("(.{9})", "$1\n");
Use
(new line):
android:text = "ThisIsSome
RealyLongText"
But in code, you can just use \n. You can define in ../res/values/strings.xml:
<string name="longname">ThisIsSome\nRealyLongText</string>
then set the text for you button
android:text="#string/longname"

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.

Moving Text Cursor in TextArea Java

Is there possible to move text cursor in TextArea (or other text component)? How? is there a function to do that?
Assume, we have a TextArea and 2 button. Each time we press the first button, the text cursor will go to the position between second letter and third letter. If only two letter, the text cursor will go to the end of string. Each time we press the second button, the text cursor will go to the beginning of string.
Could anybody help me? Thx before :D
Is there possible to move text cursor in TextArea
Its called a caret in Java. Check out the API for methods with caret in the method name. For example:
textArea.setCaretPosition( textArea.getCaretPosition() + 1 );

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);

Categories