TextView restarts Marquee when changing another TextView in the same LinearLayout

I have a LinearLayout and inside 2 TextView both have a selection and when I update the text in the first and then the second TextView restarts the tent.

<LinearLayout android:id="@+id/panel" android:layout_width="320dp" android:layout_height="match_parent" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:gravity="center_vertical" android:orientation="vertical" > <TextView android:id="@+id/first" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:gravity="bottom|center_horizontal" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true" /> <TextView android:id="@+id/second" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:gravity="top|center_horizontal" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true" /> </LinearLayout> 

I found that if for R.id.first and R.id.second I set layout_width="320dp" effect does not occur. But I want to set android:layout_width="match_parent" is there a workaround?

I found a similar problem, but without a solution: Android, RelativeLayout restarts Marquee-TextView when changing ImageView in the same RelativeLayout

+4
source share
3 answers

I had a similar problem and the IS solution should set a fixed size for Textview.

So why not do it programmatically? In my case, he solved the problem. Here is my solution in detail:

The layout is a bit complicated with lots of varying values. Here is the interesting part:

layout.xml:

 <!-- The height and visibility values change programatically --> <RelativeLayout android:layout_width="fill_parent" android:layout_height="30dp" android:layout_alignParentBottom="true" android:visibility="gone" > <FrameLayout> ... // some code ... </FrameLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" /> <!-- My scrolling textview. see below. --> <!-- The size will be set when --> <!-- the layout will be draw, --> <!-- after the Activity.onCreate(). --> <!-- I removed ALL THE UNECESSARY (I mean --> <!-- scrollHorizontally, focusable and focusableInTouchMode. --> <!-- You don't need it !!!!) --> <fr.cmoatoto.android.widget.ScrollingTextView android:id="@+id/text" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" /> <ImageView ... // some code ... /> </LinearLayout> </RelativeLayout> 

ScrollingTextView was defined in this answer

Here again:

ScrollingTextView.java:

 public class ScrollingTextView extends TextView { public ScrollingTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ScrollingTextView(Context context, AttributeSet attrs) { super(context, attrs); } public ScrollingTextView(Context context) { super(context); } @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { if(focused) super.onFocusChanged(focused, direction, previouslyFocusedRect); } @Override public void onWindowFocusChanged(boolean focused) { if(focused) super.onWindowFocusChanged(focused); } @Override public boolean isFocused() { return true; } } 

And finally, activity. As I said, you need to set a fixed width and height so that we do this programmatically using the listener in onCreate ():

MyActivity.java:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout); TextView textView = ((TextView) findViewById(R.id.home_trafic_text)); textView.setText(getString(R.string.loading)); textView.setEnabled(true); // Thanks to Romain Guy textView.addOnLayoutChangeListener(new OnLayoutChangeListener() { @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { LayoutParams params = v.getLayoutParams(); params.width = right - left; params.height = bottom - top; params.weight = 0; v.removeOnLayoutChangeListener(this); v.setLayoutParams(params); } }); } 

Be careful if you need to change orientation or something like that, but for me it is very good!

---- EDIT FOR PRE-API-11 ---

Since OnLayoutChangeListener exists only from Api v11, there is a workaround (it works, but I think it is less good):

Extract OnLayoutChangeListener from your activity:

MyActivity.java:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout); TextView textView = ((TextView) findViewById(R.id.home_trafic_text)); textView.setText(getString(R.string.loading)); textView.setEnabled(true); // Thanks to Romain Guy } 

and add onSizeChanged to your ScrollingTextView:

ScrollingTextView.java:

 public class ScrollingTextView extends TextView { ... // Same code as before ... @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); LayoutParams params = (LayoutParams) getLayoutParams(); params.width = w; params.height = h; params.weight = 0; setLayoutParams(params); } } 

Hope this helps!

+10
source

You should be prevented from placing both selections in the same ViewGroup. In your case, you can wrap each area of ​​the TextView with LinearLayout (this will not work when using RelativeLayout).

+6
source

just a simple fix .. :) no need to worry much ... just fix the width of the textview as 800dp or too wide. he will solve the reset problem

+2
source

All Articles