2

I am using a few scrolling textViews in my mobile app but i recently noticed that there is some lag when the text starts scrolling. Any ideas on why this is happening? I have two different fragments using the same code but the scrolling textViews are not "lagging" on the other one. I am trying to figure out what the issue is but i can't find it. Any help is welcome!

layout.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/card_view_bg"
    tools:layout_editor_absoluteY="25dp">

<ImageView
    android:id="@+id/cardArtImageView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    app:layout_constraintBottom_toTopOf="@+id/cardDetailsImageView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<!--
<ImageView
    android:id="@+id/cardDetailsImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:cropToPadding="false"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@drawable/card_details_box" /> -->

<!-- Implement scrolling text
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
 -->

<!-- passive skill desc
<TextView
    android:id="@+id/passiveSkillDesc"
    android:layout_width="395dp"
    android:layout_height="33dp"
    android:fontFamily="monospace"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="13sp"
    android:textStyle="italic"
    android:visibility="invisible"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="602dp" />
    -->

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="225dp"
    android:background="@drawable/card_details_closed">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/HP"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="37dp"
            android:layout_weight="1"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="0000" />

        <TextView
            android:id="@+id/ATT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginStart="78dp"
            android:layout_toEndOf="@+id/HP"
            android:layout_weight="1"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="0000" />

        <TextView
            android:id="@+id/DEF"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="46dp"
            android:layout_weight="1"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="0000" />

    </RelativeLayout>

    <TextView
        android:id="@+id/leaderSkillDesc"
        android:layout_width="250dp"
        android:layout_height="15dp"
        android:layout_above="@+id/superAttackTitle"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="14dp"
        android:layout_marginEnd="35dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@color/white"
        android:textSize="13sp"
        android:textStyle="italic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintVertical_bias="0.626" />

    <TextView
        android:id="@+id/superAttackTitle"
        android:layout_width="250dp"
        android:layout_height="15dp"
        android:layout_above="@+id/superAttackDesc"
        android:layout_alignStart="@+id/superAttackDesc"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@android:color/holo_blue_light"
        android:textSize="12sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/superAttackDesc"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/superAttackDesc"
        android:layout_width="255dp"
        android:layout_height="15dp"
        android:layout_alignEnd="@+id/leaderSkillDesc"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="73dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@android:color/white"
        android:textSize="13sp"
        android:textStyle="italic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</RelativeLayout>

<!-- passive skill title
<TextView
    android:id="@+id/passiveSkillTitle"
    android:layout_width="189dp"
    android:layout_height="30dp"
    android:fontFamily="monospace"
    android:textAlignment="viewStart"
    android:textColor="@color/cardDetailsTextColor"
    android:textSize="12sp"
    android:visibility="invisible"
    tools:layout_editor_absoluteX="207dp"
    tools:layout_editor_absoluteY="543dp" />
    -->

</LinearLayout>

UPDATE This lag actually occurs in the 2nd fragment too when i turn my phones resolution to 4K (s8)!

Stelios Papamichail
  • 955
  • 2
  • 19
  • 57

2 Answers2

1

You may have an issue with

android:layout_weight="1"

Setting weights is bad for performance, as stated here

Nested weights are bad for performance because:

Layout weights require a widget to be measured twice. When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially

However, things have evolved since then, and google has introduced a new layout: ConstraintLayout. I strongly encourage you to use it instead: ConstraintLayout

AdricoM
  • 579
  • 4
  • 11
  • hi, i'll try and improve my layout with lint and the layout inspector(+remove weight attributes). The constraint layout doesn't work for me on this project sadly – Stelios Papamichail Jan 15 '18 at 02:46
  • There is also [PercentRelativeLayout](https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html), but I didn't mention it earlier because it has been deprecated recently – AdricoM Jan 15 '18 at 07:50
  • No problem, your answer was right. After removing the weight attribute the performance improved greatly! Thanks a bunch – Stelios Papamichail Jan 15 '18 at 14:53
0

