Reputation: 33946
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?
Upvotes: 32
Views: 34670
Reputation: 11
HorizontalScrollView
is not need. just do this.
Add this in your XML.
<TextView
android:id="@+id/textview"
android:layout_width="310dp"
android:layout_height="38dp"
android:gravity="center_vertical"
android:singleLine="true"
android:scrollbars="horizontal"
android:padding="5dp"
android:textColor="@color/black"
android:textSize="18dp" />
In .java file
import android.text.method.ScrollingMovementMethod; //package
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textview = findViewById(R.id.textview);
textview.setMovementMethod(new ScrollingMovementMethod());
}
Upvotes: 1
Reputation: 202
Just use this
textview.setMovementMethod(new ScrollingMovementMethod());
textview.setHorizontallyScrolling(true);
Upvotes: 13
Reputation: 1679
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.
Upvotes: 2
Reputation: 18677
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.
Upvotes: 10
Reputation: 3417
<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.
Upvotes: 50