2

I'm trying to create an AlertDialog to show an introduction message in my application, with a "Don't show this again" CheckBox below it.

It works well when the message of the AlertDialog is short, but when is too long (requiring scrolling) the CheckBox is no longer shown. It gets "pushed out" by the TextView.

The XML for the custom view (dont_show_again.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <CheckBox 
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Don't show this again">
    </CheckBox>
</LinearLayout>

And the code to display the AlertDialog

String longMessage = getString(R.string.long_message);

LayoutInflater inflater = getLayoutInflater();
final View checkboxLayout = inflater.inflate(R.layout.dont_show_again, null);

AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("Some message")
.setMessage(longMessage)
.setView(checkboxLayout)
.setPositiveButton("Ok",
        new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
});

AlertDialog dialog = builder.create();
dialog.show();

Any ideas on how to solve this? Perhaps someone has an example of a working AlertDialog with a "Don't show this again" CheckBox?

Martin
  • 31
  • 4

2 Answers2

4

Don't set the dialog box message, instead include it in the layout XML as textView.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">

<TextView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"    
    android:singleLine="false"
    android:maxLines="3"
    android:scrollbars="vertical"/>
<CheckBox 
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Don't show this again">
</CheckBox> 
</LinearLayout>
halfer
  • 19,824
  • 17
  • 99
  • 186
Ron
  • 24,175
  • 8
  • 56
  • 97
  • Thanks! This works, but the problem is that you need to scroll down to see the CheckBox. Ideally the CheckBox should have a fixed position below the (scrollable) message. – Martin Aug 13 '11 at 11:42
  • Remove the scroll view from root. Make the Textview scrollable. I have edited the above answer. – Ron Aug 13 '11 at 12:48
0

Use the XML layout from the answer earlier.

String longMessage = getString(R.string.long_message);
LayoutInflater inflater = getLayoutInflater();
final View checkboxLayout = inflater.inflate(R.layout.dont_show_again, null);
CheckBox cb = (CheckBox)checkboxLayout.findViewById(R.id.checkBox);
TextView tv = (TextView)checkboxLayout.findViewById(R.id.message);
tv.setText(longMessage);
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("Some message")
.setView(checkboxLayout)
.setPositiveButton("Ok",
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    }
});
AlertDialog dialog = builder.create();
dialog.show();
Tsunaze
  • 3,204
  • 7
  • 44
  • 81