Search code examples
androidandroid-animationnativemarquee

How to make News Bar with Spannable Marquee TextView in Android?


I am trying to make a news bar in a textview which be animated with marquee.

I used this view with marqueeRepeatLimit = "marquee_forever"

                    <TextView
                        android:id="@+id/scroll_notifs"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:singleLine="true"
                        android:ellipsize="marquee"
                        android:marqueeRepeatLimit="marquee_forever"
                        android:scrollHorizontally="true"
                        android:padding="10dp"
                        android:gravity="center"
                        android:focusable="true"
                        android:focusableInTouchMode="true"
                        android:background="@color/black_scrollTextView"
                        android:textColor="@color/white"
                        android:textSize="@dimen/text_size_s"
                        android:visibility="gone"
                        android:freezesText="true"/>

and I set in the code :

TextView scroll_notifs = (TextView) view.findViewById(R.id.scroll_notifs); scroll_notifs.setSelected(true);

The textview act like the news bar and animated nicely, the problem now is how to make part of the textview clickable, I used the spannable string like that :

 SpannableString spannableString = new SpannableString("this is the fancy box");

    spannableString.setSpan( new ClickableSpan() {
        @Override
        public void onClick(View view) {
            TextView textView = (TextView) view;
            CharSequence charSequence = textView.getText();
            Log.d("span click",charSequence.toString());
        }
    }, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    scroll_notifs.setText(spannableString);

    scroll_notifs.setMovementMethod(LinkMovementMethod.getInstance());

So the click works but not following the moving span, it just works when I click on the first pixels of the textview, I thinkthat's because the span start at 0 and end on 3.

But when the marquee is repeated, and I start clicking on the span, following it when animated, it does not respond.

So, there is any kind of solution for this or any other idea to make a news bar with separate click listeners.

Best Regards


Solution

  • I think spannable TextView is impossible with animation, I found another way to make a nice news bar :

    http://www.scriptscoop.online/t/e52f7a28abfb/how-can-i-animate-an-android-textview-without-refreshing-it-and-fill-i.html