Android TextWatcher Replace Underscores While Typing - android

Intro:
I am currently trying to implement an input method for an EditText for my Crossword Puzzle where the user sees something like "____" in the EditText. The underscores mark missing letters, the first char entered will fill the first underscore.
Of course other cells in the puzzle might be solved already, so the EditText text could be "ST_CKOV_RF_OW". I had all this functionality already in my own input view, a subclass of view with an overridden onDraw(). This worked pretty well, except that the view won't appear on some lower Android versions and the Back key slipped through my input routine and wasn't accessible.
So I thought I'd do it with EditText, implement a TextWatcher and be fine, but I can't get it to work properly. What I have right now is working, I can use the keyboard to enter letters, but again the Backspace isn't working, and of course if the user touches into the EditText the position gets messed up.
public void beforeTextChanged(CharSequence s,int start,int count, int after){
et.removeTextChangedListener(textWatcher);
int position = text.indexOf("_");
if(position==-1) onAnswerEntered(et.getText().toString().replace("_", "")); //finished
else {
et.setSelection(et.getText().toString().indexOf("_"));
et.addTextChangedListener(textWatcher);
}
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
et.removeTextChangedListener(textWatcher);
try {
String currentKey = s.toString().substring(start, start+1);
Logger.log("Current Key", currentKey);
int position = text.indexOf("_");
Logger.log("Current Position _ ", position+"");
//replace _ with key
String sbefore=text.substring(0, position);
String safter=text.substring(position+1, text.length());
text=sbefore+currentKey+safter;
int positionNext = text.indexOf("_");
Logger.log("Next Position _ ", positionNext+"");
if(positionNext==-1) onAnswerEntered(et.getText().toString().replace("_","")); //finished
else {
et.setText(text);
et.setSelection(et.getText().toString().indexOf("_"));
et.addTextChangedListener(textWatcher);
}
} catch(IndexOutOfBoundsException ioobe) {
ioobe.printStackTrace();
}
}
I also tried to set an OnKeyListener, but it won't work on EditText (I can get backspace event, nothing else)
So maybe I am totally on the wrong track, but please help me and give me a clue to how I can accomplish my goal. Thanks.

I gave up on it and implemented a simple but working kind of hack. I receive input in my (hidden) EditText now, the output goes to the visible TextView, a function in between fills the "_" with the input from the EditText.
Ex.
hint = "A_A_A_A"
edittext input = "BBB"
textview shows "ABABABA"

Related

EditText setSelection clearing text then re-entering

I have been having issues with some odd behaviours from the EditText .setSelection that I am hoping you can all help with!
The app I am working on has a search field and there is the need to have it behave very similar to a browser search bar. For example, if the user types "fo", we would want the EditText to autocomplete to "foobar" with the autocompleted "obar" text highlighted so it can be easily replaced by the user incase the autocomplete does not match what the user was intending to type.
To accomplish this, I have an EditText field with a TextWatcher setup to try to autocomplete the text afterTextChanged. The following is my code:
editText.addTextChangedListener(new TextWatcher() {
int lastCount = 0;
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable editable) {
editText.removeTextChangedListener(this);
String searchString = editable.toString();
if (editable.length() > lastCount) {
lastCount = editable.length();
int oldLength = searchString.length();
String autoFillResult = completeAutofill(searchString);
if (!autoFillResult.equals("")) {
searchString = autoFillResult;
editable.clear();
editable.append(autoFillResult);
editText.setSelection(oldLength, autoFillResult.length());
}
} else lastCount = editable.length();
editText.addTextChangedListener(this);
}
});
My issue is as follows. Using the previous "Foobar" case:
The user types "F", the EditText autofills "oobar" and highlights it.
Then the user types the first "o"
The EditText field is momentarily cleared (i.e. afterTextChanged receives an Editable with the empty string)
The EditText correctly autofills to user supplied "Fo" followed by autocompleted "obar" which is highlighted.
The issue is the EditText is being cleared then repopulated when the user types the next character, which creates a noticeable disturbance in the EditText field. Interestingly, I have singled out the editText.setSelection(oldLength, autoFillResult.length()); as the culprit (i.e. commenting out the line gets rid of the issue, but obviously its the wrong functionality).
After completing some Google research and my own debugging I am still unsure why this is happening. The issue does not appear excessively common as I could not find it on Google and I could not figure out the reason for this issue in my own experimentation.
Thank you in advance!
the if condition should possibly be:
autoFillResult != null && !autoFillResult.equals("")
while you might be looking for (or attempting to recreate) an AutoCompleteTextView.
Try removing editable.clear();

