TextView Marquee not working [duplicate]
Asked Answered
S

20

162

I have tried to use marquee and its not working here is my code, please let me know where im going wrong

<TextView
   android:text="lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00"
   android:id="@+id/TextView02"
   android:layout_width="200dip"
   android:layout_height="wrap_content"
   android:marqueeRepeatLimit="marquee_forever"
   android:ellipsize="marquee"
   android:singleLine="true"
   android:focusable="true"
   android:inputType="text"
   android:maxLines="1">
</TextView>

i am using android SDK 2.0.1

Silverts answered 26/7, 2010 at 7:19 Comment(3)
its working in Android sdk 1.5Lakes
@ Paresh Have you tested it on 2.0.1?Silverts
May be its late but it may help others.. you have to do it programatically TextView.setSelected(true);Irina
S
327

working now :) Code attached below

<TextView
    android:text="START | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | lunch 20.00 | Dinner 60.00 | Travel 60.00 | Doctor 5000.00 | END"
    android:id="@+id/MarqueeText" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:singleLine="true"
    android:ellipsize="marquee" 
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true" 
    android:paddingLeft="15dip" 
    android:paddingRight="15dip" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:freezesText="true">

Edit (on behalf of Adil Hussain):

textView.setSelected(true) needs to be set in code behind for this to work.

Silverts answered 26/7, 2010 at 10:6 Comment(5)
To add to hooraygradschool's comment: the value of layout_width and layout_height can be whatever you want it to be (doesn't have to be fill_parent), you can specify android:singleLine="true" instead of android:lines="1", and android:scrollHorizontally="true" is not essential (text running fine for me without it).Shena
For me "maxLines=1" was not enough. But "singleLine=true" made it working.Soursop
@Soursop in fact maxLines=1 does nothing - despite being the recommended replacement for the now deprecated singleLine=true.Vas
android:singleLine="true" instead of maxline="1" made it worked for me. but is it possible to have marquee with two lines?Ambrogio
Suppose you have a custom background for the TextView with the selected state. This will always make your selected state background applied.Tuinal
M
99
android:singleLine="true"
android:ellipsize="marquee"

are the only required attributes and scrolling even works with layout_weight defined with layout_width=0dp

here is some sample code:

<TextView 
            android:id="@+id/scroller"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#FFFFFF"
            android:text="Some veryyyyy long text with all the characters that cannot fit in screen, it so sad :( that I will not scroll"
            android:layout_marginLeft="4dp"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

But what is most important is implicitely or explicitely TextView should get selected.

You can do this with:

TextView txtView=(TextView) findViewById(R.id.scroller);
txtView.setSelected(true);
Margemargeaux answered 15/12, 2011 at 8:38 Comment(6)
Instead of setting TextView 'selected' as true programmatically, you can specify "android:focusable" and "android:focusableInTouchMode" as true in the xml layout file.Shena
Just ran into a situation where TextView marquee animation was not working despite setting all the necessary xml attributes (singleLine, ellipsize, marqueeRepeatLimit, focusable, focusableInTouchMode) and calling TextView.setSelected(true) did the trick so big big thank you for pointing that out!!Shena
Thank you, this post is more helpful than the selected answer.Unexpressive
agree with khendricks!Dougall
layout_width could be either fill_parent, match_parent or wrap_content all works I tested it. Required attributes definitely are the ones mentioned in this answer along with the textView.setSelected(true) is a MUST.Loudhailer
how to find some view and setSelected in Preferences? I want to make TextView in PreferencesActivity do marquee :SOfori
T
25

These attributes must be included in the textview tag in order to allow scrolling.

Everything else is optional.

android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="fill_parent"
android:ellipsize="marquee"
Those answered 20/1, 2011 at 1:5 Comment(3)
The attribute android:layout_width="fill_parent" is not essential for text scrolling (though you do need to set the TextView's width). The following two attributes you should consider using: android:singleLine="true" and android:marqueeRepeatLimit="marquee_forever".Shena
Note that sometimes it is necessary to call TextView.setSelected(true) in code (despite setting all the necessary attributes) as per Shardul's answer so give that a try if marquee animation is not working.Shena
android:focusableInTouchMode="true" not requiredEradis
C
24

I faced the same problem and this discussion helped me I just replace this line

android:maxLines="1"

with this line in xml

android:singleLine="true"

and it works the line txtView.setSelected(true); was also in my activity.

Cue answered 17/8, 2012 at 12:26 Comment(3)
Thanks man, you saved my time.Mass
android:singleLine="true" is deprecated.Dachshund
for work with android:maxLines="1" https://mcmap.net/q/88697/-marquee-using-maxlinesExude
H
18
 <TextView
  android:ellipsize="marquee"
  android:singleLine="true"
  .../>

must call in code

textView.setSelected(true);
Huxham answered 19/1, 2017 at 0:45 Comment(2)
android:singleLine isn't neededLucerne
singleLine=true is the key here, if the number of lines is more then it won't work.Bolitho
A
15

I had gone through this situation where textview marquee was not working. However follow this and I am sure it will work. :)

