32

I'm working on a calculator. I noticed that in the default android calc you can scroll the textview horizontally. I looked up the documentation and found out about the attribute android:scrollHorizontally but after adding it to the textview I still cannot do horizontal scroll, there is no further info about it on the documentation leading me to think that only adding the attr should suffice. This is the calculator's textview:

    <TextView android:id="@+id/edit_text"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight=".8"
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:gravity="center|right"
        android:text="0" />

When characters exceed the textview width the string is trimmed and ... appear at it's end. What am I doing wrong?

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410
  • 1
    remove the android:layout_weight=".8"and set the android:layout_width="300dp", i mean more that your screen width and check – Narendra Pal Dec 31 '12 at 04:40
  • http://stackoverflow.com/questions/5472362/android-automatic-horizontally-scrolling-textview Is a good asnwer to me – Jay Jan 15 '17 at 02:39

4 Answers4

50
<HorizontalScrollView android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:scrollHorizontally="true"
        android:text="Horizontal scroll view will work now"/>

</HorizontalScrollView>

This is how you can make textview scroll horizontally.

Nirav Tukadiya
  • 3,367
  • 1
  • 18
  • 36
  • 2
    Thanks, I just tried the code. The only problem is that the text stays aligned to the left even when adding gravity: center|right to the textview. Do you know how to get the text to the right? thanks – lisovaccaro Dec 31 '12 at 19:23
  • 1
    A similar question to this has a answer here http://stackoverflow.com/questions/23950081/scroll-text-in-a-horizontal-scroll-view For runtime scrolling just add override the run method of the scroll view to shift the focus of your view to right – Vijay Mar 21 '15 at 16:33
  • 3
    This works for me after several other suggestions didn't. What I don't understand is why we need the HorizontalScrollView AND the "scrollHorizontally" in the text view... It's right - I've verified it and that's the only way I can make it work - but I don't understand it. – Mark Smith May 08 '15 at 20:34
  • 1
    @MarkSmith couldnt agree more. one would have thought `android:scrollHorizontally="true"` on a `TextView` might have been enough or even perhaps `android:scrollbars="horizontal"` but nooooo you had to be Quaid! – wal Nov 01 '16 at 04:14
  • Answer works perfectly! For those who want to remove the scrollbar add: `android:scrollbars="none"` to your HorizontalScrollView xml – I'm_With_Stupid May 31 '17 at 21:03
  • 1
    This breaks with AutoSizeableTextView, is there a way to use both? (reduce size down to `app:autoSizeMinTextSize`, the use horizontal scrolling). – EmmanuelMess Sep 22 '17 at 23:17
  • It works but now TextView click event has not worked. Added `android:clickable="true"` but still not worked. – M123 May 18 '22 at 07:53
12

Just use this

   textview.setMovementMethod(new ScrollingMovementMethod());
   textview.setHorizontallyScrolling(true);
NITIN DUDA
  • 192
  • 1
  • 14
9

I'm a bit late but I managed to achieve same result without adding the HorizontalScrollView

EditText extends TextView to support scrolling and selection. So, you can use the EditText as a TextView (touch, focus and cursor are disabled).

<EditText
    android:id="@+id/edit_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent" --> This remove that line at the bottom
    android:clickable="false" --> It can be true if you need to handle click events
    android:cursorVisible="false" --> Hide the cursor
    android:focusable="false" --> Disable focus
    android:singleLine="true"
    android:text="This is a very long text that won't be possible to display in a single line"/>

I'm just not able to test in a wide range of devices... I'm just sharing because it may be useful to someone else.

guipivoto
  • 18,327
  • 9
  • 60
  • 75
  • 1
    Big thanks, can't get it to work with TextView but with your solution and editText. Had big headache with HorizontalScrollView and TextView inside, because I had the TextView in a ListView and OnClickItem Listener don't work this way – BR75 Jul 16 '19 at 08:37
  • `TextView` never worked for me even after tinkering with it for hours. This is the only solution that worked. Also check if you have a `FrameLayout` or something that makes the layouts below cover the scrolling layout above. – Tashila Pathum Aug 23 '21 at 10:14
2

Probably a late answer but it is possible to make a TextView scroll both ways. There is no need for the scrollHorizontally property to be set in the XML or the code.

The following code makes a single-line or a multi-line TextView scroll both vertically and horizontally based on the text content.

<HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:scrollbars="none">

        <ScrollView
            android:id="@+id/scroll_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:scrollbars="none">

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/text_content"
                fontPath="fonts/roboto_medium.ttf"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/primary_text"
                android:textSize="14sp" />

        </ScrollView>
</HorizontalScrollView>

Note the layout_width for the ScrollView and TextView are set to wrap_content. The Width for the HorizontalScrollView can either be wrap_content or match_parent and has no effect.

Ram Iyer
  • 1,621
  • 1
  • 23
  • 25