开发者

Marquee text in Android

开发者_如何学GoHow can I use marquee text in an android application?


Here is an example:

public class TextViewMarquee extends Activity {
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) this.findViewById(R.id.mywidget);  
        tv.setSelected(true);  // Set focus to the textview
    }
}

The xml file with the textview:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/mywidget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:textColor="#ff4500"
        android:text="Simple application that shows how to use marquee, with a long text" />
</RelativeLayout>


TextView textView = (TextView) this.findViewById(R.id.textview_marquee);  
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setText("General Information... general information... General Information");
textView.setSelected(true);
textView.setSingleLine(true);


android:ellipsize="marquee"

This only works when your TextView has the focus.


Just put these params to your TextView - It works :D

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

And you also need to setSelected(true):

my_TextView.setSelected(true);

Greetings, Christopher


To get this to work, I had to use ALL three of the things (ellipsize, selected, and singleLine) mentioned already:

TextView tv = (TextView)findViewById(R.id.someTextView);
tv.setSelected(true);
tv.setEllipsize(TruncateAt.MARQUEE);
tv.setSingleLine(true):


Xml code

 <TextView
            android:id="@+id/txtTicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:freezesText="true"
            android:gravity="center_horizontal"
            android:marqueeRepeatLimit="marquee_forever"
            android:paddingLeft="5dip"
            android:paddingRight="5dip"
            android:scrollHorizontally="true"
            android:shadowColor="#FF0000"
            android:shadowDx="1.5"
            android:shadowDy="1.3"
            android:shadowRadius="1.6"
            android:singleLine="true"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:textStyle="bold" >
        </TextView>

Java

txtEventName.setSelected(true);

if text is small then add space before and after text

txtEventName.setText("\t \t \t \t \t \t"+eventName+"\t \t \t \t \t \t");


I found a Problem with marquee that it can't be used for short strings(As its function is only to Show Long strings).

I suggest Use Webview If you want to move short strings horizontally. Main_Activity.java Code:`

        WebView webView;

        webView = (WebView)findViewById(R.id.web);


        String summary = "<html><FONT color='#fdb728' FACE='courier'><marquee behavior='scroll' direction='left' scrollamount=10>"
                + "Hello Droid" + "</marquee></FONT></html>";

        webView.loadData(summary, "text/html", "utf-8"); // Set focus to the textview
`

main_activity.xml code:

<WebView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/web"
        ></WebView>


In XML file, you need to add following additional attributes in a TextView to get a marquee like feature:

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

In MainActivity.java file, you can get the reference of this TextView by using findViewById() and you can set the following property to this TextView to make it appear like a marquee text:

setSelected(true);

That's all you need.


As well as the XML settings identified by droidgren, my tests have shown that if the text you want to display is shorter than the width of the textview, then the marquee won't scroll at all. Possible solutions are to set the width of the view to a size smaller than the length of the text, or to concatenate the string to itself 2 or 3 times, with perhaps appropriate whitespace in-between so that the scrolling looks ok.


I have tried all of the above, but for me its didn't work. When I add

android:clickable="true"

then it's worked perfectly for me. I don't know why. But I am happy to work it.

Here is my full answer.

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"

android:clickable="true"


Add Below Code in XML

    <TextView
    android:text="Shops NearBy textdf fsdgsdgsdg dsgtsgsdgsdgsg"
    android:id="@+id/txtEventName"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit ="marquee_forever"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:scrollHorizontally="true"
    android:singleLine="true"/>

In Java add following Code:

    TextView txtEventName=(TextView)findViewById(R.id.txtEventName);
    txtEventName.setSelected(true);


Lots of answers correctly state that calling textView.setSelected(true) is required. However, if preferred to do so just via XML, one option could be to make use of the Binding Adapters when working with Data Binding.

So, just create a new adapter similar to:

@BindingAdapter("app:autoStartMarquee")
fun setAutoStartMarquee(textView: TextView, autoStartMarquee: Boolean) {
    textView.isSelected = autoStartMarquee
}

Then, you can simply use it in the XML as follows:

