4

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!

Community
  • 1
  • 1
user1548103
  • 869
  • 2
  • 12
  • 25
  • 1
    From a official-design-guidelines perspective, they would both be bad AlertDialog examples. – natario May 14 '15 at 16:19
  • Interesting. The goal I am attempting to accomplish is to present the user with a message, and accept from the user either an affirmative or a negative. I was hoping to be able to use an alertdialog for this, but if that isn't the appropriate use of an alertdialog, do you have a suggestion for what would be the better approach? Any advice would be appreciated! – user1548103 May 14 '15 at 16:20
  • 1
    Then button text should be "Proceed" and "Cancel". They require dialog buttons to be clear and concise. You have a title and a message (don't use if not necessary) to let the user understand what's going on. – natario May 14 '15 at 16:24
  • Ahh, I see. That makes sense. It's a shame, as the designer of the app is requiring the long text on the buttons so I can't get around doing that, but I do understand what you are saying entirely. It makes more sense now why this isn't a more widespread issue and/or not many answers have been presented on it thus far. – user1548103 May 14 '15 at 16:30
  • It's working on https://github.com/Vorlonsoft/AndroidRate – Alexander Savin Nov 08 '17 at 05:27

2 Answers2

6

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.
natario
  • 24,954
  • 17
  • 88
  • 158
  • This pretty much covers my needs! The information here will help me get on the right track to solving the issue. Thank you very much :) – user1548103 May 14 '15 at 19:00
4

I too faced similar issue. I was using android.app.AlertDiaglog. just changed the import to android.support.v7.app.AlertDialog.

Support import did the magic.

Deep P
  • 557
  • 5
  • 12