3

I'm having an issue with an AlertDialog I built programmatically: Its message shows up correctly in my emulator running 4.1.2. But on my Device (a Huawei U8510 running 2.3.7) the message does not automatically wrap.

Is there anything I can add to the code so the AlertDialog will allow its message to wrap? I obviously don't want to add manual line breaks. (The thing is, even when I add manual line breaks, the Dialog will only show the first line)

I guess I'm just missing something here, but I couldn't find an answer anywhere – they all refer to manually adding line breaks.

Here's the relevant code (Yes, I'm using hard-coded stings for now):

AlertDialog.Builder builder= new AlertDialog.Builder( activity );
builder.setPositiveButton( ...
builder.setNegativeButton( ...
builder.setTitle( "Wirklich löschen?" );
builder.setMessage( "Soll das Item \"" + deleteItem + "\" wirklich gelöscht werden?" );
AlertDialog dialog= builder.create();
dialog.show();

And here are the screenshots. You can see how the message wraps correctly on the Emulator (large image) but not on the device.

Device (2.3.7)

Emulator (4.1.2)

Felizett
  • 328
  • 2
  • 10

3 Answers3

3

I am not sure what is the problem or whether it is device specific or what but I can suggest a work around for this situation..you can create a custom layout with a textview and two buttons at the bottom and attach that layout to this dialog. Now you can handle your textview as you want...

Vikram Singh
  • 1,420
  • 14
  • 19
1

I recently encountered this problem in a project that was moved from Android Studio back to Eclipse.

In this situation the problem was a minSdkVersion was not defined. If you do not declare this attribute, the system assumes a default value of "1", which indicates that your application is compatible with all versions of Android.

Evidently API Level 1 (which is even earlier than Cupcake 1.5) did not wrap AlertDialog message text.

The problem was solved by adding

<uses-sdk android:minSdkVersion="16" />

to the Android Manifest. An minSdkVersion of 16 (Jelly Bean) is what was required for this application. I don't know when AlertDialog started wrapping message text, but earlier API levels e.g. 10 (Gingerbread) might also work.

On a separate note, others have found using Theme.Holo on earlier versions of Android can cause this problem, e.g. Android TextView's stopped wrapping text

Community
  • 1
  • 1
beyeriii
  • 301
  • 3
  • 6
0

If you want a consistent look and feel across all Android versions, you should consider using a custom layout.

In this case a simple TextView should do the job. You can style, align and modify the text appearance to match whatever you need.

custom_dialog.xml:

<TextView
    android:id="@+id/custom_dialog_message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/white"
    android:textSize="20sp" />

Then use the following method to apply it to your dialog:

dialog.setContentView(R.layout.custom_dialog);
jenzz
  • 7,239
  • 6
  • 49
  • 69