0

I want to create a LinearLayout with two TextViews: a label and a data placeholder. Since the data can be arbitrarily long, I want to restrict the size of the second TextView to a single line, and to automatically scroll horizontally if the data does not fit in the view.

Also, I want the width of the second TextView to be calculated at runtime, so that it can fill 70% of the parent container and be aligned to its right.

So far this is what I've got:

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/ll_denunciante" > <TextView android:id="@+id/txtv_label_denunciante" android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="0.3" android:gravity="left" android:text="@string/txtv_label_denunciante" /> <TextView android:id="@+id/txtv_data_denunciante" android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="0.7" android:gravity="right" android:maxLines="1" android:ellipsize="marquee" android:scrollHorizontally="true" android:marqueeRepeatLimit="marquee_forever" android:text="@string/txtv_no_data" /> </LinearLayout> 

But the text is simply cut off. It is not ellipsized and automatic scrolling does not work. Adding

android:focusable="true" android:focusableInTouchMode="true" 

does not fix the problem.

The answer to this question did not help either.

I'll gladly accept an answer using a RelativeLayout if it accomplishes the desired result using less code or if it is not possible using a LinearLayout.

Edit

Changing the second TextView to:

<TextView android:id="@+id/txtv_data_denunciante" android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="0.7" android:ellipsize="marquee" android:gravity="right" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true" android:text="@string/txtv_no_data" /> 

and adding

txtvDataDenunciante.setSelected(true); 

fixed it.

2
  • Is it in a ListView ? Commented Aug 31, 2014 at 16:57
  • No, it is not inside a ListView. Commented Aug 31, 2014 at 20:58

2 Answers 2

1

I tried this way and it works. Replace your 2nd TextView with the one below. Here is the Result in the attached screenshot.

<TextView android:id="@+id/txtv_data_denunciante" android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="0.7" android:ellipsize="marquee" android:gravity="right" android:focusable="true" android:focusableInTouchMode="true" android:scrollHorizontally="true" android:marqueeRepeatLimit="marquee_forever" android:maxLines="1" android:singleLine="true" android:freezesText="true" android:text="@string/txtv_no_data" android:selectAllOnFocus="true" /> 

EDIT : If XML attributes don't work in your case, then the problem is about taking the focus. In some cases, parent Layouts or some others take the focus on theirself. So you need to gain the focus for the particular View element yourself on the RunTime. To do that, you can set your TextView as selected.

yourTextView.setSelected(true); 
Sign up to request clarification or add additional context in comments.

6 Comments

Well I've just tested this with a clean layout and activity and the text does not scroll. Instead, I get static behaviour as shown here. The complete text is "this text is too long to fit inside this text view".
How long is your text to fit inside the textview ? Can you paste it here, so I can try it with the same content. Because it works so far for me.
or is it your actual text ? "this text is too long to fit inside this text view"
Your code works in a clean activity. However, my TextView is inside a Fragment that gets instantiated in a ViewPager. Then it does not work.
as far as I know it's a focus issue. So my guess is that the parent is taking the focus on it, instead of your text view. Can you try setting that in the code. yourTextView.setSelected(true);or simply try that, this sounds more promising. SOF Question
|
0

If it is in a ListView, the problem is that you TextView does not get the focus. Change your TextView to use this one :

public class AutoScrollingTextView extends TextView { public AutoScrollingTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public AutoScrollingTextView(Context context, AttributeSet attrs) { super(context, attrs); } public AutoScrollingTextView(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; } } 

With these parameters :

android:scrollHorizontally="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" 

As seen here : https://stackoverflow.com/a/9707140/1318795

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.