I have an AutoCompleteTextView in my layout. I also have an alternative way to select the same items which are present in the AutoCompleteTextView. When the alternative way is selected, I populate the value in the AutoCompleteTextView via:
autoCompleteTextView.setText(valueFromAlternativeSource);
where valueFromAlternativeSource is one of the valid auto complete options. The trouble with this is that the autocomplete dropdown appears when setText is called. Putting the following line after the above doesn't work:
autoCompleteTextView.dismissDropDown(); //Doesn't work. Why?
Any ideas on why dismiss dropdown isn't working or other ways I could dismiss the dropdown?
This works fine for me and is less complex:
ListAdapter adapter = autoCompleteTextView.getAdapter();
autoCompleteTextView.setAdapter(null);
autoCompleteTextView.setText("whatever");
autoCompleteTextView.setAdapter(adapter);
If you want to support API<17, Subclass AutoCompleteTextview and override setText(text, filter) method
#Override
public void setText(CharSequence text, boolean filter) {
if(Build.VERSION.SDK_INT>=17) {
super.setText(text, filter);
}else{
if(filter){
setText(text);
}else{
ListAdapter adapter = getAdapter();
setAdapter(null);
setText(text);
if(adapter instanceof ArrayAdapter)
setAdapter((ArrayAdapter) adapter);
else
setAdapter((CursorAdapter) adapter);
//if you use more types of Adapter you can list them here
}
}
}
Then whenever you want to set the text manually call setText(text, false)
Looks like its a problem of the order how messages are processed.
My work around looks like this:
//autoCompleteTextView.dismissDropDown();
new Handler().post(new Runnable() {
public void run() {
autoCompleteTextView.dismissDropDown();
}});
autoCompleteTextView.setText(valueFromOtherMeans, filter);
* #param filter If <code>false</code>, no filtering will be performed
* as a result of this call.
My solution (but I don't like it, there must be something better):
autoCompleteTextView.setText(valueFromAlternativeSource);
autoCompleteTextView.setDropDownHeight(0);
autoCompleteTextView.setOnKeyListener(new OnKeyListener(){
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
autoCompleteTextView.setDropDownHeight(LayoutParams.WRAP_CONTENT);
}
}
Related
I am inflating edit text in adapter class within of a list view. View is looking cool multiple edit text are appearing properly but when I do focus on the edit text or if I try to type something it loosing focus I tried to adding listeners, changing manifest file but nothing works for me.
following code is not working for me
#Override
public View getChildView(int position, int i1, boolean b, View view,
ViewGroup viewGroup) {
EditText comment = view.findViewById(R.id.txtRecordComment);
comment.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
if (lastFocussedPosition == -1 || lastFocussedPosition == position) {
lastFocussedPosition = position;
edittext.requestFocus();
}
}
}, 200);
} else {
lastFocussedPosition = -1;
}
}
});
return view;
}
please help me to resolve this issue
Since you are using a ListView, it is bound to happen with your current code implementation.
You must remember that if your EditText is in a ListView, every key down will result in the edit text losing and getting focus because all the views are redrawn, so the edit text representing whatever row used to be focused is now a completely different object.
To get the expected behaviour, declare a variable in your adapter: int focusedRow. In getView method of your adapter, add an onFocusChanged listener to the EditText and when that edit text gains focus, set focusedRow = whatever row the focused EditText happens to be in.
Also set any edit text that is in the currentlyFocusedRow to be focused.
Update:
If you have multiple EditTexts, add an onFocusChanged listener to each edit text.
Add below code in Android Manifest file
<activity android:name=".ActivityName"
android:label="#string/app_name"
android:windowSoftInputMode="adjustPan">
Hope this will work
I have a ListView with an EditText inside having an input type of numeric. When I use notifyDatasetChanged() in the focus event of the EditText I get an error.
is notifydatasetchanged()->focus->notifyDatasetChanged()->focus......
That error happens with android 7 or higher.
getView()
final ArrayAdapterListProduct.ViewHolder holder = new ArrayAdapterListProduct.ViewHolder();
holder.caption = (EditText) view.findViewById(R.id.changegift_listview_item_number);
if (promotionItem.getNumber_choose() != 0) {
holder.caption.setText(promotionItem.getNumber_choose() + "");
}
holder.caption.setId(position);
holder.caption.addTextChangedListener(new MyTextWatcher(holder.caption));
int temp_cout_product = 0;
if (promotionItem.getSoluongtonkho() > promotionItem.getPromotion_canget()) {
temp_cout_product = promotionItem.getPromotion_canget();
} else {
temp_cout_product = promotionItem.getSoluongtonkho();
}
holder.caption.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (!b) {
notifyDataSetChanged();
}
}
});
holder.caption.setFilters(new InputFilter[]{new InputFilterMinMax(0, temp_cout_product)});
return view;
So I can't input numberic into editext
The issue is you are setting the OnFocusChangeListener inside the getView() method and inside that you are calling notifyDataSetChanged() when the view does not have focus. What actually happens with this code is your ListView is created with the initial call to notifyDataSetChanged() and is filled with views that do not have focus. This means that every time getView() is called and you set the OnFocusChangeListener it's going to recall notifyDataSetChanged() resulting in an infinite loop.
Without seeing more of your code or understanding why you are calling notifiyDataSetChanged() inside a focus listener it's hard to help.
I'm building an interface similar to the Google Hangouts chat interface. New messages are added to the bottom of the list. Scrolling up to the top of the list will trigger a load of previous message history. When the history comes in from the network, those messages are added to the top of the list and should not trigger any kind of scroll from the position the user had stopped when the load was triggered. In other words, a "loading indicator" is shown at the top of the list:
Which is then replaced in-situ with any loaded history.
I have all of this working... except one thing that I've had to resort to reflection to accomplish. There are plenty of questions and answers involving merely saving and restoring a scroll position when adding items to the adapter attached to a ListView. My problem is that when I do something like the following (simplified but should be self-explanatory):
public void addNewItems(List<Item> items) {
final int positionToSave = listView.getFirstVisiblePosition();
adapter.addAll(items);
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(positionToSave);
}
});
}
Then what the user will see is a quick flash to the top of the ListView, then a quick flash back to the right location. The problem is fairly obvious and discovered by many people: setSelection() is unhappy until after notifyDataSetChanged() and a redraw of ListView. So we have to post() to the view to give it a chance to draw. But that looks terrible.
I've "fixed" it by using reflection. I hate it. At its core, what I want to accomplish is reset the first position of the ListView without going through the rigamarole of the draw cycle until after I've set the position. To do that, there's a helpful field of ListView: mFirstPosition. By gawd, that's exactly what I need to adjust! Unfortunately, it's package-private. Also unfortunately, there doesn't appear to be any way to set it programmatically or influence it in any way that doesn't involve an invalidate cycle... yielding the ugly behavior.
So, reflection with a fallback on failure:
try {
Field field = AdapterView.class.getDeclaredField("mFirstPosition");
field.setAccessible(true);
field.setInt(listView, positionToSave);
}
catch (Exception e) { // CATCH ALL THE EXCEPTIONS </meme>
e.printStackTrace();
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(positionToSave);
}
});
}
}
Does it work? Yes. Is it hideous? Yes. Will it work in the future? Who knows? Is there a better way? That's my question.
How do I accomplish this without reflection?
An answer might be "write your own ListView that can handle this." I'll merely ask whether you've seen the code for ListView.
EDIT: Working solution with no reflection based on Luksprog's comment/answer.
Luksprog recommended an OnPreDrawListener(). Fascinating! I've messed with ViewTreeObservers before, but never one of these. After some messing around, the following type of thing appears to work quite perfectly.
public void addNewItems(List<Item> items) {
final int positionToSave = listView.getFirstVisiblePosition();
adapter.addAll(items);
listView.post(new Runnable() {
#Override
public void run() {
listView.setSelection(positionToSave);
}
});
listView.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
#Override
public boolean onPreDraw() {
if(listView.getFirstVisiblePosition() == positionToSave) {
listView.getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
else {
return false;
}
}
});
}
Very cool.
As I said in my comment, a OnPreDrawlistener could be another option to solve the problem. The idea of using the listener is to skip showing the ListView between the two states(after adding the data and after setting the selection to the right position). In the OnPreDrawListener(set with listViewReference.getViewTreeObserver().addOnPreDrawListener(listener);) you'll check the current visible position of the ListView and test it against the position which the ListView should show. If those don't match then make the listener's method return false to skip the frame and set the selection on the ListView to the right position. Setting the proper selection will trigger the draw listener again, this time the positions will match, in which case you'd unregister the OnPreDrawlistener and return true.
I was breaking up my head until I found a solution similar to this.
Before adding a set of items you have to save top distance of the firstVisible item and after adding the items do setSelectionFromTop().
Here is the code:
// save index and top position
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
// for (Item item : items){
mListAdapter.add(item);
}
// restore index and top position
mList.setSelectionFromTop(index, top);
It works without any jump for me with a list of about 500 items :)
I took this code from this SO post: Retaining position in ListView after calling notifyDataSetChanged
The code suggested by the question author works, but it's dangerous.
For instance, this condition:
listView.getFirstVisiblePosition() == positionToSave
may always be true if no items were changed.
I had some problems with this aproach in a situation where any number of elements were added both above and below the current element. So I came up with a sligtly improved version:
/* This listener will block any listView redraws utils unlock() is called */
private class ListViewPredrawListener implements OnPreDrawListener {
private View view;
private boolean locked;
private ListViewPredrawListener(View view) {
this.view = view;
}
public void lock() {
if (!locked) {
locked = true;
view.getViewTreeObserver().addOnPreDrawListener(this);
}
}
public void unlock() {
if (locked) {
locked = false;
view.getViewTreeObserver().removeOnPreDrawListener(this);
}
}
#Override
public boolean onPreDraw() {
return false;
}
}
/* Method inside our BaseAdapter */
private updateList(List<Item> newItems) {
int pos = listView.getFirstVisiblePosition();
View cell = listView.getChildAt(pos);
String savedId = adapter.getItemId(pos); // item the user is currently looking at
savedPositionOffset = cell == null ? 0 : cell.getTop(); // current item top offset
// Now we block listView drawing until after setSelectionFromTop() is called
final ListViewPredrawListener predrawListener = new ListViewPredrawListener(listView);
predrawListener.lock();
// We have no idea what changed between items and newItems, the only assumption
// that we make is that item with savedId is still in the newItems list
items = newItems;
notifyDataSetChanged();
// or for ArrayAdapter:
//clear();
//addAll(newItems);
listView.post(new Runnable() {
#Override
public void run() {
// Now we can finally unlock listView drawing
// Note that this code will always be executed
predrawListener.unlock();
int newPosition = ...; // Calculate new position based on the savedId
listView.setSelectionFromTop(newPosition, savedPositionOffset);
}
});
}
I am trying this code
findUsStateOrMileSpinnerState.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view,
int i, long l)
{
spinnerState = findUsStateOrMileSpinnerState.getSelectedItem().toString();
if(spinnerState.equalsIgnoreCase("State")){
getDetailsState();
}
if(spinnerState == "Miles"){
getDetailsMiles();
}
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
}
);
On some selected item it should call another listener.
Spinner is having Miles and State.
But it is not going through the if statement, am I doing something wrong.
Looking forward to your reply.
thanks.
Did you try using the eqauls() method for comparing two strings in your code instead of == operator ?
if (spinnerState.equals("Miles") {
getDetailsMiles();
...
}
Check this out plz : http://www.java-samples.com/showtutorial.php?tutorialid=221
Try using the getSelectedIndex() instead of getSelectedItem(). You have to be sure you always get "State" and "Miles" at specified index.
You should do string compare via equals and not spinnerState == "Miles"
Try using equalsIgnoreCase or equals instead of using == to compare strings
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);