I'm a relatively new Android developer and have run into a problem that I don't really know how to correct and was hoping you guys might be able to advise!
In Kitkat and below, when I create an AlertDialog that has 2 buttons, the text of both buttons would wrap if they extend beyond the length of the button. However, in L, the text refuses to fit.
Android 4.4:
Android L:
I found some other examples of people having this issue and looking for a fix, so I started poking through the solutions that were presented for them
Alert dialog buttons problems in Android L
https://github.com/hotchemi/Android-Rate/issues/40
Based on the answers, the conclusion appeared to be a custom style for the alertdialog.
I created a values-v21 folder, and in it had a styles.xml. It looks like the following, from the above:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Material.Light">
<item name="android:alertDialogTheme">@style/CustomAlertDialogStyle</item>
</style>
<style name="CustomAlertDialogStyle" parent="android:Theme.Material.Light.Dialog.Alert">
<item name="android:buttonBarButtonStyle">@style/CustomButtonBarButtonStyle</item>
<item name="android:buttonBarStyle">@style/CustomButtonBarStyle</item>
</style>
<style name="CustomButtonBarStyle" parent="@android:style/Widget.Material.Light.ButtonBar.AlertDialog">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:height">@null</item>
<item name="android:minHeight">@null</item>
</style>
<style name="CustomButtonBarButtonStyle" parent="@android:style/Widget.Material.Light.Button.Borderless.Colored">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
</style>
</resources>
Unfortunately, I saw no visible change at all in the lollipop Alertdialog, and I am still not quite familiar enough with the the layout values to see what might be wrong.
So after poking around for a while, I thought I would ask: Does anyone know how to go about making the Android L button text word-wrap similar to the Android 4.4?
Thanks!
As said, new design guidelines clearly state that dialogs should have clear and concise texts, to let the user take a fast decision. Having long long actions is thus discouraged.
However, from Lollipop onward, a new design pattern has been allowed to accomodate larger texts. Take a look here and scroll down to Stacked full-width buttons. Basically you can have your buttons in a stack, and use the full dialog width. It doesn't solve your problem, in that you are still limited to the full width, but could help.
As for how to do it, I think the AlertDialog
class does not provide easy methods in that direction. A few suggestions:
positive
and negative
buttons, and just set a custom view for a dialog with no buttons at all. In that view you can have title, message and all the buttons you wish.forceStacking()
, as you can read. It's very easy to implement.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With