0

I have a little problem with my first android app:

I made a ScrollView which contains a TextView. The ScrollView should scroll to the end of the TextView (so to the bottom of the ScrollView) everytime I press a button, so the actual added text is in view.

The app is about simple adding strokes (like this -> 'I') to the TextView by pressing a button.

Actually it jumps down the first press (like it should), the second press on top (why?), the third down (like it should), the fourth back on top (why?) and so on..

      @Override
        public void onClick(View v) {

            zaehler += 1;

            textviewStriche.setTextIsSelectable(true);

            if (zaehler % 5 == 0) {
                textviewStriche.setText(striche, TextView.BufferType.SPANNABLE);
                Spannable spannable = (Spannable) textviewStriche.getText();
                spannable.setSpan(STRIKE_THROUGH_SPAN, 0, striche.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                striche += " ";

            } else {
                striche += "I";

                textviewStriche.setText(striche, TextView.BufferType.SPANNABLE);
                Spannable spannable = (Spannable) textviewStriche.getText();

                if (striche.length()>4) {
                    spannable.setSpan(STRIKE_THROUGH_SPAN, 0, ((zaehler / 5)*5-1), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }

            }

            String zaehlerstr = String.valueOf(zaehler);
            textviewZaehler.setText(zaehlerstr);

            scrollview.fullScroll(ScrollView.FOCUS_DOWN);
        }

Already searched for the problem and tried to use

   scrollview.post(new Runnable() {
                public void run() {
                    scrollview.scrollTo(scrollview.getBottom(),scrollview.getBottom());
                }
   });

or

   scrollview.pageScroll(ScrollView.FOCUS_DOWN);

instead of

   scrollview.fullScroll(ScrollView.FOCUS_DOWN);

but it didn't work either.

I think it's caused by the spannable, because without it works correctly, but I couldn't figure out what's the problem, if it's there.

Thank you in advance!

dabo248
  • 3,367
  • 4
  • 27
  • 37

1 Answers1

0
ScrollView scrollview = ((ScrollView) findViewById(R.id.scrollview));
scrollview.post(new Runnable() {
@Override
public void run() {
    scrollview.fullScroll(ScrollView.FOCUS_DOWN);
}

});

This should work, it works fine by me...

Joeri
  • 85
  • 7
  • Thanks, works better now! Bizarrely it jumps now like 1 of 10 times on top, the rest, like it should, on the bottom. – dabo248 Mar 10 '15 at 00:42