开发者

TextView Marquee not working [duplicate]

开发者 https://www.devze.com 2023-01-08 15:41 出处:网络
This question already has answers here: Marquee text in Android (20 answers) Closed 4 years ago. I have tried to use marquee and its not working
This question already has answers here: Marquee text in Android (20 answers) Closed 4 years ago.

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


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.


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);


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"


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.


 <TextView
  android:ellipsize="marquee"
  android:singleLine="true"
  .../>

must call in code

textView.setSelected(true);


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);


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.


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


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!


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


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)


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.


Use the following line in your code:

TextView.setSelected(true);


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


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


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); 


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);
    } 


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" 

`


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消