<TextView
         android:id="@+id/tv_marquee"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:ellipsize="marquee"
         android:focusable="true"
         android:focusableInTouchMode="true"
         android:freezesText="true"
         android:maxLines="1"
         android:scrollHorizontally="true"
         android:text="This is a sample code of marquee and it works"/>

and programmatically add these 2 lines...

tvMarquee.setHorizontallyScrolling(true);
tvMarquee.setSelected(true);

tvMarquee.setSelected(true) is required incase if any one of the view is already focused and setSelected will make it work. No need to use.

android:singleLine="true"

it is deprecated and above codes works.

Appellee answered 4/5, 2017 at 8:5 Comment(1)
How to change the speed of scrolling text?Disadvantage
S
14

Very Simple working code:

For infinitely scrolling text

            <TextView
            android:id="@+id/textView_News_HeadLine"
            style="@style/black_extra_large_heading_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="8dp"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="-1"
            android:singleLine="true"
            android:text="HeadLine: Banglawash To be Continued" />

& you should must write from your activity

textView.setSelected(true);
Sheree answered 13/11, 2013 at 4:27 Comment(0)
D
11

You must add the these attributes which is compulsary to marquee

 android:ellipsize="marquee"     
 android:focusable="true"    
 android:focusableInTouchMode="true"     
 android:singleLine="true"     
 android:marqueeRepeatLimit="marquee_forever"     
 android:scrollHorizontally="true"

And also add this line of code in the activity class

textView.setSelected(true)
Discharge answered 3/12, 2015 at 13:20 Comment(0)
N
10

I've encountered the same problem. Amith GC's answer(the first answer checked as accepted) is right, but sometimes textview.setSelected(true); doesn't work when the text view can't get the focus all the time. So, to ensure TextView Marquee working, I had to use a custom TextView.

public class CustomTextView extends TextView {
    public CustomTextView(Context context) {
        super(context);
    }
    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }


    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        if(focused)
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    @Override
    public void onWindowFocusChanged(boolean focused) {
        if(focused)
            super.onWindowFocusChanged(focused);
    }


    @Override
    public boolean isFocused() {
        return true;
    }
}

And then, you can use the custom TextView as the scrolling text view in your layout .xml file like this:

<com.example.myapplication.CustomTextView
            android:id="@+id/tvScrollingMessage"
            android:text="@string/scrolling_message_main_wish_list"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit ="marquee_forever"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:scrollHorizontally="true"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@color/black"
            android:gravity="center"
            android:textColor="@color/white"
            android:textSize="15dp"
            android:freezesText="true"/>

NOTE: in the above code snippet com.example.myapplication is an example package name and should be replaced by your own package name.

Hope this will help you. Cheers!

Nealon answered 16/4, 2016 at 10:51 Comment(2)
This is brilliant!Juvenal
This is the only solution that works everywhere.Tintinnabulation
M
9

Working Code:

<TextView
    android:id="@+id/scroller"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:singleLine="true"
    android:text="Some veryyyyy long text with all the characters that cannot fit in screen, it so sad :( that I will not scroll"
    android:textAppearance="?android:attr/textAppearanceLarge" />
Mariano answered 6/11, 2012 at 7:45 Comment(0)
V
8

I'm working with minSDK=14 and was curious what set of these variations would work. I ended up with:

android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"

in addition to other formatting stuff. I didn't need scrollHoriontally, focusable, or focusableInTouchMode.

This set did require a call to

setSelected(true)

What I find interesting is that singleLine has allegedly been deprecated, with a recommendation to replace it with maxLines = 1. Except - when I do that, that change alone stops the text from scrolling. One would hope that when singleLine eventually bites the dust, that all its current behavior will be triggered by maxLines...

Vas answered 4/5, 2014 at 16:24 Comment(0)
D
5

Just add those as said above:

    android:singleLine="true" 
    android:ellipsize="marquee"
    android:marqueeRepeatLimit ="marquee_forever"

AND!! you must use TextView.setSelected(true) inside your java code.

The reason for marquee not working with some of the guys in this article , If you have an input form with an EditText ( which is an input), The EditText will be the one with focus and selection by default in the form. Now, if you force it thru TextView.setSelected(true), TextView will eventually do marquee no matter what.

So the whole idea is to make the TextView widget focused and selected to make marquee work.

Deafanddumb answered 13/8, 2013 at 13:41 Comment(0)
N
4

Use the following line in your code:

