Search code examples
androidlistviewmarquee

Android's TextView with marquee in a ListView won't animate for the fist time it is shown


I've got a ListView with a custom adapter. Every item in it contains a TextView.

<TextView
    android:id="@+id/chat_name"
    android:layout_height="48dp"
    android:layout_width="match_parent"
    android:textColor="#FFFFFF"
    android:gravity="center_vertical"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:singleLine="true"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

I need this TextView to not be focusable, because I rely on the onListItemClick() method. As I learned, onListItemClick() won't fire if you set focusable="true".

In the adapter I set:

TextView chatName = (TextView) v.findViewById(R.id.chat_name);
chatName.setText(chat.getChatName());
chatName.setSelected(true);

And it works every time but first one. When an item is added to the adapter it shows on the listview, but it isn't marqueed. When item is clicked, another activity is displayed. After the user returns to the ListView, the item is suddenly marqueed. I want it marqueed all the time.

I have already tried subclassing the TextView with a method that overwrites isFocused() to always return true, but that didn't help. Behavior remained the same. I also experimented with various values of focusable and focusableInTouchMode attributes, but with no success.


Solution

  • Please use requestFocus() function as follows.

    chatName.setSelected(true);
    chatName.requestFocus();
    

    I will work.