Created
February 13, 2018 03:20
-
-
Save AdamMc331/989705c1259c93c580e5840c8dfe3c29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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