Skip to content

Instantly share code, notes, and snippets.

@NielsMasdorp
Created June 26, 2018 05:53
Show Gist options
  • Save NielsMasdorp/c430b7993f72728d0c231f90072c5d68 to your computer and use it in GitHub Desktop.
Save NielsMasdorp/c430b7993f72728d0c231f90072c5d68 to your computer and use it in GitHub Desktop.
Show an AlertDialog with EditText that is properly tinted
private fun show() {
    val input = AppCompatEditText(context).apply {
        setHint(R.string.parking_product_add_promo_code_hint)
        ViewCompat.setBackgroundTintList(this, ColorStateList.valueOf(ContextCompat.getColor(context, R.color.orange)))
    }
    AlertDialog.Builder(ContextThemeWrapper(context, R.style.DFWTheme_Parking))
        .setTitle("Title")
        .setEditText(input)
        .setPositiveButton("Button", null)
        .create()
        .show()
}

With added extension function:

fun AlertDialog.Builder.setEditText(editText: EditText): AlertDialog.Builder {
    val container = FrameLayout(context)
    container.addView(editText)
    val containerParams = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT
    ).apply {
        topMargin = context.resources.getDimensionPixelSize(R.dimen.spacing_small)
        leftMargin = context.resources.getDimensionPixelSize(R.dimen.spacing_medium_small)
        rightMargin = context.resources.getDimensionPixelSize(R.dimen.spacing_medium_small)
    }
    container.layoutParams = containerParams
    setView(FrameLayout(context).apply {
        addView(container)
    })
    return this
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment