Skip to content

Instantly share code, notes, and snippets.

@Ryszardenko
Last active September 17, 2020 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ryszardenko/b23252b9e577ebaefd5fcec9cbd24687 to your computer and use it in GitHub Desktop.
Save Ryszardenko/b23252b9e577ebaefd5fcec9cbd24687 to your computer and use it in GitHub Desktop.
//CustomClass removes soft keyboard on back press instead of suggestions
import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.ArrayAdapter
import android.widget.Filter
import androidx.core.content.ContextCompat
import ***your.package***
import ***your.package***.databinding.RecyclerSearchFilterItemBinding
class CustomAutoCompleteTextView : androidx.appcompat.widget.AppCompatAutoCompleteTextView {
constructor(context: Context) : super(context) {}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
super(context, attrs, defStyleAttr) {
}
override fun onKeyPreIme(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK && isPopupShowing) {
val inputManager =
context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if (inputManager.hideSoftInputFromWindow(
findFocus().windowToken,
InputMethodManager.HIDE_NOT_ALWAYS
)
) {
return true
}
}
return super.onKeyPreIme(keyCode, event)
}
}
class AutoCompleteAdapter(context: Context, private val listener: SelectionListener) :
ArrayAdapter<SelectionItem>(context, R.layout.recycler_search_filter_item) {
private val list = mutableListOf<SelectionItem>()
fun setList(list: List<SelectionItem>) {
this.list.clear()
this.list.addAll(list)
}
override fun getFilter(): Filter {
return filter
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val item = getItem(position)
val view = convertView ?: LayoutInflater.from(context)
.inflate(R.layout.recycler_search_filter_item, parent, false)
val binding = RecyclerSearchFilterItemBinding.bind(view)
binding.tvName.text = item?.name
setNameColor(item, binding)
onClickChangeSelectField(binding, item)
hideKeyboardOnSuggestionTouch(binding)
return binding.root
}
private fun setNameColor(
item: SelectionItem?,
binding: RecyclerSearchFilterItemBinding
) {
if (item?.isSelected == true) {
binding.tvName.setTextColor(ContextCompat.getColor(context, R.color.cobalt))
} else {
binding.tvName.setTextColor(ContextCompat.getColor(context, R.color.charcoal))
}
}
private fun onClickChangeSelectField(
binding: RecyclerSearchFilterItemBinding,
item: SelectionItem?
) {
binding.tvName.setOnClickListener {
if (item?.isSelected == true)
item.isSelected = null
else
item?.isSelected = true
item?.let { listener.updateList(it) }
}
}
@SuppressLint("ClickableViewAccessibility")
private fun hideKeyboardOnSuggestionTouch(binding: RecyclerSearchFilterItemBinding) {
binding.tvName.setOnTouchListener { v, event ->
val inputMethodManager = v.context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(v.applicationWindowToken, 0)
}
}
private val filter = object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val results = FilterResults()
val suggestions = ArrayList<SelectionItem>()
if (constraint.isNullOrEmpty()) {
suggestions.addAll(list)
} else {
val filteredList = list.filter { it.name.trim().contains(constraint.trim(), true) }
suggestions.addAll(filteredList)
}
results.values = suggestions
results.count = suggestions.size
return results
}
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
clear()
val list = results?.values as List<SelectionItem>
addAll(list)
notifyDataSetChanged()
}
override fun convertResultToString(resultValue: Any?): CharSequence {
return (resultValue as SelectionItem).name
}
}
}
interface SelectionListener {
fun updateList(item: SelectionItem)
}
interface SelectionItem {
val id: Int
val name: String
var isSelected: Boolean?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment