John Ward
John Ward

Reputation: 940

Can't get user entered text from android AutoCompleteTextView after item selected

I have an AutoCompleteTextView in a Fragment (android.support.v4.app). After the user selects an item in the dropdown, I want to retain the text they entered. Here's what I have.

First the class definition:

public class MyFragment extends Fragment
implements
    AutoCompleteTextView.OnDismissListener,
    AutoCompleteTextView.OnKeyListener,
    AutoCompleteTextView.OnEditorActionListener {

In onViewCreated() I have these assignments:

    myAutoComplete.setOnDismissListener(MyFragment.this);
    myAutoComplete.setOnKeyListener( MyFragment.this );
    myAutoComplete.setOnEditorActionListener(MyFragment.this);

And I've added these methods:

@Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
    Log.i("************(arg0)  ", arg0.toString());
    Log.i("************(arg1)  ", arg0.toString());
    Log.i("***********(keyevent)  ", arg2.toString());
    return false;
}

@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
    Log.i("************(arg0)  ", arg0.toString());
    Log.i("************(arg1)  ", arg0.toString());
    Log.i("***********(keyevent)  ", arg2.toString());
    return false;
}

@Override
public void onDismiss() {
    // Fires when the dropdownlist is dismissed.
    // The event is assigned in onViewCreated
}

My first hope was that the onDismiss() event would fire before the user-entered text was replaced by the selected item. The event, as shown, does fire correctly, but after the users text was replaced with the selected item.

I then wired up two different key listeners, hoping to capture and store the value while it's being entered. These do not fire. After researching this issue, I've learned that key listeners were only designed to work on physical keyboards. Though some people appear to have gotten it to work with soft kybds, broad compatibility appears to be a problem.

So I'm stuck, and I don't have any hair left. Anyone have any suggestions?

Upvotes: 0

Views: 244

Answers (1)

marcos E.
marcos E.

Reputation: 475

@Rushi Ayyappa is right use TextWatcher

autoCompleteTextView1.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(){
          if(isSpinnerAlreadySelected){
              autoCompleteTextView1.setEnabled(true);
              autoCompleteTextView1.performClick();
          }
      });


autoCompleteTextView1.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
             if(isSpinnerAlreadySelected){
                 autoCompleteTextView1.setEnabled(false);
                 //Notify the user he cannot type and why?? Toast??
             }
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

Upvotes: 1

Related Questions