'Material AlertDialog height issue
For some reason the height of MaterialAlertDialogBuilder became larger than its content. I am not sure if this is a padding bottom.
fun Context.materialDialog(
title: String?,
message: String?,
positiveLabel: String?,
positiveListener: DialogInterface.OnClickListener?,
negativeLabel: String?,
negativeListener: DialogInterface.OnClickListener?,
isCancelable: Boolean
) = MaterialAlertDialogBuilder(this).apply {
title?.let {
setTitle(title)
}
message?.let {
setMessage(message)
}
positiveLabel?.let {
setPositiveButton(it, positiveListener)
}
negativeLabel?.let {
setNegativeButton(it, negativeListener)
}
setCancelable(isCancelable)
}
alertDialog = materialDialog(
getString(R.string.to_usd, _getAsset.name),
null,
getString(R.string.navigation_close),
{ dialog: DialogInterface, _: Int -> dialog.dismiss() },
null,
null,
false
).create()
convertAction.setOnClickListener {
val linearLayoutCompat = LinearLayoutCompat(this@Activity)
val usdAmount = AppCompatTextView(this@Activity).apply {
width = ViewGroup.LayoutParams.MATCH_PARENT
gravity = Gravity.CENTER_HORIZONTAL
text = getString(R.string.sampleUsd)
}
val cryptoAmountEditText = AppCompatEditText(this@Activity).apply {
inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL
hint = getString(R.string.field_amount, _getAsset.name)
isSingleLine = true
doAfterTextChanged {
var coins = text.toString()
try {
if (coins.isEmpty()) coins = "0.00"
val cryptoVal = coins.toDouble()
usdAmount.text = getString(
R.string.converted_to_usd,
NumbersUtil.formatFractional(
cryptoVal * _getAsset.metricsDomain.marketDataDomain.priceUsd!!
)
)
} catch (e: Exception) {
usdAmount.text = getString(R.string.invalid_amount, _getAsset.name)
}
}
}
with(linearLayoutCompat) {
orientation = LinearLayoutCompat.VERTICAL
setPadding(30, 0, 30, 0)
addView(cryptoAmountEditText)
addView(usdAmount)
}
alertDialog.setView(linearLayoutCompat)
alertDialog.show()
}
Style XML
<style name="Theme.App" parent="Theme.MaterialComponents.DayNight">
<item name="materialAlertDialogTheme">@style/AppDialogTheme</item>
</style>
<!-- AlertDialog theme on both light and night mode -->
<style name="DialogButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">@color/primaryDarkAccent</item>
</style>
<style name="AppDialogTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="buttonBarPositiveButtonStyle">@style/DialogButtonStyle</item>
<item name="buttonBarNegativeButtonStyle">@style/DialogButtonStyle</item>
</style>


However this only happens on this activity, on my splash screen the height is correct just like a regular and simple AlertDialog.
Android API version: 21
Material Library version: 1.5.0
Device: Samsung J1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
