The char "\u200B" is automatically added to the EditText - android

I am working on an Android Application in which one I would like to compare some string values in EditText.
For example, in a first EditText, I start to entry "dav" and then select "David" from the keyboard suggestions. In a second EditText, I start to entry "dav", then select "David" from the keyboard suggestions and then correct the content to "Dav".
Every seems to be OK. If I retrieve the content of the EditText (with getEditableText().toString().trim()) the debugger tells me that "David" is a word composed by 5 characters and "Dav" a word composed by 3 characters.
If now I click on the EditText that contains "Dav" and I select "David" from the keyboard suggestions, the debugger tells me that the word "David" is composed by 6 characters. The last character is "\u200B".
Why this character is automatically add and how can I remove it in a generic way ?
Thank you for your help.

\u200B is a unicode character zero width space. It seems to me it's being added by the keyboard you are using. I assume if you change your keyboard it's possible you won't see that behavior.
One way to handle that is replacing that character and dealing with the actual String:
#Test
public void zero_space_character() {
String David = "David\u200B";
String theRealDavid = David.replace("\u200B", "");
assertNotEquals(David, theRealDavid);
assertEquals("David", theRealDavid);
}

It should be getText(). toString(). trim().

Related

How to detect and remove a unicode-sequence emoji symbol from inputConnection?

Let's say I have an edittext field and I have to implement "backspace" functionality on it.
Deleting a simple letter character is fine, it works:
Character.isLetter(inputConnection.getTextBeforeCursor(1, 0).toString()) {
inputConnection.deleteSurroundingText(1, 0);
}
The problem comes when the character is an emoji symbol.
Its length is expressed as 2 utf-16 chars, for an example:
Grinning face: 😀
Unicode codepoint: U+1F600
Java escape: \ud83d\ude00
In such a case, I would simply remove 2 chars.
However, there are cases where an emoji is formed by multiple codepoints, like:
Rainbow flag: 🏳️‍🌈
Unicode codepoint sequence: U+1F3F3 U+FE0F U+200D U+1F308
Java escape: \ud83c\udff3\ufe0f\u200d\ud83c\udf08
When I press backspace, only one java escaped char gets deleted, not whole emoji. For flag example, only this \udf08 last part would be deleted, presenting user with screwed up emoji symbol. Surrogate pair check doesn't get me out of the hole here, I would still have screwed up emoji.
How can I properly find out the correct amount of chars to remove, so I would delete 1 whole emoji when pressing backspace? (for the flag example, I would need to get the number 6, to remove it fully)

Android EditText return Vietnamese wrong format

I'm doing a Android project and facing a problem with EditText when I type Vietnamese.
Example, when i type the word "thử" into EditText and get string from it.
String text = edittext.getText().toString()
It always returns a String object with 4 characters "t", "h", "ư" and the accent character.
But if i create a String object by code like:String text = "thử";. It only contains 3 characters "t", "h" and "ử". So they do not match when I compare them. I want the String object contain 3 characters, not 4 characters.
I also think about a way that loop through all characters to replace them manually. But Vietnamese has 12 vowels and 6 accents so that it makes me have to check 72 cases. I don't think it is a good way. Anyway to get proper text from EditText? Or any good way to replace the text manually?UPDATE:I have found why the EditText always return weird String. It is cause by the phone keyboard app. I am using LG Magna and using default keyboard app. The app always encodes seperately base vowels and accents everything i input. I have just installed another keyboard app, then it works like a charm.Now, I have to find a way to make sure that the text always returns properly from any keyboard app.
Android use UTF-8 codepage, so please be sure that you're typing your vietnamese symbols using those UTF-8 but not any kind of Windows-1258`

Skip suggestion string value from Android EditText

I have an Android EditText with suggestions enabled. My goal is not to disable suggestion (since i'm able to do that), but to skip some string values from them.
For example, suppose that i'd like to skip 'hello' word.
If i write 'he' in the EditText, my suggestions list shouldn't contain 'hello' since it's a string that has to be skipped. Is it possible?
Hope i explained myself, thanks

Android EditText - Capitalize first letter of sentences using setText()

Folks,
I need to capitalize first letter of every sentence. I followed the solution posted here
First letter capitalization for EditText
It works if I use the keyboard. However, if I use setText() to programatically add text to my EditText, first letter of sentences are not capitalized.
What am I missing? Is there a easy way to fix or do I need to write code to capitalize first letters in my string before setting it to EditText.
The only thing the inputType flag does is suggest to the input method (e.g. keyboard) what the user is attempting to enter. It has nothing to do with the internals of text editing in the EditText view itself, and input methods are not required to support this flag.
If you need to enforce sentence case, you'll need to write a method which does this for you, and run your text through this method before applying it.
You can use substring to make this
private String capSentences( final String text ) {
return text.substring( 0, 1 ).toUpperCase() + text.substring( 1 ).toLowerCase();
}
Setting inputType doesn't affect anything put into the field programmatically. Thankfully, programmatically capitalizing the first letter is pretty easy anyway.
public static String capFirstLetter(String input) {
return input.substring(0,1).toUpperCase() + input.substring(1,input.length());
}

How to search and Highlight Text within an EditText

I've searched high and low for something that seems to be a simple task. Forgive me, I am coming to Android from other programming languages and am new to this platform and Java.
What I want to do is create a dialog pop-up where a user enters text to search for and the code would take that text and search for it within all the text in an EditText control and if it's found, highlight it.
I've done this before, for example in VB and it went something similar to this pseudo code:
grab the text from the (EditText) assign it to a string
search the length of that string (character by character) for the substring, if it's found return the position (index) of the substring within the string.
if found, start the (EditText).setSelection highlight beginning on the returned position for the length of
Does this make sense?
I just want to search a EditText for and when found, scroll to it and it'll be highlighted. Maybe there's something in Android/Java equivalent to what I need here?
Any help / pointers would be greatly appreciated
grab the text from the (EditText) assign it to a string
Try the code sample below:
EditText inputUsername = (EditText) findViewById(R.id.manageAccountInputUsername);
inputUsername.getText().toString()
^^ Replace the IDs with the IDs you are using.
After this, you have the standard string methods available. You could also use Regex for a complex search query:
http://developer.android.com/reference/java/lang/String.html
http://developer.android.com/reference/java/util/regex/package-summary.html

Categories

Resources