2

I want to have a TextView with the marquee effect in my RemoteView

I have this layout XML describing the layout of my RemoteView (R.layout.notification_loading_small):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="64dip"
              android:background="@color/primary_background_inverted">

    <ProgressBar android:indeterminate="true"
                 android:layout_width="64dip"
                 android:layout_height="64dip"
                 android:id="@+id/marker_progress" style="?android:attr/progressBarStyle"
                 android:layout_gravity="center_vertical|center_horizontal"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:orientation="vertical"
        android:padding="@dimen/padding_small"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/notification_small_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textColor="@color/primary_textcolor_inverted"
            android:textSize="@dimen/text_size_large"
            android:text="@string/loading"/>

        <TextView
            android:id="@+id/notification_small_textview2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textColor="@color/secondary_textcolor_inverted"
            android:textSize="@dimen/text_size_small"
            android:text="@string/loading_advice"/>
    </LinearLayout>

</LinearLayout>

And a method in my Java like this:

public void createTestNotification() {
        mSmallLoadingNotificationView = new RemoteViews(getPackageName(), R.layout.notification_loading_small);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                PlayerService.this)
                .setSmallIcon(R.drawable.ic_notification).setContentTitle(APP_NAME)
                .setOngoing(true).setPriority(NotificationCompat.PRIORITY_MAX)
                .setContent(mSmallLoadingNotificationView);

        mNotification = builder.build();

        startForeground(PLAYERSERVICE_NOTIFICATION_ID, mNotification);
}

I want to make the text in id/notification_small_textview2 to marquee horizontally, but it seems like an impossible task so far...

According to the Internet I need to add android:ellipsize="marquee", android:marqueeRepeatLimit="marquee_forever", android:lines="1" and android:singleLine="true" in my TextView. It also seems that we need to call the method setSelected(true) from the TextView in Java. However I cannot find a way to do this in that method... What am I missing?

How should I modify my Java code in order for the marquee to work?

PedroD
  • 5,670
  • 12
  • 46
  • 84

2 Answers2

3

I think the marquee way is going to work only if the text display length is going to be bigger than the width of the TextView, and since you set it up as match_parent, it needs to be a really long text.

Also, you need to set the focusable properties. I tried it with the following:

<TextView
    android:text="Some string here that is wider than your screen"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:focusable="true"
    android:focusableInTouchMode="true"/>

If your string is not long enough, the first thing that comes to mind is animating the TextView left and right. Or maybe implementing your custom TextView that draws text with an offset.

N.T.
  • 2,601
  • 1
  • 14
  • 20
  • I can assure that the string in the TeztView is larger than the width. However I put it there using Java instead of using XML. I will try with the focusable. – PedroD Aug 04 '15 at 09:47
  • Does not work, even with android:layout_width="match_parent", I think the issue here is the RemoteView context. – PedroD Aug 04 '15 at 10:09
  • I miss the old times where would do the trick with no fuss. – PedroD Aug 04 '15 at 10:13
  • 1
    yes, I think you're right about `RemoteViews`. Check out these links and see if anything there works: [http://stackoverflow.com/questions/12990405/custom-notification-marquee-text-doesnt-work-android](http://stackoverflow.com/questions/12990405/custom-notification-marquee-text-doesnt-work-android) and [http://stackoverflow.com/questions/13765150/why-is-marquee-not-working-in-widget](http://stackoverflow.com/questions/13765150/why-is-marquee-not-working-in-widget) – N.T. Aug 04 '15 at 12:34
0

I am not sure of what you can do with notifications, but if the problem is related to the TextView you may be able to use a WebView instead with some HTML.

https://stackoverflow.com/a/17007724/3545278

Community
  • 1
  • 1
leb1755
  • 1,386
  • 2
  • 14
  • 29