10

I have a simple app that contains just an AutoCompleteTextView (code below). I have the OnItemClickListener and OnItemSelectedListener defined. Clicking on the individual items from the dropdown suggestions triggers the onItemClick event. However, with a bluetooth keyboard, using the arrows keys to navigate to a given item doesn't seem to trigger the onItemSelected event (the logs are not seen for this event).

What triggers this onItemSelected event? I was under the impression that a highlight on one of the dropdown items does it, but that doesn't seem to be the case.

If OnItemSelectedListener is not the correct event listener for the highlighted item, is there any that satisfies this requirement?

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {

    String[] options = {"a1", "a2", "a3", "b1", "b2", "b3", "b4", "b5"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // An adapter object
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, options);

        AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
        autoCompleteTextView.setAdapter(adapter);
        autoCompleteTextView.setThreshold(1);

        // Set the listeners
        autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("OnItemClick", "[AutoCompleteTextView] Item clicked");
            }
        });
        autoCompleteTextView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Log.d("onItemSelected", "[AutoCompleteTextView] Item selected");
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Log.d("onNothingSelected", "[AutoCompleteTextView] Nothing here");
            }
        });

    }
}
Some guy
  • 1,210
  • 1
  • 17
  • 39

1 Answers1

0

You should use onFocusChangeListener for this. On onItemSelectedListener is for selectable views (like the ones in listview).

vladimir123
  • 484
  • 6
  • 12