Positioning the cursor in an un-editable EditText on Android application

I have been reading the answers from the post How to set cursor position in EditText? I do have a specific problem in my EditText which might be the cause of the problem. In my xml layout, I define an EditText called "edit" whose inputType = none. According to the android developer documents, if the inputType = none, the text in not editable. The reason why is "none" is caused this is the way I prevent the android general keyboard from popping up over my User-Interface , which works very well.
I want to do something like this: user entered : 5 + sin( ) , now the cursor editText cursor position should be right after the ) symbol, but there's no cursor shown since the inputType = none. What I need to do is move the position of where the next text input will be placed in between the ( ) symbols. What happens is that the next text input goes right after the ) symbol.
So if the next text input is 4, then: 5 + sin( )4 , I don't want this. I rather have the following: 5 + sin(4 )
I am using a custom made keyboard which has the button "( )". Here's what I have tried without any success:
//once the user presses the "( )" button:
Selection.setSelection(edit.getText(),edit.getText().length() - 2);
Another way I have tried:
//once the user presses the "( )" button:
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setSelection(edit.getText().length() - 2);
edit.setInputType(InputType.TYPE_NULL);
Neither of the two ways has worked for me. I've also tried to set the inputType of the editText to something else besides "none" on the xml file. But, the android keyboard pops up on top of my custom made keyboard.
Any thoughts or ideas would be helpful
I don't know how exactly does your () button work but you can give a try this one.
yourEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if( s.length()>0 && s.charAt(s.length()-1) == ')' ){
yourEditText.setSelection(s.length()-1);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});

android: EditText input format

In my app I have to format the input of two EditTexts like:
1234 4567 67: Ten digits that grouped by four. (The space is automatically, not inserted by user)
11/14: Four digits that separated by '/'. (The '/' is inserted automatically)
I don't know how to do it. Please help:
Put a listener on the edit text as afterTextChanged.
Get the number of digits by using the length() function.
Once you get the number of digits, you can extract each digit and then insert space of '/' at the appropriate place.
len=editText.getText.toString().length();
then you can do the appropriate change by checking the length.
num=Integer.parseInt(editText.getText.toString());
temp=num;
if(len>=10)
{
A:
if((len-4)>0)
{
for(i=0;i<(len-4);i++)
{
temp=temp/10; //we get the first 4 digits
}
editText.setText(temp+" "); //place letters and add space
temp=num%(10^(len-4)); //get the num without the first-4 letters
len=len-4; //modify length
goto A; //repeat again
}
editText.setText(temp); //add the last remaining letters
}
else if(len==4)
{
temp=num;
temp=temp%100; //store the last 2 digits
num=num/10; //get the first 2 digits
editText.setText(num+"/"+temp);
}
i havnt tried this but i think this will work.
Hope this will help. :)
I can think of two ways of achieving it:
Use addTextChangedListener:
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// Do your tricks here
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
Create custom Edittexts
This link wont do what you are looking for, but will give you an idea how to create custom EditText.
Use "onKeyListener" to get the input event.
EditText OnKeyDown
Then check for correct input and count the digits. Add the whitespace/slash in your code.
Sample code:
if (editText.getText.toString().length() % 4 == 0) editText.setText(editText.getText.toString() + " ");
Didn't try it by myself, but this would be the way i would try. In addition i would check for numeric input too.

Permanent hint in EditText

