6

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>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Stelios Papamichail
  • 955
  • 2
  • 19
  • 57
  • you should put relative layout inside the horizontal scroll view. – Farhana Naaz Ansari Jan 25 '18 at 12:23
  • @farhana you mean put the whole TOP relative layout inside a horizontal scroll view? How would that help me? – Stelios Papamichail Jan 25 '18 at 12:26
  • Yes, not main Relative layout, that relative layout which contains text view. – Farhana Naaz Ansari Jan 25 '18 at 12:26
  • @farhana that relative layout contains everything but the ImageView. How would that horizontal scroll view help me? – Stelios Papamichail Jan 25 '18 at 12:29
  • Use [systrace](https://developer.android.com/studio/command-line/systrace.html). – azizbekian Feb 02 '18 at 18:43
  • 1
    instead of `setContentView` include both layouts in your main layout and then use `firstLayout.setVisibility(View.GONE);` and `SecondLayout.setVisibility(View.VISIBLE);` and reverse. and change arrow ids to `arrowButton1` and `arrowButton2` and see if it works. – M D P Feb 03 '18 at 05:10
  • @azizbekianI've been trying to use systrace but I keep getting this error: https://stackoverflow.com/questions/48606877/systrace-output-error – Stelios Papamichail Feb 05 '18 at 07:26
  • 1
    Can you create an [MCVE](https://stackoverflow.com/help/mcve) that demonstrates this problem? It would help in finding an answer if you can. – Cheticamp Feb 07 '18 at 01:33
  • @SteliosPapamichael do you fixed issue ? – Swaminathan V Feb 08 '18 at 10:23
  • @RaguSwaminathan nope..still there sadly – Stelios Papamichail Feb 08 '18 at 10:27
  • so leaderSkillDesc and superAttackTitle stop scrolling when navigating to next page ? But works fine when starting freshly ? – Swaminathan V Feb 08 '18 at 10:36
  • try to move your background images to drawable-nodpi folder. if not then create it. it seems strange, but it worked for me. – Rumit Patel Feb 08 '18 at 14:40
  • @RaguSwaminathan what do you mean by starting fresh? Reopening the screen or the app and going to the first layout again? – Stelios Papamichail Feb 08 '18 at 16:21
  • Why are you add listeners recursively? I suppose after dozen clicks you will have dozen listeners which do the same thing. And after refreshing screen phone could remove them. Could you run app in debug mode and count how many listeners you have? Also, could you add in question more debug output (say, add debug message on entering and exiting the problem method and methods which call it) – ADS Feb 09 '18 at 13:48

1 Answers1

1

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

Stuey
  • 48
  • 7