AutoCompleteTextView detect when selected entry from list edited by user - android

I have an AutoCompleteTextView I use to select an item from a long list. The user should only be able to select a predetermined item from the list. They should not be able to enter their own item.
The way I check to make sure they submit only an item from the list is to use setOnItemClickListener to trigger a boolean flag. The problem is that after the boolean flag is set to true, they can still edit the selected text of the item. I need to detect this and set the boolean flag to false again. How do I do this. I have seen a suggestion to use onKeyDown, but I am not sure how to implement this.

You can add text changed listener:
autoCompleteTextView.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});

Implement a TextWatcher, which will give you 3 methods which will constantly get call backs when someone changes the text.
If the string grows, your user is typing by himself again.

Use
AutoCompleteTextView#setOnItemSelectedListener()
- works like a charm.

Related

Android AutoCompleteTextView is overlapping while typing suggestion list

I am trying to implement HashTag and mention by using AutocompleteTextview. It is working fine.
Depends on the entered keyword I am setting adapter in AutocompleteTextview. For that, I am using TextWatcher. It is showing the result. Till this working fine. But when I am typing the suggestion list is overlapping.
In the image, black Shadow is because of multiple pages. The number of pages is increasing when text in AutocompleteTextview is changing.
autoCompleteTextview.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.toString().startsWith("#")) {
setupTagUserAutocomplete(data);
}
else if (s.toString().startsWith("#")) {
setupHashAutocomplete(list);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
How to avoid this?
You do not have to use TextWatcher. Using TextWatcher will create a new suggestion list every time the text is changed.
You have to use an adapter for that purpose.
That is the reason it is creating so many suggestion-lists.
You have to create a custom adapter since there is a custom requirement because of the two conditions ( # and #).
Here is a link to an AutoCompleteTextView tutorial for you to go through. I hope you find this one useful.
https://www.studytonight.com/android/autocomplete-textview
In the above tutorial refer to the steps, but instead of an ArrayAdapter create a custom one. Inside that adapter, you can have that list according to the text.

TextWatcher monitor only current input

I have EditText which is connected with TextWatcher I'm monitoring when user presses # letter. That will make a Listview appear with names of commentators on particular post. When user chooses one of the users from ListView, name is append to EditText and ListView is hidden.
But the problem is when user continues typing ListView will appear again because afterTextChanged(Editable s) monitors the whole inputted text which already contains letter #.
Is there a way to monitor only what user is actually typing not the whole inputed text? Or somehow escape last inputed word in TextWatcher? Or any other suggestions how to solve this.
I was researching but didn't find anything useful.
Thanks in advance
There can be many ways to achieve this. This is one of them. You can check weather your list is already filled or not.
final boolean isListSet = false;
public static final String textToFind = "#";
editText.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) {
}
#Override
public void afterTextChanged(Editable s) {
if (!isListSet && s.toString().contains(textToFind)) {
// set your list here
isListSet = true;
}
if (!s.toString().contains(textToFind)) {
// remove your list
isListSet = false;
}
}
});

Lag after setting text in OnTextChanged()

Below is an example that replicates my lagging problem. Once I set the text on the EditTextView it takes at least 1.5 seconds for the user to be allowed to input another character.
amountEditText.addTextChangedListener(new TextWatcher() {
#Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override public void afterTextChanged(Editable s) {}
#Override public void onTextChanged(CharSequence s, int start, int before, int count) {
String amount = s.toString();
if( !amount.equals(current )) {
amountEditText.removeTextChangedListener(this);
amountEditText.setText(s);
Selection.setSelection(amountEditText.getText(), amountEditText.getText().length());
amountEditText.addTextChangedListener(this);
}
}
});
I've searched around and have not found a solution.
I identified that the issue was coming from the textView.setText() call.
The solution was to not use setText(), instead use the Editable that is provided to you in the onTextChanged callback.
I tried to use the Editable before, however i couldn't get it working with inputs such as "$12,000".
This was due to having InputFilters still attached to the Editable.
Regards,
Scott.
It lags because you remove and then readd your listener.
You seems to be trying to select the text inside the EditText. To do so, simply set editText.setSelectAllOnFocus(true); or android:selectAllOnFocus="true" in xml. Then remove the entire TextChangedListener from your code.
According to me it is most probably because of trying to remove (amountEditText.removeTextChangedListener(this);) and add (amountEditText.addTextChangedListener(this);) the Text Change Listener every time. I would recommend you to replace your code without those adding and removal.
Hope this helped.

Android: How to keep Default number always even if user wants to delete it, and how to delete default number when user inputs a value?

I have a simple EditText, which inputs only numbers.
I want to show 0(zero) even if user presses backspace.
Now zero is getting deleted.
Moreover, I need to remove this default zero, when user starts entering values.
How do i achieve it?
You should use android:hint="0" or in more complex way..
You should use TextWatcher and override the below three methods. THer you can get the text in the arguments as CharSequence in the beforeTextChanged and onTextChanged methods and in afterTextChanged you have Editable from where you can get the data in the EditText. Write in the desired function as per your logic and it should work like a charm.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
It sounds like you want:
<EditText
android:hint="0" />

Android - EditText saves T9 suggested values

I have an application where user inputs text into EditText field. After user clicks OK (in keyboard input mode), a correct value is in the EditText (lets say "Smile").
if (answers.get(counter).getText().equals(opponentAnswers.get(counter)))
But this if statement fails, because the same EditText has the values that were suggested by T9 option, when user was inputing his answer (for example values of EditText would be "Smile Smiling Smiled"), while it should only have a value "Smile".
Any ideas how to solve this issue?
That's really weird. These are kind of guesses, but this is what I'd try next if I were you:
A. Instead of doing an equals against getText(), try doing a toString on getText(), so:
if (answers.get(counter).getText().toString().equals(opponentAnswers.get(counter)))
B. If that doesn't work then you could try adding a TextWatcher using addTextChangedListener on the EditText, and getting the value from that. Calling toString() on the editable returned in afterTextChanged might give you the value you want.
private class SearchTextWatcher implements 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) {
}
#Override
public void afterTextChanged(Editable s) {
//Get the text the user sees
String textShownToUser = s.toString();
}
}
Hope this helps! Best of luck!

Categories

Resources