mtveezy
mtveezy

Reputation: 711

Dynamic TextView won't marquee if text is longer than view

I have the user input text into an EditText, and then a textview displays that text. Below I have the TextView (preview_pane) updating its text every time the EditText is updated, which works properly.

EditText myed = (EditText) findViewById(R.id.text_input);
    myed.setFilters(new InputFilter[] {new InputFilter.AllCaps()});
    myed.addTextChangedListener(new TextWatcher() {

        ...

        @Override
        public void afterTextChanged(Editable s) {
            TextView previewText = (TextView) findViewById(R.id.preview_pane);
            EditText et = (EditText) findViewById(R.id.text_input);
            previewText.setText(et.getText().toString());
            previewText.setSelected(true);
        }
    });

And below I have the xml of the TextView set to marquee

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_margin="30sp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="#FFFFFF">
    <TextView
        android:id="@+id/preview_pane"
        android:layout_width="wrap_content"
        android:layout_height="80sp"
        android:layout_gravity="center_horizontal"
        android:textSize="70sp"
        android:textColor="#DDDDDD"
        android:layout_marginTop="10sp"
        android:includeFontPadding="false"
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        />
</RelativeLayout>

When the text is shorter than the 300sp layout width, it marquees properly. However, I don't want it to marquee if it all fits in the layout. And, once the text becomes too long to fit on the layout, it ellipsizes (...) the end of the text and doesn't marquee, the complete opposite of what I want!

So, my question is, how can I make it so the textview doesn't marquee the text when its short enough to fit in the layout, and does marquee it without ellipsizing it when its too long?

Upvotes: 1

Views: 998

Answers (1)

Jenisha Makadiya
Jenisha Makadiya

Reputation: 832

set TextView width to match parent

<TextView
        android:id="@+id/preview_pane"
        android:layout_width="match_parent"
        android:layout_height="80sp"
        android:layout_gravity="center_horizontal"
        android:textSize="70sp"
        android:textColor="#DDDDDD"
        android:layout_marginTop="10sp"
        android:includeFontPadding="false"
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        />

Hope this will helps you..!

Upvotes: 1

Related Questions