I have a situation where I would like the user to complete a sentence for me. For example, consider a EditText with a hint of "The last time I ". Normally, when a user clicks an EditText, the hint disappears, but I would like it to stay. Additionally, I would like the text to be permanent, so that it cannot be erased... leaving the user with only one option... complete the sentence.
The first part is fairly simple, just use the setText() method of EditText to place the hint. The difficult part is the latter. How can I have text in an EditText that the user cannot erase?
Well couldn't you do it in code? Some algorithim like, if the text is less than 16 characters (length of "The last time I ") then set the text to that. Therefore whenever they clicked it, if they tried to erase it, it would just go back to the default text.
Also, another idea..why don't you just make a TextView thats right edge aligns with the left edge of the EditText box, the user would never know that it was another box. This is acutally the best solution, if you don't want the text ever to be edited, just make it a TextView
Described problem can be solved using android.text.TextWatcher.
public class CompleteSentenceWathcher implements TextWatcher {
private final String initialText;
private int start;
private int after;
private int count;
public CompleteSentenceWathcher(String initialText) {
this.initialText = initialText;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
this.start = start;
this.count = count;
this.after = after;
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(start < initialText.length()) {
if(s.toString().startsWith(initialText)) {
return;
}
if(count >= 1 && after == 0) {
if(start+count+1 <= initialText.length()) {
s.replace(start, start+count, initialText.substring(start, start+count+1));
} else {
s.replace(start, start, initialText.substring(start, start+1));
}
} else if(count == 0 && after >= 1) {
s.delete(start, start+after);
}
}
}
}
Create an instance of EditText and add the TextWatcher.
EditText editText = new EditText(this);
editText.setText("I love");
editText.addTextChangedListener(new CompleteSentenceWathcher(editText.getText().toString()));
I've implemented this with an InputFilter, where _PERMANENT_HINT_TEXT is the text at the end of the EditText that I don't want the user to be able to modify. I recommend adding a color span to it, so that it is grayed out to hopefully look like a hint/disabled section of text. This should hopefully improve the UX as they should automatically assume it is unmodifiable, and not just wonder why some part of the EditText (that they usually can completely change) isn't "working". This approach allowed the text to be set after
the InputFilter was set on the EditText, which was a requirement for me since I used this on an EditTextPreference.
To be clear, I needed the permanent text to exist at the end of the EditText, instead of the beginning, but that should be symmetrical to my implementation.
new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int source_start, int source_end,
Spanned destination, int destination_start, int destination_end) {
final int protected_text_start = (TextUtils.isEmpty(destination)? source.length() : destination.length()) - _PERMANENT_HINT_TEXT.length();
// Allows input into unprotected region
if (source_start + destination_start - source_end < protected_text_start)
return null;
// Prevents deletion of protected region
else if (TextUtils.isEmpty(source))
return destination.subSequence(destination_start, destination_end);
// Ignores insertion into protected region
else
return "";
}
}
use EditText.setFilters(new InputFilters[] { /* InputFilter goes here */ }; to add it to the desired EditText.
Just checking for the length wouldn't be adequate... I could type "This is a really long text I put into the box" and it would accept it even though it doesn't begin with "The last time I" string.
Personally, I would probably go for the prevention method suggested of using a TextView over that of a check on the way out. But if you're going to validate it afterwards, you'd actually need to check the beginning of the returned string.

Android EditText boxes

I want to cause the focus of one edit text box to move to another on editting (meaning you can only type on letter before it automatically moves on to the next edit text).
It's the "on edit" that I can't get my head around. Can anyone help me out with a simple example? Theres a lot I need to implement it into, so just a basic understanding should set the ball rolling ^_^
I do not really recommend this. With soft keyboards and multiple languages, what exactly is "one letter"? After all, a soft keyboard might enter in an entire word, like it or not.
CommonsWare makes an excellent point: you can't prevent the user from adding more characters to the EditText box, however you can listen to what's changed and act on that. Here's how to:
EditText editbox = (EditText) findViewById(R.id.MyEditBoxName);
editbox.addTextChangedListener(new TextWatcher()
{
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
public void afterTextChanged(Editable s)
{
// Test s for length, request focus for the next edit.
// editbox2.requestFocus();
}
});
Be careful not to get yourself into an infinite loop changing the editbox, any changes you make will cause these methods to be called again recursively.

Categories

Resources