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.

Community
  • 1
  • 1
dazedviper
  • 914
  • 7
  • 15

2 Answers2

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);
osayilgan
  • 5,770
  • 7
  • 45
  • 68
  • 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](http://imgur.com/VjKY8eT). The complete text is "this text is too long to fit inside this text view". – dazedviper Aug 31 '14 at 20:58
  • 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. – osayilgan Aug 31 '14 at 21:54
  • or is it your actual text ? "this text is too long to fit inside this text view" – osayilgan Aug 31 '14 at 21:57
  • 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. – dazedviper Sep 02 '14 at 21:39
  • 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](http://stackoverflow.com/a/2504840/1080954) – osayilgan Sep 02 '14 at 21:53
  • Yep. That fixed it. Thank you. See my edit to the question. If you edit your answer I'll accept it. – dazedviper Sep 02 '14 at 22:14
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

Community
  • 1
  • 1
Stephane Mathis
  • 6,192
  • 6
  • 38
  • 68