You have to load the large text efficiently by turning the string into an html-string

    private void someMethod(){
yourTextView.setText(Html.fromHTML(escape(someText)));
yourTextView.setMovementMethod(new ScrollingMovementMethod());}

    public static String escape(String s) {
        StringBuilder builder = new StringBuilder();
        boolean previousWasASpace = false;
        for( char c : s.toCharArray() ) {
            if( c == ' ' ) {
                if( previousWasASpace ) {
                    builder.append("&nbsp;");
                    previousWasASpace = false;
                    continue;
                }
                previousWasASpace = true;
            } else {
                previousWasASpace = false;
            }
            switch(c) {
                case '<': builder.append("&lt;"); break;
                case '>': builder.append("&gt;"); break;
                case '&': builder.append("&amp;"); break;
                case '"': builder.append("&quot;"); break;
                case '\n': builder.append("<br>"); break;
                // We need Tab support here, because we print StackTraces as HTML
                case '\t': builder.append("&nbsp; &nbsp; &nbsp;"); break;  
                default:
                    if( c < 128 ) {
                        builder.append(c);
                    } else {
                        builder.append("&#").append((int)c).append(";");
                    }    
            }
        }
        return builder.toString();
    }

Seconly, have to get ride of your RelativeLayouts as its an expensive parent

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/card_view_bg"
    tools:layout_editor_absoluteY="25dp">

<ImageView
    android:id="@+id/cardArtImageView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    app:layout_constraintBottom_toTopOf="@+id/cardDetailsImageView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<!--
<ImageView
    android:id="@+id/cardDetailsImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:cropToPadding="false"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@drawable/card_details_box" /> -->

<!-- Implement scrolling text
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
 -->

<!-- passive skill desc
<TextView
    android:id="@+id/passiveSkillDesc"
    android:layout_width="395dp"
    android:layout_height="33dp"
    android:fontFamily="monospace"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="13sp"
    android:textStyle="italic"
    android:visibility="invisible"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="602dp" />
    -->

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="225dp"
    android:background="@drawable/card_details_closed">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
<LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
android:orientation="vertical"> 
        <TextView
            android:id="@+id/HP"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="37dp"
            android:layout_weight="1"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="0000" />

        <TextView
            android:id="@+id/ATT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginStart="78dp"
            android:layout_weight="1"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="0000" />

 </LinearLayout>
        <TextView
            android:id="@+id/DEF"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginEnd="46dp"
            android:layout_weight="1"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="0000" />

    </FrameLayout>
<LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="bottom">

    <TextView
        android:id="@+id/leaderSkillDesc"
        android:layout_width="250dp"
        android:layout_height="15dp"
        android:layout_above="@+id/superAttackTitle"
        android:layout_marginBottom="14dp"
        android:layout_marginEnd="35dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@color/white"
        android:textSize="13sp"
        android:textStyle="italic"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintVertical_bias="0.626" />

    <TextView
        android:id="@+id/superAttackTitle"
        android:layout_width="250dp"
        android:layout_height="15dp"
        android:layout_above="@+id/superAttackDesc"
        android:layout_alignStart="@+id/superAttackDesc"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@android:color/holo_blue_light"
        android:textSize="12sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/superAttackDesc"
        android:layout_width="255dp"
        android:layout_height="15dp"
        android:layout_marginBottom="73dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@android:color/white"
        android:textSize="13sp"
        android:textStyle="italic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

 </LinearLayout>
</FrameLayout>

<!-- passive skill title
<TextView
    android:id="@+id/passiveSkillTitle"
    android:layout_width="189dp"
    android:layout_height="30dp"
    android:fontFamily="monospace"
    android:textAlignment="viewStart"
    android:textColor="@color/cardDetailsTextColor"
    android:textSize="12sp"
    android:visibility="invisible"
    tools:layout_editor_absoluteX="207dp"
    tools:layout_editor_absoluteY="543dp" />
    -->

</LinearLayout>

Lastly if you are logging the text, please remove all calls to log

Niza Siwale
  • 2,390
  • 1
  • 18
  • 20