Skip to content

Instantly share code, notes, and snippets.

@AdamMc331
Created February 13, 2018 03:20
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 AdamMc331/989705c1259c93c580e5840c8dfe3c29 to your computer and use it in GitHub Desktop.
Save AdamMc331/989705c1259c93c580e5840c8dfe3c29 to your computer and use it in GitHub Desktop.
class ClickableListEditText<T> : AppCompatEditText {
var items: List<T> = ArrayList()
private val displayItems: Array<String>
get() = items.map { it.toString() }.toTypedArray()
private var mHint: CharSequence
var onItemSelectedListener: OnItemSelectedListener<T>? = null
constructor(context: Context) : super(context) {
mHint = hint
configureOnClickListener()
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
mHint = hint
configureOnClickListener()
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
mHint = hint
configureOnClickListener()
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
isFocusable = false
isClickable = true
}
private fun configureOnClickListener() {
setOnClickListener { view ->
val builder = AlertDialog.Builder(view.context)
builder.setTitle(mHint)
builder.setItems(displayItems) { _, selectedIndex ->
setText(displayItems[selectedIndex])
onItemSelectedListener?.onItemSelectedListener(items[selectedIndex], selectedIndex)
}
builder.setPositiveButton("Close", null)
builder.create().show()
}
}
interface OnItemSelectedListener<in T> {
fun onItemSelectedListener(item: T, selectedIndex: Int)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment