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:
<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" /> <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);
and add onSizeChanged to your ScrollingTextView:
ScrollingTextView.java:
public class ScrollingTextView extends TextView { ...
Hope this helps!