Home » Android » android – Setting of TextView stops marquee scrolling of other TextView

android – Setting of TextView stops marquee scrolling of other TextView

Posted by: admin June 15, 2020 Leave a comment

Questions:

This was asked elsewhere, but the solution did not work for me. So posing it again with more context. The issue is that an activity contains a scrolling music title text view which is disrupted by an updating elapsed time counter text view.

I have these two TextView widgets in my activity layout (although they are encompassed by other layout containers).

<TextView android:id="@+id/v_current_time"
   android:minWidth="26dp"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center_vertical"
   android:gravity="right|center_vertical"
   android:singleLine="true"
   android:textSize="12sp"
   android:enabled="false"
  />

<TextView android:id="@+id/v_track_title"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textSize="18sp"
   android:textStyle="normal"
   android:singleLine="true"
   android:ellipsize="marquee"
   android:marqueeRepeatLimit="marquee_forever"
   android:scrollHorizontally="true"
   android:focusable="true"
   android:focusableInTouchMode="true"
   android:enabled="true"
/>

The music title is dynamically set to (in the test case) a long line of text. If I never update the text for the current time with the following line, the music title will happily scroll forever no matter how I interact with my pause and play buttons.

tvCurrentTime.setText(DataFormat.formatTime(progress));

But if I do set the text for the current time, the music title will stop. Pressing my pause button somehow kicks scrolling back into gear, but pressing play will cause the current time to update and stop it again.

Per the suggestion in this thread, I attempted to couple the setting of the time text with re-enabling of the scrolling title as follows:

tvCurrentTime.setText(DataFormat.formatTime(progress));
tvTitle.setEnabled(true);

This has no effect on the failure other than to reset the scroller each time it restarts.

Is there some detail that I am missing, or any other thoughts as to what could be going wrong? Thanks a lot!

How to&Answers:

Marquee is problematic. When TextView (or the Window containing the TextView)loses focus the marquee is stopped and reset (see the sources for TextView).
I guess you have 3 possible solutions here:

  1. You can set android:focusable="false" in all other Views in your layout. That way your TextView shouldn’t lose focus. But that’s probably not the solution you want.
  2. You can subclass TextView and change onFocusChanged() and onWindowFocusChanged() to prevent marquee from being stopped and reset.
  3. Create your own implementation of marquee.

Answer:

There is another way to solve this without removing all RelativeLayout.

You can simply wrap the marquee TextView with a LinearLayout inside the RelativeLayout as a container.

Answer:

(Promoting comment above to an answer) Turns out that the TextView XML configs above work fine without any runtime changes (to reset enabled=true or whatever) IF I get rid of the RelativeLayout’s in my layout file and replace them with LinearLayout’s. And neither suggestion 1 or 2 above (not sure about 3) works if I don’t. That seems like a subtle and bogus undocumented failure of RelativeLayout.

Answer:

In java code, doing tvTitle.setSelected(true); (here, tvTitle is your sliding TextView variable) worked for me. Doing this, seems to make it focused again. So worked like a charm.