4

I am making a app in which I have a textview(occupying most of my activity). Textview displays a large amount of text across multiple pages (pages are nothing but the large text assigned into a string array and each array element being displayed on page change trigger). I used the code from below thread to introduce right/left swipe to navigate across pages.

Android: How to handle right to left swipe gestures

Below is the exact code I am using in my all.

public class OnSwipeTouchListener extends Activity implements OnTouchListener{

private final GestureDetector gestureDetector = new GestureDetector(getBaseContext(), new GestureListener());

public boolean onTouch(final View v, final MotionEvent event) {
    //super.onTouch(v, event);
    return gestureDetector.onTouchEvent(event);
}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            } 
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

and then this is how I use this class in my activity

        myTextView.setOnTouchListener(new OnSwipeTouchListener() {
        public void onSwipeRight() {
            displayPreviousPage() //code to display previous page
        }
        public void onSwipeLeft() {
            displayNextPage()  //code to display next page
        }
    });

Everything works fine till now. Now I wanted to enable vertical scrolling for my textview in case my text goes beyond the screen height. Hence I added the below line in my code to do the same.

myTextView.setMovementMethod(new ScrollingMovementMethod());

But that doesn’t seem to work. Any suggestions why? Is it that I have overridden onFling method in my OnSwipeTouchListener class and that setMovementMethod was also using the same(onFling method) for its implementation?

Community
  • 1
  • 1
user1938357
  • 1,466
  • 3
  • 20
  • 33
  • is your TextView dynamic..? – Pragnani Feb 24 '13 at 13:56
  • no, it is static, defined beforehand in xml. I can however make it dynamic if that helps. – user1938357 Feb 25 '13 at 12:09
  • Try instead of adding setMovementMethod, try setting XML attribute android:scrollbars="vertical" – Pragnani Feb 25 '13 at 12:17
  • I need to do that dynamically in my code becuase I am enabling scrolling only once the lines are more than the screenheight. I tried contentTextView.setVerticalScrollBarEnabled as well but did not work. – user1938357 Feb 25 '13 at 20:19
  • I changed my code to use a different approach. Refer post below http://stackoverflow.com/questions/15121003/issue-with-screen-swipe-detection-code/15122827#15122827 Still the same issue:( – user1938357 Feb 27 '13 at 21:52
  • Did you find any solution to this problem? – rudedude Sep 18 '14 at 11:29

0 Answers0