TextView.setSelected(true);
Novokuznetsk answered 4/9, 2013 at 14:1 Comment(0)
C
3
package com.app.relativejavawindow;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.view.Menu;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final RelativeLayout relativeLayout = new RelativeLayout(this);
        final RelativeLayout relativeLayoutbotombar = new RelativeLayout(this);
        textView = new TextView(this);
        textView.setId(1);

        RelativeLayout.LayoutParams relativlayparamter = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);

        RelativeLayout.LayoutParams relativlaybottombar = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        relativeLayoutbotombar.setLayoutParams(relativlaybottombar);


        textView.setText("Simple application that shows how to use marquee, with a long ");
        textView.setEllipsize(TruncateAt.MARQUEE);
        textView.setSelected(true);
        textView.setSingleLine(true);


        relativeLayout.addView(relativeLayoutbotombar);
        relativeLayoutbotombar.addView(textView);
        //relativeLayoutbotombar.setBackgroundColor(Color.BLACK);
        setContentView(relativeLayout, relativlayparamter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

this code work properly but if ur screen size is not fill this text it will not move try to palcing white space end of text

Cartel answered 28/5, 2013 at 4:17 Comment(0)
D
2

Yes, marquee_forever also work in case of fixed width for TextView. (e.g. android:layout_width="120dp")

Must required attributes are:

  1. android:focusable="true"
  2. android:focusableInTouchMode="true"
  3. android:singleLine="true" // if it's missing text appear in multiple line.

Working code:

<TextView
                android:id="@+id/mediaTitleTV"
                android:layout_width="220dp"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:marqueeRepeatLimit="marquee_forever"
                android:singleLine="true"
                android:text="Try Marquee, it works with fixed size textview smoothly!" />
Divergency answered 1/6, 2013 at 20:18 Comment(1)
android:scrollHorizontally="true" is required tooHenrique
I
2

I have created a custom class AlwaysMarqueTextView

public class AlwaysMarqueeTextView extends TextView
{
    protected boolean a;

    public AlwaysMarqueeTextView(Context context)
    {
        super(context);
        a = false;
    }

    public AlwaysMarqueeTextView(Context context, AttributeSet attributeset)
    {
        super(context, attributeset);
        a = false;
    }

    public AlwaysMarqueeTextView(Context context, AttributeSet attributeset, int i)
    {
        super(context, attributeset, i);
        a = false;
    }

    public boolean isFocused()
    {
        return a || super.isFocused();
    }

    public void setAlwaysMarquee(boolean flag)
    {
        setSelected(flag);
        setSingleLine(flag);
        if(flag)
        setEllipsize(TruncateAt.MARQUEE);
        a = flag;
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) 
    {
        if(focused)

            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    @Override
    public void onWindowFocusChanged(boolean focused)
    {
        if(focused)
            super.onWindowFocusChanged(focused);
    }
}

And you can startMarquee when desire.. like

//textView.setSelected(true); No need of Selection..
textview.setAlwaysMarquee(true); 
Irina answered 2/3, 2015 at 9:8 Comment(2)
I'm trying to implement this but the text isn't marquee'ing. I've added it as a class, using it instead of the TextView, set the various standard marquee parameters on it and then called setAlwaysMarquee.Marvamarve
Figured it out. However there is a slight pause each time it cycles. Any idea how to make it constantly move round rather than have that slight pause.Marvamarve
R
2

To have your own scroll speed and flexibility to customize marquee properties, use the following:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal"
    android:lines="1"
    android:id="@+id/myTextView"
    android:padding="4dp"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="Simple application that shows how to use marquee, with a long text" />

Within your activity:

private void setTranslation() {
        TranslateAnimation tanim = new TranslateAnimation(
                TranslateAnimation.ABSOLUTE, 1.0f * screenWidth,
                TranslateAnimation.ABSOLUTE, -1.0f * screenWidth,
                TranslateAnimation.ABSOLUTE, 0.0f,
                TranslateAnimation.ABSOLUTE, 0.0f);
        tanim.setDuration(1000);
        tanim.setInterpolator(new LinearInterpolator());
        tanim.setRepeatCount(Animation.INFINITE);
        tanim.setRepeatMode(Animation.ABSOLUTE);

        textView.startAnimation(tanim);
    } 
Ranchod answered 21/3, 2017 at 8:20 Comment(0)
E
1

Just put these parameters in your TextView. It works :)

    android:singleLine="true" 
    android:ellipsize="marquee"
    android:marqueeRepeatLimit ="marquee_forever"
    android:scrollHorizontally="true"
    android:focusable="true"
    android:focusableInTouchMode="true" 

`

Edmead answered 8/8, 2013 at 11:43 Comment(0)
G
0

android:focusable="true" and android:focusableInTouchMode="true" are essential....

Because I tested all others without these lines and the result was no marquee. When I add these it started to marquee..

Giess answered 28/11, 2014 at 7:48 Comment(0)
R
0

Most of the answers are identical,
but also notice that in some cases marquee will not work without specifying of width in dips for the container.
For instance if you use weight in parent container

android:layout_width="0dp"
android:layout_weight="0.5"

marquee may not work.

Ribbonfish answered 17/8, 2016 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.