Android textview autoscroll back and forth - android

I have a textView, which looks like this:
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:scrollHorizontally="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:singleLine="true" />
And this makes my textView scroll if I set it as selected programmatically with textView.isSelected = true.
What I want, though, is for the text view to scroll horizontally to the left and once it reaches the end to scroll back to the right, automatically. And I want this to happen only if the text cannot fit the layout.
Any ideas?
I thought about wrapping it in a horizontal view but still, how do I make it work like this?

Related

TextViews fill the available space but never clip the content

I have (horizontal) scrollview with some textview. I want these textview to fill all the space on the screen and divide the space between them evenly. Sort of like a viewpagerstrip
However, I have realized that in some languages the translation of my strings are longer than the textview and clips the text. Is there a way to make sure the content of a (single line) textview is never clipped.
The remaning textview should just divide the remaning space. If there is not space enough I want them to go outside the screen.
before oncreate
TextView ticker;
in oncreate
ticker = findViewById(R.id.ticker);
ticker.setFocusable(true);
ticker.requestFocus();
ticker.setSelected(true);
here is the xml
<TextView
android:id="#+id/ticker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#fff000"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:fadingEdgeLength="5dp"
android:focusable="false"
android:gravity="bottom"
android:lines="1"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="#string/ticker"
android:textColor="#000000"
android:textColorHighlight="#FFFFFF"
android:textSize="30sp" />

Automatic horizontal scroll in textview with layout_weight

I have been looking at other threads but I couldn't find an answer so here comes my question:
Is it possible to create a automatically horizontal scrolling TextView with a button to the right of it using layout_weight?
"My incredibly long search text here" "The button"
I have tried to make a scrollable textview with "fill_parent" instead of 0dp and layout_weight as well but then the entire text takes up the "row" (obviously since it is fill_parent) and the button is not shown AND the text didn't scroll horizontally even then when I ran it in the android virtual device.
Edit: forgot to write how I tried to make the scrollable textview
<TextView
android:singleLine="true"
android:scrollHorizontally="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/searchResult"
android:focusable="true"
android:focusableInTouchMode="true"/>
Of course it's possible. Something like this will do:
<LinearLayout
android:layout_widht="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0px"
android:layout_weight="7"
.../>
<Button
android:layout_width="0px"
android:layout_weight="3"
... />
</LinearLayout>
Key here is make the width zero so that there's all horizontal space remaining when the weight mechanism assigns all remaining space to linear layout components in relation to their layout weight.

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" />

Smooth horizontal scrolling text

I understand that this might be a duplicate, but I havent found an answer.....yet!
How can I make a smooth horizontal scrolling text in my Android-app? I want the text to scroll like a stockticker on the TV-news. I also need a callback when the scrolling is completed so i can set a new text to scroll.
What is the best way to do this?
Thanks!
Good Question.
First of all to do this, place following code in your layout file for TextView
android:ellipsize="marquee"
android:focusable="false"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
Then write following code in your code file,
TextView txt = (TextView) findViewById(R.id.textView);
txt.setSelected(true);
You must give setSelected(true) to make textview scroll automatically.
Thanks.
For Manually Scrolling
<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 is working"/>
</HorizontalScrollView>
For Automatic Scrolling
<TextView
android:text="Single-line text view that scrolls automatically if the text is too long to fit "
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

Android text layout question: two textviews, side-by-side, with different layout alignments and weights

