Advertisement
chayanforyou

InstantAutoComplete.kt

Mar 21st, 2023 (edited)
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.27 KB | None | 0 0
  1. import android.content.Context
  2. import android.util.AttributeSet
  3. import android.view.MotionEvent
  4. import androidx.appcompat.widget.AppCompatAutoCompleteTextView
  5.  
  6. /**
  7.  * Created by Chayan Mistry on 21/04/2023
  8.  * Always Visible AutoComplete Dropdown
  9.  */
  10. class InstantAutoComplete @JvmOverloads
  11. /**
  12.  * Constructor
  13.  * @param context Context
  14.  * @param attrs Attribute Set for view
  15.  */
  16. constructor(context: Context, attrs: AttributeSet? = null) : AppCompatAutoCompleteTextView(context, attrs) {
  17.  
  18.     /**
  19.      * Get android default attributes
  20.      * https://stackoverflow.com/a/70577844/5280371
  21.      */
  22.     private val sInputType = attrs?.getAttributeIntValue(
  23.         "http://schemas.android.com/apk/res/android",
  24.         "inputType", -1
  25.     ) ?: 0
  26.  
  27.     init {
  28.         inputType = sInputType
  29.     }
  30.  
  31.     override fun enoughToFilter(): Boolean {
  32.         return true
  33.     }
  34.  
  35.     override fun onTouchEvent(event: MotionEvent): Boolean {
  36.         if (event.action == MotionEvent.ACTION_DOWN) {
  37.             performClick()
  38.         }
  39.         return super.onTouchEvent(event)
  40.     }
  41.  
  42.     override fun performClick(): Boolean {
  43.         if (!isPopupShowing) {
  44.             performFiltering(null, 0)
  45.             showDropDown()
  46.         }
  47.         return super.performClick()
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement