5

I have not found a solution for my problem, maybe you can help me here.

I am using a RelativeLayout with an ImageView and a TextView as children. The TextView contains a large text and should scroll from right to left. But everytime when I set a new image to the ImageView, the marquee starts from beginning.

I think that by setting a new image the TextView looses its focus and so the marquee starts again. How can I prevent that or is there something else I am doing wrong? Would be great if someone could point me to the correct solution.

Thanks a lot!

You can reproduce my problem with this code:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils.TruncateAt;
import android.view.Display;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RelativeLayoutActivity extends Activity {

    private ImageView mImageView;
    private TextView mTextView;

    private Handler mHandler = new Handler();
    private int mCurrentImage = 0;

    private Runnable mCallback = new Runnable() {
        @Override
        public void run() {
            toggleImage();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        initLayout();
        setLongText();

        mHandler.postDelayed(mCallback, 5000);
    }

    private void initLayout() {
        RelativeLayout layout = (RelativeLayout) findViewById(R.id.parentLayout);

        Display display = getWindowManager().getDefaultDisplay(); 
        int screenWidth = display.getWidth();
        int screenHeight = display.getHeight();

        mImageView = new ImageView(this);
        mImageView.setScaleType(ScaleType.FIT_XY);
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(screenWidth,screenHeight);        
        mImageView.setImageDrawable(this.getResources().getDrawable(R.drawable.red));
        layout.addView(mImageView, rlp);

        // marquee text at the bottom
        mTextView = new TextView(this);
        mTextView.setSingleLine();
        mTextView.setEllipsize(TruncateAt.MARQUEE);
        mTextView.setMarqueeRepeatLimit(-1);
        mTextView.setHorizontallyScrolling(true);
        mTextView.setFocusable(true);
        mTextView.setFocusableInTouchMode(true);
        mTextView.setBackgroundColor(Color.BLACK);
        mTextView.setTextColor(Color.WHITE);

        rlp = new RelativeLayout.LayoutParams(screenWidth, 50);
        rlp.topMargin = screenHeight-100;
        layout.addView(mTextView, rlp);
    }

    private void setLongText() {
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<50; i++) {
            sb.append(" ");
        }
        for (int i=0; i<50; i++) {
            sb.append("A");
        }
        for (int i=0; i<50; i++) {
            sb.append("B");
        }
        for (int i=0; i<50; i++) {
            sb.append("C");
        }
        mTextView.setText(sb.toString());
        mTextView.setSelected(true);
    }

    private void toggleImage() {
        mCurrentImage++;
        if (mCurrentImage % 2 == 0) {
            mImageView.setImageDrawable(this.getResources().getDrawable(R.drawable.red));
        } else {
            mImageView.setImageDrawable(this.getResources().getDrawable(R.drawable.green));
        }

        mHandler.postDelayed(mCallback, 5000);
    }
}
robotniko
  • 206
  • 3
  • 8
  • Possible duplicate of [TextView restarts Marquee when changing another TextView in same LinearLayout](http://stackoverflow.com/questions/11856875/textview-restarts-marquee-when-changing-another-textview-in-same-linearlayout) – Mogsdad Feb 25 '16 at 19:33

4 Answers4

5

I had a same problem and I fixed it just now:)

If your TextView which in layout XML contains layout_weight

android:layout_weight="1"

Remove it!This attibute cause marquee restart.

Hope helpful:)

Peter Zhao
  • 7,456
  • 3
  • 21
  • 22
5

The marquee reset problem is because of loss of focus of the TextView on which it is running. To overcome these issues you can simply surround your TextView with another RelativeLayout so that it is in a different ViewGroup as your other views.

You have another option: Create a new class that is a subclass of TextView and override onFocusChanged and onWindowFocusChanged to prevent loss of focus for the said textview.

That's it. Using these techniques your marquee won't restart every time another element gains focus.

Advait Saravade
  • 3,029
  • 29
  • 34
0

See my answer here : https://stackoverflow.com/a/13841982/1823503

The idea is :

  • to fix the width and height programmatically (you did it)
  • to setSelected (you did it)
  • to Override your TextView to fix the focus problems
Community
  • 1
  • 1
CmoaToto
  • 219
  • 3
  • 6
  • Hi, this did not work for me. I have added your ScrollingTextView and also added an override for "onSizeChanged", because I am on API 10. Then I have replaced my TextView with a ScrollingTextView instance and also added mTextView.setEnabled(true). But ScrollingTextView.onSizeChanged() is only called once and the marquee still restarts. Did I miss something? – robotniko Mar 04 '13 at 19:59
0

just a simple Fix..:) no need to worry much...just fix width of textview as some 800dp or too higher width. it will solve reset issue

    <TextView
    android:id="@+id/adv_txt_view"
    android:layout_width="800dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:singleLine="true"
    android:ellipsize="marquee"                               

    android:fadingEdge="horizontal"
    android:marqueeRepeatLimit="marquee_forever"                           
    android:focusableInTouchMode="true"                           
    android:focusable="true"                     
    android:scrollHorizontally="true"                       
    android:textColor="@color/black"
    android:text="large text to scroll!!" />