androidandroid-alertdialogandroid-scroll

Making an android dialog scroll till the end before dismissing it


I have an android alert dialog that I show to user upon first time use. Now the text in this alert dialog is large, so scrolling is necessary.

I would like to make the dialog scroll by itself when the user presses OK button, till he reaches the end of the text.

How to go about this programmatically?

Pseudocode of expected behaviour:

alert.setPositiveButton("Let's Get Started!!",new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
       if(scrolled_till_end)
           dismissDialogue();
       else
           scrollFurther();   // scroll_further to next unviewed part of dialog
    }
});

Solution

    1. first of all you should add a ScrollView into the dialog.
    2. as the native button of the dialog will always dismiss the dialog which you don't want, so you need to add button by yourself too.
    3. when user click the button you added, you can check the scrollY of the ScrollView, if it is not touch the bottom you can use ScrollView#smoothScrollTo() to scroll it to the bottom.

    that's it.

    Edit #1 in the onClickListener maybe something like:

    int textTotalHeight = textView.getHeight();
    int pageHeight = scrollView.getHeight();
    int scrollY = scrollView.getScrollY();
    if(scrollY < textTotalHeight - pageHeight) {// not touch the bottom
        scrollView.smoothScrollTo(0, scrollY + pageHeight);// scroll one page height
    } else {// touch the bottom, dismiss the dialog
        dialog.dismiss();
    }