Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal scroll on textview on android?

Tags:

java

android

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?

like image 874
lisovaccaro Avatar asked Dec 31 '12 04:12

lisovaccaro


3 Answers

<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.

like image 90
Nirav Tukadiya Avatar answered Nov 18 '22 00:11

Nirav Tukadiya


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.

like image 33
W0rmH0le Avatar answered Nov 18 '22 01:11

W0rmH0le


Just use this

   textview.setMovementMethod(new ScrollingMovementMethod());
   textview.setHorizontallyScrolling(true);
like image 10
NITIN DUDA Avatar answered Nov 17 '22 23:11

NITIN DUDA