...
<TextView
    ...
    android:ellipsize="marquee"
    android:singleLine="true"
    app:autoStartMarquee="@{true}"/>
...

No need to call it from code anymore.


Use this to set Marque:

    final TextView tx = (TextView) findViewById(R.id.textView1);
    tx.setEllipsize(TruncateAt.MARQUEE);
    tx.setSelected(true);
    tx.setSingleLine(true);
    tx.setText("Marquee needs only three things to make it run and these three things are mentioned above.");

You do not need to use "android:marqueeRepeatLimit="marquee_forever" into xml file. Marquee will work even without this.


You can use a TextView or your custom TextView. The latter is when the textview cannot get focus all the time.

First, you can use a TextView or a 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.

Then in case of using CustomTextView, you should define the CustomTextView class:

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

Hope it will be helpful to you. Cheers!


This will be equivalent to "end":

where = TruncateAt.END


With the above answer, you cannot set the speed or have flexibility for customizing the text view functionality. 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);//set the duration
        tanim.setInterpolator(new LinearInterpolator());
        tanim.setRepeatCount(Animation.INFINITE);
        tanim.setRepeatMode(Animation.ABSOLUTE);

        textView.startAnimation(tanim);
    } 


This is my xml customTextView Object here you can use simply TextView to replace on Tag.

 <com.wedoapps.crickethisabkitab.utils.view.montserrat.CustomTextView
                android:id="@+id/lblRateUsPlayStore"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/_10sdp"
                android:layout_marginBottom="@dimen/_10sdp"
                android:layout_marginStart="@dimen/_5sdp"
                android:layout_marginEnd="@dimen/_5sdp"
                android:text="@string/please_rate_us_5_star_on_play_store"
                android:textAllCaps="false"
                android:textColor="@color/green"
                android:textSize="@dimen/_25ssp"
                android:textStyle="bold"
                android:visibility="visible"
                android:linksClickable="true"
                android:autoLink="web|phone"/>

And here is My Java File code. i have set my html text on server just replace your text on textview object. i have put this code is marquee tag with clickable if any links on this textview to open mobile or webBrowser.

CustomTextView lblRateUsPlayStore = findViewById(R.id.lblRateUsPlayStore);
lblRateUsPlayStore.setMovementMethod(LinkMovementMethod.getInstance());
                        lblRateUsPlayStore.setText( Html.fromHtml(documentSnapshot.getString("DisplayText")));
                        TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(lblRateUsPlayStore, 12, 20, 2, 1);

                        lblRateUsPlayStore.setEllipsize(TextUtils.TruncateAt.MARQUEE);

                        // Set marquee repeat limit (unlimited)
                        lblRateUsPlayStore.setMarqueeRepeatLimit(-1);
                        lblRateUsPlayStore.setHorizontallyScrolling(true);
                        lblRateUsPlayStore.setSelected(true);
                        lblRateUsPlayStore.setLinksClickable(true);
                        lblRateUsPlayStore.setFocusableInTouchMode(true);
                        lblRateUsPlayStore.setFocusable(true);


You can use

android:ellipsize="marquee"

with your textview.

But remember to put focus on the desired textview.


For setting Marquee programatically

TextView textView = (TextView) this.findViewById(R.id.textview_marquee);  
textView.setEllipsize(TruncateAt.MARQUEE);
textView.setMarqueeRepeatLimit(-1);
textView.setText("General Information... general information... General Information");
textView.setSelected(true);
textView.setSingleLine(true);


Special Thanks to David Roman , i changed his answer to kotlin

import android.content.Context
import android.graphics.Rect
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView

class MarqueeTextView : AppCompatTextView {
constructor(context: Context?) : super(context!!) {}
constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {}
constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(
    context!!,
    attrs,
    defStyle
) {
}

override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
    if (focused) super.onFocusChanged(focused, direction, previouslyFocusedRect)
}

override fun onWindowFocusChanged(focused: Boolean) {
    if (focused) super.onWindowFocusChanged(focused)
}

override fun isFocused(): Boolean {
    return true
}
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