Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontally Scrolling Text Performance Issues

My app contains a few textViews which are supposed to be scrolling horizontally. This works when my layout is first loaded but after clicking on a button to load another layout and then re-click on that button to load back the first layout those textViews start scrolling after a "big" delay(like 20 secs+). I'm trying to figure out the source of this problem with the Layout Inspector, Hierarchy Viewer etc. but no luck.

UPDATE: I noticed something strange today.When i go from the 1st layout to the 2nd one and vice versa, although the TextViews stop scrolling, if i close and re-open the phone's screen, the scrolling works like a charm. This seems totally strange to me, do you know what's causing this and why?

CardViewActivity.java:

public class CardViewActivity extends AppCompatActivity {

private ImageView cardArtImageView;
private TextView leaderSkillDescText;
private TextView superAttackTitleText;
private TextView superAttackDescText;
private TextView passiveSkillTitleText;
private TextView passiveSkillDescText;
private TextView hpText;
private TextView attText;
private TextView defText;
private TextView costText;
private Button arrowButton;
private int selectedItemPosition;
private boolean isBtnClicked = false;

// Listener member field for each layout's button. This listener will be used recursively
private View.OnClickListener arrowButtonListener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        // When the arrowButton is clicked, choose the right layout based on the button's state
        int resID = isBtnClicked ? R.layout.cardview_refined : R.layout.cardview_expand_details;

        setContentView(resID);

        // If we're in the first layout, initialize the cardArtImageView field
        if(isBtnClicked) {
            cardArtImageView = findViewById(R.id.cardArtImageView);
        }

        viewDefinitions(!isBtnClicked);
        initCardViewData(selectedItemPosition);
        setSelectedViewsInit();

        // Set the arrowButton's listener to this listener (recursively)
        arrowButton.setOnClickListener(arrowButtonListener);

        // toggle our flag field
        isBtnClicked = !isBtnClicked;
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cardview_refined);

    // Retrieving the data sent over from MainActivity
    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();

    if (bundle != null) {
        selectedItemPosition = bundle.getInt("Card Index");
    }
    //Toast.makeText(this, "WIDTH: " + SCREEN_WIDTH, Toast.LENGTH_SHORT).show();

    // Initializing our views
    cardArtImageView = findViewById(R.id.cardArtImageView);
    viewDefinitions(false);
    setSelectedViewsInit();

    initCardViewData(selectedItemPosition);

    arrowButton.setOnClickListener(arrowButtonListener);
}

/**
 * Sets the required textViews as selected to allow automatic scrolling
 */
private void setSelectedViewsInit() {
    leaderSkillDescText.setSelected(true);
    superAttackTitleText.setSelected(true);
    superAttackDescText.setSelected(true);
    if (passiveSkillTitleText != null && passiveSkillDescText != null) {
        passiveSkillTitleText.setSelected(true);
        passiveSkillDescText.setSelected(true);
    }
}

/**
 * Adds the views's definitions
 *
 * @param initPassiveInfo used to decide whether or not the passiveSkillDesc & ..Title != null
 *                        so that they can be defined
 */
private void viewDefinitions(boolean initPassiveInfo) {
    leaderSkillDescText = findViewById(R.id.leaderSkillDesc);
    superAttackTitleText = findViewById(R.id.superAttackTitle);
    superAttackDescText = findViewById(R.id.superAttackDesc);
    if (initPassiveInfo) {
        passiveSkillTitleText = findViewById(R.id.passiveSkillTitle);
        passiveSkillDescText = findViewById(R.id.passiveSkillDesc);
    } else {
        Log.d("Definitions", "Passive info == null");
    }
    hpText = findViewById(R.id.HP);
    attText = findViewById(R.id.ATT);
    defText = findViewById(R.id.DEF);
    costText = findViewById(R.id.COST);
    arrowButton = findViewById(R.id.arrowButton);
}

/**
 * Initialize the cardViewActivity's views with the data from the CardInfoDatabase.java class
 *
 * @param selectedItemPosition Used to initialize this activity's views if the intent was called from the MainScreen Fragment
 */
private void initCardViewData(int selectedItemPosition) {
    if (cardArtImageView != null) {
        cardArtImageView.setImageResource(CardInfoDatabase.cardArts[selectedItemPosition]);
    }
    leaderSkillDescText.setText(CardInfoDatabase.leaderSkills[selectedItemPosition]);
    superAttackTitleText.setText(CardInfoDatabase.superAttacksName[selectedItemPosition]);
    superAttackDescText.setText(CardInfoDatabase.superAttacksDesc[selectedItemPosition]);
    if (passiveSkillTitleText != null && passiveSkillDescText != null) {
        passiveSkillTitleText.setText(CardInfoDatabase.passiveSkillsName[selectedItemPosition]);
        passiveSkillDescText.setText(CardInfoDatabase.passiveSkillsDesc[selectedItemPosition]);
    }
    hpText.setText(CardInfoDatabase.hp[selectedItemPosition].toString());
    attText.setText(CardInfoDatabase.att[selectedItemPosition].toString());
    defText.setText(CardInfoDatabase.def[selectedItemPosition].toString());
    costText.setText(CardInfoDatabase.cost[selectedItemPosition].toString());
}
}

