0

I've a HorizontalScrollView with a TextSwitcher as child, as shown below.

<HorizontalScrollView
    android:id="@+id/horizontal_scroll_view_equation"
    android:scrollbars="none"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="0dp"
    android:fillViewport="true"
    android:layout_gravity="right">

    <TextSwitcher
        android:id="@+id/text_switcher_equation"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <TextView
            android:layout_marginRight="@dimen/spacing_medium"
            android:layout_marginLeft="@dimen/spacing_medium"
            android:id="@+id/text_view_equation_default"
            android:background="@android:color/transparent"
            android:gravity="center_vertical|right"
            android:maxLines="1"
            android:textSize="@dimen/font_xxxlarge"
            android:scrollHorizontally="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView
            android:layout_marginRight="@dimen/spacing_medium"
            android:layout_marginLeft="@dimen/spacing_medium"
            android:id="@+id/text_view_equation_anim"
            android:background="@android:color/transparent"
            android:gravity="center_vertical|right"
            android:maxLines="1"
            android:textSize="@dimen/font_xxxlarge"
            android:scrollHorizontally="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </TextSwitcher>
</HorizontalScrollView>

The goal of the piece of code above is to create a horizontal scrollable TextView. This code is part of a calculator, so the text of the TextView is dynamic. This means that sometimes the TextView needs to be scrollable, and sometimes not (depending on the length of the text).

The strange behavior that I have now is the following. Once the user enters more and more characters, the TextView becomes perfectly scrollable. However, when the user clears the equation, the HorizontalScrollView keeps collapsed, meaning that it is still scrollable, while the text of the TextView is just one character (a zero). This means that the user can scroll the text to the left and right, while the text is just one character.

In the following, this behavior is demonstrated (see the images below). In the left image, an equation is entered by the user. Then, the user clicks on clear and the equation is cleared, which means that is set to 0. However, as you can see on the right image, the TextView can still be scrolled to the right.

des des des

Another remarkable phenomena is that when the user clicks on clear again (so twice in a row), the scrolling of the TextView is not possible anymore. For some reason this is not the case when the user clicks on reset the very first time, but I do not see why.

What I would like to achieve is that once the text of the TextView is cleared (thus the text of the TextView contains one character), that it is not possible for the user to scroll the text to the left or right.

t.r.hogenkamp
  • 73
  • 2
  • 7

1 Answers1

0

when clear is clicked scroll to the max right ( in case user scrolled to the left ) you can achieve that by

                while(scoll_view.canScrollHorizontally(1)) {
                    scoll_view.scrollBy(50, 0);
                }

now you need to disable scrolling when clear is clicked and re-enable it when user click's another button ( other numbers in your case ) and you do that like that

add this to your class and use intercept to disable and enable the scrolling

class OnTouch implements View.OnTouchListener {
        public boolean intercept = false;
        @Override public boolean onTouch(View v, MotionEvent event) {
            return intercept;
        } 
}

apply the onTouchLisntener to the HorizontalScrollView

    final OnTouch listener = new OnTouch();
    scoll_view.setOnTouchListener(listener);

and when user clear clicked after the while scroll disable scrolling by

            listener.intercept = true;

re-enable scrolling again by doing

            listener.intercept = false;
ahmed nader
  • 578
  • 3
  • 9
  • @t.r.hogenkamp it should work i've tested it, can you include some code?? – ahmed nader Jul 27 '20 at 11:22
  • I am using sliding out and sliding in animation for the TextSwitcher. The problem here is that you clear the text before it is slided out. As a consequence, the slided out animation is not shown, because the text is empty. – t.r.hogenkamp Jul 27 '20 at 11:34