0

I am using AutoCompleteTextView to suggest my list , but i want the suggestions to be inline and not show a drop down list. How can i achieve that ? This is my current code that works as drop down list:

String[] birds = getResources().getStringArray(
            R.array.birds_list);

// Create the adapter and set it to the AutoCompleteTextView
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, birds);

mAutoCompleteTextView.setAdapter(adapter);
mAutoCompleteTextView.setThreshold(1);

mAutoCompleteTextView
            .setOnFocusChangeListener(new OnFocusChangeListener() {

                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    mAutoCompleteTextView.showDropDown();

                }
            });

mAutoCompleteTextView
            .setOnEditorActionListener(new OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView v, int actionId,
                                                  KeyEvent event) {
                    if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
                                || (actionId == EditorInfo.IME_ACTION_DONE)) {
                        Utils.hideSoftKeyboard(AddMedicationActivity.this);
                    }
                    return false;
                }
            });
Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
Jesus Dimrix
  • 4,378
  • 4
  • 28
  • 62
  • Possible duplicate of http://stackoverflow.com/questions/21812036/android-autocompletetextview-inline-predictions – Somnath Sarode Apr 27 '15 at 12:43
  • http://stackoverflow.com/questions/7458291/how-can-i-avoid-autocomplete-dropdown-appearing-when-text-is-programatically-set – Jain Nidhi Apr 28 '15 at 05:53

1 Answers1

0

This question is pretty old, but I have implemented a simple example of how this can be achieved, The example is written in kotlin but can easily be used with Java as well:

class EditTextInlineAutoComplete : AppCompatEditText {

private var listOfOptions: Collection<String> = emptyList()
private var lastTypedKeyCode: Int = 0

constructor(context: Context) : super(context) {
    init()
}

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
    init()
}

constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
    init()
}

private fun init() {
    val textWatcher = object : TextWatcher {
        
        override fun afterTextChanged(stringToFind: Editable?) {

            if (lastTypedKeyCode == KeyEvent.KEYCODE_DEL) {
                lastTypedKeyCode = KeyEvent.KEYCODE_UNKNOWN
                return
            }

            removeTextChangedListener(this)

            var result = stringToFind?.let { listOfOptions.filter { x -> x.startsWith(it) }.take(1) }
            LoggingHelper.d("Candidates: ${result?.joinToString()}")
            if (result?.isEmpty() == true) {
                addTextChangedListener(this)
                return
            }
            val predictedNumber = result?.get(0)
            val indexInOriginalText = stringToFind?.let { predictedNumber?.indexOf(stringToFind.toString()) } ?: 0
            val startIndex = indexInOriginalText + stringToFind?.length!!
            val endIndex = predictedNumber?.length
            val suffixOfNumber = predictedNumber?.substring(startIndex, endIndex!!)
            stringToFind?.append(suffixOfNumber)
            setSelection(startIndex, text?.length!!)

            addTextChangedListener(this)

        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            
        }

    }
    addTextChangedListener(textWatcher)
}

override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
    lastTypedKeyCode = keyCode
    return super.onKeyDown(keyCode, event)
}

fun updateCollection(strings: Collection<String>) {
    listOfOptions = strings
}

}

Asaf
  • 381
  • 4
  • 4