Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog Buttons not wrapping in Lollipop

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 4.4

Android L:

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!

like image 314
user1548103 Avatar asked Dec 24 '22 19:12

user1548103


1 Answers

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:

  • Forget about 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.
  • Use this great library, that will give your dialogs a fresh design on older devices and has a convenient method called forceStacking(), as you can read. It's very easy to implement.
like image 139
natario Avatar answered Jan 02 '23 09:01

natario