'AutoCompleteTextView to be inline
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;
}
});
Solution 1:[1]
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
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Asaf |
