Search code examples
androidscrollhorizontal-scrollinghorizontalscrollviewautoscroll

HorizontalScrollView cut the text in a TextView and autoscorlling


I have a ListView and I want that when I click on a ListView item, the bottom TextView change its value and if the text is too large it's autoscrolling.

1) HorizontalScrollView cuts off the text, the original text is "Enrique Iglesias - Bailando ft. Descemer Bueno, Gente De Zona", but there's no space so I see only "Enrique Iglesias - Bailando ft.". Now, I want that with scrolling I see also " Descemer Bueno, Gente De Zona", but there is not... the TextView value is correct, there is the full name, I see with logs...

What I see:

enter image description here

What I see clicking on the TextView:

enter image description here

2) Autoscrolling not working, I have to click on the TextView for scrolling.

This is my code layout:

    titleSongPlayngTextView = (TextView) rootView.findViewById(R.id.song_title);
    titleSongPlayngTextView.setMovementMethod(new ScrollingMovementMethod());

    final HorizontalScrollView s = (HorizontalScrollView)
    rootView.findViewById(R.id.title_scroller);
    s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);

This is my code layout (putting TextView in HorizontalScrollView tag not works scrolling, also with a click):

    <LinearLayout
        android:id="@+id/info_playing_song"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.82"
        android:orientation="vertical">

        <HorizontalScrollView
            android:id="@+id/title_scroller"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

        </HorizontalScrollView >

        <TextView
            android:id="@+id/song_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:foregroundGravity="left"
            android:text="Title Song"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit ="marquee_forever"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:scrollHorizontally="true"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="?android:attr/textColorPrimary" />

UPDATE:

In my fragment I have:

