Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - issue with auto scrolling in TextView

I have an Activity which serves as a music player. When it starts, a MediaPlayer object gets initialized and started. In the layout, I have TextViews to display the artist and the title. Those values (retrieved from the server) can be long so I added

   android:ellipsize="marquee"
   android:marqueeRepeatLimit="marquee_forever"
   android:scrollHorizontally="true"

to make the text scroll. There is a strange issue: when I press the "pause" button, the TextViews are scrolling, but while the music is playing they don't scroll. Any ideas?

P.S. I update the SeekBar every 500ms using a Runnable, could the issue be related to this?

Here's my layout:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center_vertical"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/main_header"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:orientation="horizontal" >
</LinearLayout>

<ImageView
    android:id="@+id/imgCoverArt"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_below="@+id/main_header"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="15dp"
    android:layout_marginTop="20dp"
    android:src="@drawable/cover_default" />

    <SeekBar
    android:id="@+id/seekBarMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imgCoverArt"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp"
    android:paddingLeft="9dp"
    android:paddingRight="9dp"
    android:progressDrawable="@drawable/pb"
    android:thumb="@drawable/pbhead" />

<TextView
    android:id="@+id/tvArtist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/seekBarMain"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="35dp"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#FFFFFF" />

<TextView
    android:id="@+id/tvTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvArtist"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#FFFFFF" />



<TextView
    android:id="@+id/tvTimeElapsed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/seekBarMain"
    android:layout_alignLeft="@+id/seekBarMain"
    android:layout_marginBottom="-10dp"
    android:text="02:54"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#FFFFFF" />

<TextView
    android:id="@+id/tvTotalTime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/seekBarMain"
    android:layout_alignRight="@+id/seekBarMain"
    android:layout_marginBottom="-10dp"
    android:text="05:45"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#FFFFFF" />

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="10dp"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btnPrevious"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="6dp"
        android:background="@drawable/prev_btn_state" />

    <Button
        android:id="@+id/btnPlay"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center_vertical"
        android:background="@drawable/button_play" />

    <Button
        android:id="@+id/btnNext"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="3dp"
        android:background="@drawable/next_btn_state" />
</LinearLayout>

</RelativeLayout>
like image 526
Droidman Avatar asked Mar 11 '13 18:03

Droidman


1 Answers

<TextView
        android:text="Single-line text view that scrolls automatically if the text is too long to fit in the widget" 
        android:singleLine="true" 
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true" 
        android:scrollHorizontally="true"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>

add this your TextView,

            android:focusable="true"
            android:focusableInTouchMode="true" 

is needed to scroll automatically.

More example are there in GitHub.

https://github.com/FireZenk/FireZenk-s-TickerText

https://github.com/kaeppler/ignition/blob/master/ignition-core/ignition-core-lib/src/com/github/ignition/core/widgets/ScrollingTextView.java

Hope this will help you.

like image 143
Amit Gupta Avatar answered Oct 20 '22 10:10

Amit Gupta