Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, RelativeLayout restarts Marquee-TextView when changing ImageView in same RelativeLayout

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);
    }
}
like image 716
robotniko Avatar asked Nov 20 '11 17:11

robotniko


2 Answers

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:)

like image 173
Peter Zhao Avatar answered Nov 03 '22 03:11

Peter Zhao


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.

like image 34
Advait Saravade Avatar answered Nov 03 '22 03:11

Advait Saravade