TextView in HorizontalScrollView not scrolling automatically when text is added - android

I have a TextView that takes in Strings and I want the TextView to scroll to the last text added on the right automatically. The ScrollView works but I have to scroll manually to the last text added when it's out of the TextView's field of view. Please help.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5"
android:fillViewport="true"
android:scrollbars="none">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:scrollHorizontally="true"
android:textColor="#android:color/black"
android:textSize="24sp" />
</HorizontalScrollView>

yourScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT);

I solved this problem through the use EditTextView instead of TextView and disabling focusEnabled by setting keyListener to null in order to achieve the behavior of a normal TextView and also scroll automatically like I wanted.

Related

Prevent auto Line Break in Multi lines TextView

I know this question has been ask several times but maybe not exactly for this requirement.
I want to create a log view, so currently based on a TextView and obviously, like in all log views, in AS's logcat for example, I will have several lines so I can enable vertical scrollbar and I can have long log lines, each line terminated by a \n.
Here I hoped that horizontal scrollbar could help me. But it seems not, right?
If not, how to do?
EDIT my current Textview definition:
<TextView
android:id="#+id/tvLog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="#android:color/darker_gray"
android:ems="10"
android:gravity="fill_horizontal"
android:inputType="none"
android:lines="5"
android:maxLines="20"
android:scrollbars="horizontal|vertical"
android:singleLine="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/elvTests" />
as i have understood that you want all text to be displayed and that you can scroll it downwards so based on that i guess you can do something like this
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/long_text"/>
</ScrollView>
putting your text view in a scroll view that matches its parent in width and height and a text view with wrap_content will enable this behavior hope this helps
EDIT
put this in your code that should do the work
TextView textview= (TextView) findViewById(R.id.your_textview_id);
textview.setMovementMethod(new ScrollingMovementMethod());

Android: Adding Vertical Scroll to TextView

I'm trying to set up a TextView vertical scroll in the XML layout, it barely works, The scroll seems to move when I hardly swipe down the vertical bar, I mostly get to scroll down the layout instead of the textview, it's difficult.
I attached my code below.
<TextView
android:id="#+id/evento_descripcion"
android:layout_width="match_parent"
android:layout_height="150dp"
android:text="#string/t_descripcion"
android:textColor="#010101"
android:layout_below="#+id/evento_fondo"
android:textSize="16sp"
android:layout_marginTop="20dp"
android:gravity="left"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:scrollbars="vertical"
android:fadeScrollbars="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="insideInset"
/>
Try adding this to your code
YOURTEXTVIEW.setMovementMethod(new ScrollingMovementMethod());
Also check this and this out, I think it will help you.

Avoid TextView resizing if text is too long

I have a TextView that when it has a long text, it resizes to make the text fit inside, breaking the UI.
Is there a way to use a XML attribute to make the TextView not resizable?
I was thinking about using the TextView inside a ScrollView, is there other options?
For this to work, set specific width to textview (It will display textview without scroll)
android:layout_width=""
If you want to show scroll, add scrollview with specific size and inside scroll layout, add textview like
<ScrollView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/scrollView"
android:fillViewport="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
100dp is dummy size here.
Solution :
<TextView
android:elipsize="marquee"
</TextView>
check out this Answer for more options :
Elipsize Examples
this will slide the textView
Try this code:
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:singleLine="false"
android:singleLine="false"
android:text="Long multiline text"/>

Horizontally Scrollable TextView show inconsistent behaviour across android versions

I have a text view that has a single lined Arabic (Right-to-Left) text. The Text content increases by time, and the last (left-most) part should always appear. The horizontal scroll bar is initially set to right, and auto scrolls to left as text increases.
The below code works great on a 2.3 Android phone.
This comes from the layout.xml
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="none"
android:fadingEdge="horizontal"
android:gravity="right"
android:scrollHorizontally="true"
android:scrollbars="horizontal"
android:singleLine="true"
android:text="#string/txt_start"
android:textAppearance="?android:attr/textAppearanceLarge" />
I have this in the onCreate() method of my Activity
tv = (TextView) findViewById(R.id.textView1);
tv.setMovementMethod(new ScrollingMovementMethod());
The problem is the inconsistent behaviour of the text view on phones having android 4.0+ . The horizontal scroll bar is initially no placed on the right, so the TextView is initially blanked! If I try to scroll the blank textView, the text shows up. When extra text is added, the scroll-bar is not scrolled to show the new text, but if I manually scrolled the text, it appears.
I searched stackoverflow, and tried to add the below properties with no success.
android:focusable="true"
android:focusableInTouchMode="true"
None of those solutions worked in my case where I wanted to animate scrolling of TextView with android:gravity="right". Simple workaround that helped though is to keep TextView gravity to left but place it aligned to right inside a RelativeLayout container:
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoScrollingTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="left"
android:singleLine="true"
android:ellipsize="none">
</RelativeLayout>
Try this example, it should work:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/myText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/myscroll"
android:ellipsize="marquee"
android:singleLine="true"
android:scrollHorizontally="true"
android:marqueeRepeatLimit="10"
>
</LinearLayout>
Note: when testing the marquee, don't test it in design view inside Eclipse, try it on the emulator\device ..
Also if you want to add a scrolling effect in code:
TextView tView = (TextView) findViewById(R.id.myText);
tView.stSelected(true);
To make the marquee scroll non-stopping on the end of marquee:
android:marqueeRepeatLimit=”marquee_forever”
It's found that the gravity Left/Right are deprecated, a developer should use Start/End. For the text to scroll it needs a "wrap_content" set to its width, additionally the textDirection needs to be set to RTL. For unknown reasons, textDirection did not work programmatically, but worked in the xml.
This code works well on all API versions of android.
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:textDirection="rtl"
android:scrollHorizontally="true"
android:scrollbars="horizontal"
android:singleLine="true"
android:ellipsize="none"
android:text="#string/txt_start"
android:textAppearance="?android:attr/textAppearanceLarge" />

Scroll a text automatically on horizontal

I want to scroll a text automatically in horizontal way. I tried the method below, but the text was not scrolling.
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/bg_1"
android:textColor="#cc0000"
android:scrollHorizontally="true"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
/>
So please tell me how to scroll a text and if there is any need to add some code in the class file also.
your TextView don´t scroll because their with must be fill_parent, and the other thing is that the textview must be selected (not pressed) to scroll.
Also, beside what Franco told you, you have set android:ellipsize="marquee"(that truncate the text to fit on that single line you have set), so probably that mix up with the scrolling you set by android:scrollHorizontally="true"
My suggestion is to try to remove the ellipsize property.
i put a listview in above textview so the textview is not scrolling. so itried this one
set listview focusable is false
listview.setfocusable(false);
then it works the text was scrolling.
Try this....
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="John Jonathan Samuwell Abbruzzi"
android:singleLine="true"
android:scrollHorizontally="true"
android:textSize="50sp" />
</HorizontalScrollView>

Categories

Resources