35

I have an issue with the Dialog.Builder, where the Buttons are cut off. How can I resolve this or is this an issue for Motorola devices?

  • making the text shorter is not a solution
  • I expect the same behaviour like the S5-screenshot, Buttons too long -> Buttons below each other

Device: Motorola Moto G / OS: Android 5.0.2 enter image description here

Device: Galaxy S5 / OS: Android 5.0.2 enter image description here

Here's the code and theme for showing the Dialog

public void showDialog(final String title, final String message,
                       final OnClickListener onClickPositive,
                       final OnClickListener onCLickNegative, final String positiveButton,
                       final String negativeButton, final boolean cancelable) {
    if (!isFinishing()) {
        runOnUiThread(new Runnable() {

            @Override
            public void run() {

                if (dialog != null && dialog.isShowing()) {
                    dialog.cancel();
                }

                Builder builder;
                if (android.os.Build.VERSION.SDK_INT >= 14) {
                    builder = new AlertDialog.Builder(new ContextThemeWrapper(
                            MyActivity.this,
                            android.R.style.Theme_DeviceDefault_Light_Dialog));
                } else {
                    builder = new Builder(MyActivity.this);
                }

                if (title != null) {
                    builder.setTitle(title);
                }
                if (message != null) {
                    builder.setMessage(message);
                }

                if (positiveButton != null) {
                    builder.setPositiveButton(positiveButton, onClickPositive);
                }
                if (negativeButton != null) {
                    builder.setNegativeButton(negativeButton, onCLickNegative);
                }
                builder.setCancelable(cancelable);

                dialog = builder.show();
                colorizeDialog(dialog);
            }
        });
    }
}

//theme-xml
<style name="Theme.DeviceDefault.Light.Dialog" parent="Theme.Holo.Light.Dialog" >
    <item name="android:windowTitleStyle">@android:style/DialogWindowTitle.DeviceDefault.Light</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.DeviceDefault.Dialog</item>

    <item name="android:buttonBarStyle">@android:style/DeviceDefault.Light.ButtonBar.AlertDialog</item>
    <item name="borderlessButtonStyle">@android:style/Widget.DeviceDefault.Light.Button.Borderless.Small</item>

    <item name="textAppearance">@android:style/TextAppearance.DeviceDefault.Light</item>
    <item name="textAppearanceInverse">@android:style/TextAppearance.DeviceDefault.Light.Inverse</item>
</style>

########################

UPDATE EDIT

Seems like, the behaviour is not the same on every device. We have a second issue, with adding the "neutral" Button. Again, Galaxy S5 adding buttons below each other (from top to bottom: positiv, neutral, negative)

enter image description here

Motorola Moto G (API 5.0.2 / left side) shows neutral Button in the middle (red "Abbrechen") and cuts again the button text (blue arrow).

Nexus 4 (API 4.3 / right side) shows the neutral Button at the left side, instead of in the middle

Seems like we have to implement an custom dialog....

longi
  • 11,104
  • 10
  • 55
  • 89
  • I think the problem is that text length exceeds window length. You can't put a text longer that container dimension. – Giuseppe Jul 20 '15 at 13:42
  • @Giuseppe: Yes, the text is too long. The problem is, why does Galaxy S5 handles this right (Buttons shown below each other), while this expected behaviour doesn't work on Motorola?! – longi Jul 20 '15 at 13:54
  • ah ok, you did not specify this. I thought it was your alternative to resolve this problem. Maybe the problem is on your listener and the machine build the code wrong! What are onClickPositive and onClickNegative? – Giuseppe Jul 21 '15 at 07:36
  • @Giuseppe which listener might be the problem? (fyi: onClickNegative = "final OnClickListener onCLickNegative") – longi Jul 21 '15 at 13:00
  • 1
    try using your own layouts, otherwise you are at the mercy of the system's layouts for these dialogs. screen dimensions and dpi are some of the factors that will cause the "same" layout to appear differently on different devices. – escape-llc Jul 21 '15 at 14:41
  • 1
    This is not answering your most important question, but the order of buttons has already been changed at least once in the past, see [this discussion](https://code.google.com/p/android/issues/detail?id=24138), so I wouldn't be surprised, if they changed them again in 4.3. – Nudelsuppe Aug 03 '15 at 07:38

2 Answers2

1

Have you tried using Dialog instead?

final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");

// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");

// make 3 buttons instead of one
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    //do something
    }
});

dialog.show();

recommendation: use linear layout for the dialog.

Francisco
  • 254
  • 3
  • 9
0

I know this is an old question ... but in my case it helped using the android.support.v7.app.AlertDialog instead of the "normal" one.

Robert
  • 353
  • 3
  • 12