1

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
    }
});
Reaz Murshed
  • 21,071
  • 12
  • 69
  • 87
SoulRayder
  • 4,699
  • 5
  • 39
  • 86

2 Answers2

1

you can do this work in two way:

1: disable submit button and force user to scroll the scrollbar to enable it...then after that user scrolled to the end of goal scrollbar enable the submit button and allow user to use that button.

2: Yourself should scroll the scrollbar programmatically to end of it and after that finished the dialog box,you can scroll your scrollbar with scrollbar.smoothScrollTo(x,y) method. like this :

private void scrollFurther()
{
     ScrollView sv= (ScrollView) findViewById(R.id.scv_main);
     sv.smoothScrollTo(sv.getBottom());
     dismissDialogue();
}
Morteza
  • 51
  • 2
  • 10
  • Second option would be the one i am looking for.... but how would i track the *scrolled_till_end* flag as described above? – SoulRayder Feb 05 '17 at 06:39
  • I thing you can find your answer in this link : http://stackoverflow.com/a/10334353/3137633 – Morteza Feb 05 '17 at 06:48
1
  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();
}
AssIstne
  • 416
  • 4
  • 12
  • Please provide code for this...and moreover i would like to scroll to the next unviewed section rather than directly to the ver bottom..like in a license agreement – SoulRayder Feb 05 '17 at 07:22
  • Thanks @AssIstne . I had already done the first part. I had to modify this code to take the relative layout instead of the textview since my dialog has a layout of multiple textviews. Also, *after* the dialog is created and shown we need to *override* the onclicklistener behaviour by taking reference to the button directly to finally make this work, as in this [answer](http://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked) by eric. Thanks for the core logic though :) – SoulRayder Feb 05 '17 at 13:14