Android AutoCompleteTextView is overlapping while typing suggestion list - android

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.

Related

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: Search from listView with editText and custom tags

I have a program which stores items to database, and I want to make a search view for those items. Database items have a name and a set of tags linked to them (These tags, to be exact: https://android-arsenal.com/details/1/2566). In the search view, I want to list all the items in the db and within the same view, filter the results by name, using input in editText, and by the tags, by showing the available tags as a tag cloud. Does anyone know an example or tutorial where similar search structure would have been implemented?
You may into this tutorial:
http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/
You can use filters on the listview as shown in the above example. You can implement your own filters for custom functionality. Use of onTextChangedListener depends upon your use case.
This is code snippet of how to implement simple search function on edittext text change. Hope you get an idea
searchInput.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
if (!s.toString().isEmpty()) {
CenterRepository.getInstance().getSearchResultCollection().getListOfSearchResult().clear();
//Iterate over reference models
for (ReferenceModel model :
CenterRepository.getInstance().getXRefModelCollection().getListOfReferenceModels()) {
//if serach key is present in model name or model number
if (model.getModelName().toLowerCase().contains(s.toString()) || model.getModelNumber().toLowerCase().contains(s.toString())) {
//add to search collection
CenterRepository.getInstance().getSearchResultCollection().addToSearchResultsCollection(model);
}
}
//display search results
renderListData(true);
} else {
//display all models
renderListData(false);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});

How to make a custom Data Picker like Date Picker in Android

How to make a custom Data Picker like Date Picker in Android so that I can get data (int data) by custom input or by using the buttons (- & +) so that it become easy for user to enter larger value !!
Has anyone any idea how to implement this?
please provide example, code snippet if possible.
I want to make like this
left - Button
middle - EditText
right - Button
I want Like this image--
This is the example of Date Picker in Android:-
then try this link
try on text changed listener as described in the link to update the variable whenever user changes the edit text, then variable will hold the changed value and so you can use your method to change values of that variable
Design a layout as you want with two buttons and one edittext in middle
Then give one button addition funtionality and other substraction functionality
Follow little code snippet, provided below
addButton.setOnClickListener(new OnClickListener(){
public void onClick(){
int valueInEditText = Integer.parseInt(etReference.getText().toString());
etReference.setText(String.valueOf(valueInEditText + 1));
}
});
Similaryly, do it for substration button.
Note: Do validation if you don't want negative values.
You might want to use the NumberPicker which will allow you to select any value.
It's not the same look but it might work for you:
https://android--examples.blogspot.com/2015/05/how-to-use-numberpicker-in-android.html
After reading through various posts, examples, articles and Android documentation I found solution to my own question, It can be achieved by two ways:
First by using NumberPicker
Secondly by adding TextChangedListener to the EditText
Here is the code how I achieved it :
int noOfCups = 2;
EditText editTextNoOfCups;
editTextNoOfCups = (EditText) findViewById(R.id.edit_text_no_of_cups);
editTextNoOfCups.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) {
Log.e("pss", String.valueOf(s));
noOfCups = Integer.parseInt(String.valueOf(s));
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
Here is the link to official Android documentation :
TextWatcher | Android Developers

AutoCompleteTextView detect when selected entry from list edited by user

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.

Disable Android AutoCompleteTextView after user selects item from drop down

I'm using Android's AutoCompleteTextView with a CursorAdapter to add autocomplete to an app. In the view's onItemClickListener() (i.e. when the user touches one of the autocompleted drop down items) I retrieve the text and place it in the EditText so that the user can modify it if they need to.
However, when I call setText() on the TextView the autocomplete behavior is triggered and the dropdown shows again. I'd like to only show the dropdown if the user types new text with the keyboard. Is there a way to do this?
You can use the dismissDropDown() method of the AutoCompleteTextView object. Take a look at the documentation.
When we click on item suggested in AutoCompleteTextView.onTextChanged() is performed before onItemClick
So, to avoid this try below code..
autocompletetextview.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (autocompletetextview.isPerformingCompletion()) {
// An item has been selected from the list. Ignore.
} else {
// Perform your task here... Like calling web service, Reading data from SQLite database, etc...
}
}
#Override
public void afterTextChanged(final Editable editable) {
}
});
If you wish to dissmis AutoCompleteTextView's dropdown you should use its post(Runnable r) method. It works for me :)
Here is an example:
mAutoCompleteTextView.post(new Runnable() {
public void run() {
mAutoCompleteTextView.dismissDropDown();
}
}
Answering my own question after a couple hours of hacking at this: It turns out you should implement your own OnItemClickListener and instead rely on the existing click listener to populate the TextView. I had originally implemented the onItemClickListener because it was using the results of Cursor.toString() to populate the text view. To change the output String, you should implement convertToString(Cursor) in your CursorAdapter. The CharSequence that gets returned will be populated in the text view.
Doing this will also prevent the dropdown from showing up again (since setText() triggers the completion behavior but the default onItemClickListener does not).
Different approach.
I agreed dismissDropDown() works but in my case, it wasn't working as expected. So, I used:
autoCompleteTextView.setDropDownHeight(0);
And if you want to show the dropdown list again, you an use
autoCompleteTextView.setDropDownHeight(intValue);

Categories

Resources