firstLayout.xml:

<?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"
    android:contentDescription="@string/card_image" />

<!--
<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"
 -->

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

    <TextView
        android:id="@+id/COST"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/leaderSkillDesc"
        android:layout_marginBottom="9dp"
        android:layout_toEndOf="@+id/superAttackDesc"
        android:paddingLeft="3dp"
        android:paddingRight="3dp"
        android:text="00"
        android:textColor="@color/white"
        android:textSize="17sp"
        android:textStyle="bold" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">

        <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="33dp"
            android:fontFamily="monospace"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="18sp"
            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="73dp"
            android:layout_toEndOf="@+id/HP"
            android:fontFamily="monospace"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="18sp"
            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="42dp"
            android:fontFamily="monospace"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="18sp"
            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="13dp"
        android:layout_marginEnd="37dp"
        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="245dp"
        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="74dp"
        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"
         />

    <Button
        android:id="@+id/arrowButton"
        android:layout_width="60dp"
        android:layout_height="35dp"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/leaderSkillDesc"
        android:layout_marginBottom="7dp"
        android:layout_marginStart="75dp"
        android:background="@drawable/arrow_up"
        android:textOff=""
        android:textOn="" />

</RelativeLayout>

</LinearLayout>

SecondLayout.xml:

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

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

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@drawable/card_details_open">

    <TextView
        android:id="@+id/COST"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativeLayout3"
        android:layout_marginTop="48dp"
        android:layout_toEndOf="@+id/superAttackDesc"
        android:paddingLeft="3dp"
        android:paddingRight="3dp"
        android:text="00"
        android:textColor="@color/white"
        android:textSize="17sp"
        android:textStyle="bold" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:id="@+id/relativeLayout3">

        <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="33dp"
            android:fontFamily="monospace"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="18sp"
            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="73dp"
            android:layout_toEndOf="@+id/HP"
            android:fontFamily="monospace"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="18sp"
            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="42dp"
            android:fontFamily="monospace"
            android:text="0000"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold"
            tools:text="0000" />

    </RelativeLayout>

    <TextView
        android:id="@+id/leaderSkillDesc"
        android:layout_width="250dp"
        android:layout_height="15dp"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/COST"
        android:layout_marginEnd="36dp"
        android:layout_marginTop="16dp"
        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="245dp"
        android:layout_height="15dp"
        android:layout_alignStart="@+id/leaderSkillDesc"
        android:layout_below="@+id/leaderSkillDesc"
        android:layout_marginTop="23dp"
        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_alignStart="@+id/superAttackTitle"
        android:layout_below="@+id/superAttackTitle"
        android:layout_marginTop="5dp"
        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" />


    <TextView
        android:id="@+id/passiveSkillTitle"
        android:layout_width="255dp"
        android:layout_height="17dp"
        android:layout_alignStart="@+id/superAttackDesc"
        android:layout_below="@+id/superAttackDesc"
        android:layout_marginBottom="3dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="23dp"
        android:background="@color/passiveSkillNameBackground"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:paddingLeft="7dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="viewStart"
        android:textColor="@android:color/holo_blue_light"
        android:textColorHighlight="@android:color/black"
        android:textSize="13sp"
        android:textStyle="italic"
        android:visibility="visible"
        tools:layout_editor_absoluteX="207dp"
        tools:layout_editor_absoluteY="543dp" />

    <TextView
        android:id="@+id/passiveSkillDesc"
        android:layout_width="250dp"
        android:layout_height="15dp"
        android:layout_alignStart="@+id/passiveSkillTitle"
        android:layout_below="@+id/passiveSkillTitle"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:fontFamily="monospace"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="13sp"
        android:textStyle="italic"
        android:visibility="visible"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="602dp" />

    <Button
        android:id="@+id/arrowButton"
        android:layout_width="60dp"
        android:layout_height="35dp"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/leaderSkillDesc"
        android:layout_marginBottom="7dp"
        android:layout_marginStart="75dp"
        android:background="@drawable/arrow_down"
        android:textOff=""
        android:textOn="" />

</RelativeLayout>

</RelativeLayout>
like image 886
Stelios Papamichail Avatar asked Jan 25 '18 12:01

Stelios Papamichail


1 Answers

I'd suggest using a RecyclerView with a custom Adapter, RecyclerView can be used vertically or horizontally.

like image 198
Stuey Avatar answered Oct 15 '22 19:10

Stuey