I'm still a bit of an Android noob, forgive me if this is simple and I'm just not seeing it.
There are two portions of text in a view that spans the entire width horizontally, but is only as high as one line of text. The left side must always be displayed in full, but should take no more horizontal space than it needs. The right side should be pushed over by the left side and fill up the remainder of the screen width. If the right side text is smaller than this width, the text should be right-aligned horizontally. If the text is greater than the width, it should scroll horizontally.
The text on the right side will be updated frequently and should slide up with new text when the app tells it (explaining the TextSwitcher in the layout).
I have tried two different layout styles. In both situations, I can get the left side to "push" the layout, the right side to scroll, but I can't figure out how to get the right side to right align. It is always left aligned. Here is a picture showing what is happening...
http://img10.imageshack.us/img10/5599/androidlayout.png
In addition (but less important), in my layout code I have android:fadingEdge="none" on the TextViews, but it still has a faded edge on the left and right side when it scrolls. Why is that?
Here are the two layouts I created, which yield the results shown, but not the results I want.
Using a horizontal LinearLayout...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayoutStatusBar"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2px"
android:background="#555555"
>
<TextView
android:id="#+id/TextViewTimer"
android:textSize="18px"
android:textColor="#FFFFFF"
android:layout_gravity="left"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0px"
android:layout_marginRight="3px"
android:text="Left Side"
>
</TextView>
<TextSwitcher
android:id="#+id/TextSwitcherDetails"
android:inAnimation="#anim/push_up_in"
android:outAnimation="#anim/push_up_out"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="3px"
android:layout_marginRight="0px"
>
<TextView
android:id="#+id/TextViewDetails1"
android:textSize="18px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:fadingEdge="none"
android:text="Right Side 1"
>
</TextView>
<TextView
android:id="#+id/TextViewDetails2"
android:textSize="18px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:fadingEdge="none"
android:text="Right Side 2 - This is a really long text this is long and fun and fun and long"
>
</TextView>
</TextSwitcher>
</LinearLayout>
So how do I get that text on the right side to right-align. Thanks!
EDIT:
I found the problem... There were a few things wrong. First, I discovered the difference between gravity and layout_gravity. I needed to use gravity to right-align the text.
I can't add any more links, so copy and paste the link below
thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/
However, that didn't give me exactly the layout I wanted, because long lines of text will be right-aligned before scrolling and the front part of the message cannot be read until it loops around. What I needed to do was use three "columns" of views, with the weight on the middle (empty) view to be "1" while the others "0" so that it pushes the right-most text as far as possible to the right, while still maintaining left-alignment.
Next, I discovered that the width of a TextSwitcher is the same as the largest child TextView. This is a problem when switching from a long line to a short line using setText() because the middle column won't push the small text over to the edge. The solution is to use two setText()s. One to cause the fade-out animation, then push a runnable to run after the animation to set the text again to cause the fade-in animation of the new line.
Now my only problem is that when the fade-out animation runs on a long line of text, if it is in mid-scroll, it resets to the X=0 position before fading, so the effect is that it scrolls, jumps to the origin position, then fades out. Anybody know how to fix that?
(I removed the RelativeLayout code above since that was wrong, since this post is really long.)
Here is the working layout code...
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayoutStatusBar"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2px"
android:background="#333333"
>
<TextView
android:id="#+id/TextViewTimer"
android:textSize="18px"
android:textColor="#FFFFFF"
android:layout_gravity="top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginLeft="1px"
android:layout_marginRight="4px"
android:text="Left Side"
>
</TextView>
<View
android:id="#+id/ViewSpacer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
></View>
<TextSwitcher
android:id="#+id/TextSwitcherDetails"
android:inAnimation="#anim/push_up_in"
android:outAnimation="#anim/push_up_out"
android:layout_gravity="top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginRight="1px"
>
<TextView
android:id="#+id/TextViewDetails1"
android:textSize="18px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:fadingEdge="none"
>
</TextView>
<TextView
android:id="#+id/TextViewDetails2"
android:textSize="18px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:fadingEdge="none"
>
</TextView>
</TextSwitcher>
</LinearLayout>
I found the problem! There were a few things wrong. First, I discovered the difference between gravity and layout_gravity. I needed to use gravity to right-align the text.
http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/
However, that didn't give me exactly the layout I wanted, because long lines of text will be right-aligned before scrolling and the front part of the message cannot be read until it loops around. What I needed to do was use three "columns" of views, with the weight on the middle (empty) view to be "1" while the others "0" so that it pushes the right-most text as far as possible to the right, while still maintaining left-alignment.
Next, I discovered that the width of a TextSwitcher is the same as the largest child TextView. This is a problem when switching from a long line to a short line using setText() because the middle column won't push the small text over to the edge. The solution is to use two setText()s. One to cause the fade-out animation, then push a runnable to run after the animation to set the text again to cause the fade-in animation of the new line.
Unfortunately, horizontally scrolling the text looks terrible when the text switches every five to ten seconds. So I'm just letting it run off the screen.

Categories

Resources