BIBEKRBARAL
BIBEKRBARAL

Reputation: 4475

Marquee text in Android

How can I use marquee text in an android application?

Upvotes: 80

Views: 127203

Answers (21)

Arezou_ghorbani
Arezou_ghorbani

Reputation: 19

In Android (KOTLIN), the Marquee effect for scrolling text can be achieved by using the TextViewwidget and setting the android:ellipsize and android:marqueeRepeatLimit attributes in the layout XML file. Here's an example of how to create a marquee TextView in Kotlin:

  1. In your layout XML file (e.g. activity_main.xml), add a TextView with the desired text and marquee attributes:

    <TextView
    android:id="@+id/marqueeTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:text="This is a marquee text view example."
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:scrollHorizontally="true"/>
    

In this example, we set android:singleLine to true to ensure the text scrolls horizontally on a single line. The android:ellipsize attribute is set to "marquee" to enable the marquee effect. The android:marqueeRepeatLimit attribute is set to "marquee_forever" to make the marquee repeat indefinitely. The android:text attribute specifies the initial text to display.

Note: To make the marquee work, the TextView needs to have focus. Therefore, we set android:focusable and android:focusableInTouchMode to true to allow the TextView to gain focus.

  1. In your Kotlin code, you can optionally add the following lines to enable the marquee effect programmatically:

val marqueeTextView = findViewById<TextView>(R.id.marqueeTextView) marqueeTextView.isSelected = true

By setting isSelected to true, the TextView will start scrolling immediately when the view gains focus.

Make sure to replace R.id.marqueeTextView with the actual ID of your TextView in your code. Adjust the layout attributes and text content according to your desired appearance and behavior.

Upvotes: -1

Mahdi Safarmohammadloo
Mahdi Safarmohammadloo

Reputation: 563

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

Upvotes: 0

Akshay Upadhyay
Akshay Upadhyay

Reputation: 109

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>

Upvotes: 8

droidgren
droidgren

Reputation: 7118

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>

Upvotes: 149

Sourabh Tejraj
Sourabh Tejraj

Reputation: 61

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

Upvotes: 0

Rohit Lalwani
Rohit Lalwani

Reputation: 579

You can use

android:ellipsize="marquee"

with your textview.

But remember to put focus on the desired textview.

Upvotes: 0

Sarquella
Sarquella

Reputation: 1139

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.

Upvotes: 2

Gaurav meghanathi
Gaurav meghanathi

Reputation: 49

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

Upvotes: 0

Akshay Chopra
Akshay Chopra

Reputation: 1253

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.

Upvotes: 6

Jaydev
Jaydev

Reputation: 1812

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

Upvotes: 1

Nikhil
Nikhil

Reputation: 107

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

Upvotes: 2

David Roman
David Roman

Reputation: 431

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!

Upvotes: 1

Christopher Stock
Christopher Stock

Reputation: 1435

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

Upvotes: 19

Ashish Anand
Ashish Anand

Reputation: 3581

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

Upvotes: 25

Dhiraj Aggarwal
Dhiraj Aggarwal

Reputation: 118

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.

Upvotes: 1

Md Sufi Khan
Md Sufi Khan

Reputation: 1771

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"

Upvotes: 2

PrvN
PrvN

Reputation: 2395

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

Upvotes: 10

Avinash
Avinash

Reputation: 1

This will be equivalent to "end":

where = TruncateAt.END

Upvotes: 0

BRG
BRG

Reputation: 101

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

Upvotes: 10

Maurits Rijk
Maurits Rijk

Reputation: 9985

android:ellipsize="marquee"

This only works when your TextView has the focus.

Upvotes: 25

John J Smith
John J Smith

Reputation: 11931

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.

Upvotes: 2

Related Questions