public void setMediaButtons(View rootView) {
    albumSongPlayngImageView = (ImageView) rootView.findViewById(R.id.album_song);

    titleSongPlayngTextView = (TextView) rootView.findViewById(R.id.song_title);
    titleSongPlayngTextView.setMovementMethod(new ScrollingMovementMethod());
    titleSongPlayngTextView.setText(firstSong.getTitle());
    titleSongPlayngTextView.setSelected(true);
    Log.d(LOG_TAG, "After scroll clicked song: " + titleSongPlayngTextView.getText());
        //*************** HERE the TextView value is correct***************

    artistSongPlayngTextView = (TextView) rootView.findViewById(R.id.song_name_artist);
    artistSongPlayngTextView.setMovementMethod(new ScrollingMovementMethod());
    artistSongPlayngTextView.setSelected(true);
    artistSongPlayngTextView.setText(firstSong.getArtist());

In MusicService I have:

public void playSong() {
    mediaPlayer = musicPlayer.getMediaPlayer();
    mediaPlayer.setOnPreparedListener(this);

    mediaPlayer.reset();
    Song currPlaySong = arrayOfSongs.get(currentSongPosition);
    long currSong = currPlaySong.getID();
    Uri trackUri = ContentUris.withAppendedId(
            android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            currSong);

    //set the data source
    try {
        mediaPlayer.setDataSource(getApplicationContext(), trackUri);

        songTitle = currPlaySong.getTitle();
        titleSongPlayngTextView.setText(songTitle);

        Log.d(LOG_TAG, "TextView value: " + titleSongPlayngTextView.getText());
        //*************** HERE the TextView value is correct***************

        songArtist = currPlaySong.getArtist();
        artistSongPlayngTextView.setText(songArtist);


    } catch (Exception e) {
        Log.e("MUSIC SERVICE", "Error setting data source", e);
    }
    mediaPlayer.prepareAsync();
}

And finally this is my modified layout (maybe it there the problem? Another problem is that if TextView value si too large I get a very small ImageView):

<?xml version="1.0" encoding="utf-8"?>
<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">

    <ListView
        android:id="@+id/list_songs"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/fragment_button_player" />

    <LinearLayout
        android:id="@+id/fragment_button_player"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/darker_gray"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:weightSum="1">

        <ImageView
            android:id="@+id/album_song"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.21"
            android:src="@drawable/ic_album_black_24dp" />

        <LinearLayout
            android:id="@+id/info_playing_song"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.82"
            android:orientation="vertical">


            <TextView
                android:id="@+id/song_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxLines="1"
                android:foregroundGravity="left"
                android:text="Title Song"
                android:singleLine="true"
                android:ellipsize="marquee"
                android:marqueeRepeatLimit ="marquee_forever"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:scrollHorizontally="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="?android:attr/textColorPrimary" />

                <TextView
                    android:id="@+id/song_name_artist"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:ellipsize="marquee"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:marqueeRepeatLimit="marquee_forever"
                    android:maxLines="1"
                    android:scrollHorizontally="true"
                    android:singleLine="true"
                    android:text="Artist Name"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

        </LinearLayout>

        <ImageButton
            android:id="@+id/prev_song"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:foregroundGravity="center_vertical|bottom|right"
            android:padding="8dp"
            android:src="@drawable/ic_skip_previous_black_24dp"
            android:tintMode="src_in" />

        <ImageButton
            android:id="@+id/play_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:foregroundGravity="center_vertical|bottom|right"
            android:padding="8dp"
            android:src="@drawable/ic_play_arrow_black_24dp"
            android:tintMode="src_in" />

        <ImageButton
            android:id="@+id/next_song"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:foregroundGravity="center_vertical|bottom|right"
            android:padding="8dp"
            android:src="@drawable/ic_skip_next_black_24dp"
            android:tintMode="src_in" />

    </LinearLayout>

</RelativeLayout>

Here is the screen with imageView problema, only in the first image it has the real dimension, all other are scaled for TextView value too large. I want always the same dim, the real dim, like in first image.

SOLUTION for textview value cut off see here: Scrolling TextView value are cut off

enter image description here


Solution

  • By default, the marquee will only scroll onfocus. So if you setSelected as true, it will always be focused and hence scroll.

    You need to setSelected to true on your textview, so the text will scroll.

    titleSongPlayngTextView.setSelected(true);
    

    Also, there's no point having the scrollview without the textview in it. You do not need to use a scrollview, but if you do, put the textview in it.

    As for it cutting off the text, I would need to see your code, as you have the player controls over the textview.

    edit
    after more info

    The sum of your weights is greater than your weightsum of 1.

    You first two child elements are greater than 1 and you have not included your image buttons in this.

    You can not use weightsum, in which case the weightsum will be the total of all the weights. Otherwise ensure your weights add up to the to your weight sum. Also an oversight on my part, set your layout widths to 0dp when using layout weight.

    <LinearLayout
        android:id="@+id/fragment_button_player"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:weightSum="1">
        <ImageView
            android:id="@+id/album_song"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.21"
            android:src="@drawable/ic_album_black_24dp" />
        <LinearLayout
            android:id="@+id/info_playing_song"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.82"
            android:orientation="vertical">
            <TextView
                android:id="@+id/song_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            <TextView
                android:id="@+id/song_name_artist"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
        </LinearLayout>
    
        // These below are not included in your weightSum
    
        <ImageButton
            android:id="@+id/prev_song"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
        <ImageButton
            android:id="@+id/play_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
        <ImageButton
            android:id="@+id/next_song"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"            
    </LinearLayout>
    

    So you would have something like this:

        <ImageView
            android:id="@+id/album_song"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_weight="0.2"
            android:foregroundGravity="center_vertical|bottom|left"
            android:src="@drawable/ic_album_black_24dp"/>
    
        <LinearLayout
            android:id="@+id/info_playing_song"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/song_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:foregroundGravity="left"
                android:marqueeRepeatLimit="marquee_forever"
                android:maxLines="1"
                android:scrollHorizontally="true"
                android:singleLine="true"
                android:text="Title Song and too much information to display in one line."
                android:textAppearance="?android:attr/textAppearanceMedium"/>
    
            <TextView
                android:id="@+id/song_name_artist"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:marqueeRepeatLimit="marquee_forever"
                android:maxLines="1"
                android:scrollHorizontally="true"
                android:singleLine="true"
                android:text="Artist Name and too much information to read in one line."
                android:textAppearance="?android:attr/textAppearanceSmall" />
    
        </LinearLayout>
    
        <ImageButton
            android:id="@+id/prev_song"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_weight="0.1"
            android:foregroundGravity="center_vertical|bottom|right"
            android:padding="8dp"
            android:src="@drawable/ic_skip_previous_black_24dp"
            android:tintMode="src_in"/>
    
        <ImageButton
            android:id="@+id/play_pause"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_weight="0.1"
            android:foregroundGravity="center_vertical|bottom|right"
            android:padding="8dp"
            android:src="@drawable/ic_play_arrow_black_24dp"
            android:tintMode="src_in"/>
    
        <ImageButton
            android:id="@+id/next_song"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_weight="0.1"
            android:foregroundGravity="center_vertical|bottom|right"
            android:padding="8dp"
            android:src="@drawable/ic_skip_next_black_24dp"
            android:tintMode="src_in"/>
    
    </LinearLayout>
    

    I've tested this and it works. You can jiggle with your weights to suit your needs.