TextView cuts off text when it is long enough
Asked Answered
T

8

26

I have strange problem with TextView, it cuts off part of the text at the end. My layout looks like

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="top|center_horizontal"
        android:gravity="top|center_horizontal"
        android:orientation="vertical" >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="top|center_horizontal"
            android:gravity="top|center_horizontal"
            android:orientation="horizontal"
            android:layout_weight="1"
            android:layout_marginBottom="5dp">
            <Button
                android:id="@+id/btnPreviousQuestion"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/selector_arrow_left"
                android:clickable="true"
                android:visibility="gone" >
            </Button>
            <TextView
                android:id="@+id/txtQuestion"
                style="@style/question_text"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="top|center_horizontal"
                android:layout_weight="1"
                 />
            <Button
                android:id="@+id/btnNextQuestion"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:background="@drawable/selector_arrow_right"
                android:clickable="true" >
            </Button>
        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:orientation="horizontal" >
            <WebView
                android:id="@+id/wvMultimediaQuestion"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|center_horizontal"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="55dp"
                android:layout_weight="1"
                android:visibility="gone" />
        </LinearLayout>
    </LinearLayout>

and txtQuestion cuts off text when it is long enough. What is wrong, does anybody know ?

Trusteeship answered 23/12, 2011 at 13:27 Comment(2)
cuts off - when you display the text or when you parse it from the TextView?Bargello
@Trusteeship can you post screenshot of text cut off. small confusion in question. I tested your layout, its displaying entire text given.Breastwork
H
66

Make use of these attributes

    android:lines="1"
    android:scrollHorizontally="true"
    android:ellipsize="end"

will append "..." at the end. But this will not solve problem in honeycomb tab

So for honeycomb tablet add the following atttibute also

android:singleLine="true" 

On the other hand if you require marquee effect

android:singleLine="true"
android:ellipsize="marquee" 
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true" 
android:focusable="true" 
android:focusableInTouchMode="true
Harcourt answered 23/12, 2011 at 13:50 Comment(1)
Your code on the marquee effect only works for a single TextView in the layout. If you try to have multiple items scrolling then it doesn't work. How do you go about multiple TextView to have this effect?Joacimah
D
6

Yes there are some attributes you need to set, but first let us know what type of textview do you want exactly, single line or multi line?

There are some attributes you can take care of:

android:singleLine="true"  // or false
android:ellipsize="marquee"   // must check
android:lines="1"
Digester answered 23/12, 2011 at 13:31 Comment(0)
L
2

Set below properties in layout file. It works fine for me

android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
Leilaleilah answered 6/3, 2019 at 7:56 Comment(0)
D
1

If you expect differing lengths of text for your view or you're going to change the textSize of different instances of your TextView then you should use View.addOnLayoutChangeListener().

See an example here: https://mcmap.net/q/180582/-android-last-line-of-textview-cut-off

Dhow answered 19/5, 2017 at 12:14 Comment(1)
I can't see any reference to changes at runtime. If the layout doesn't change after creating the listener doesn't do much. In fact, nothing at all.Loopy
I
1

Set the layout_gravity on the TextView to fill

Inaugural answered 10/8, 2022 at 20:2 Comment(1)
please be more descriptive?Carny
F
0

You need to set android:minLines="2"

Forsook answered 30/4, 2013 at 23:0 Comment(1)
Why? What should that do?Loopy
M
0

For me it I realised I was using the layout view of a different device to the one I was testing with. When I corrected this, I could see the textlayout was way too wide so I used this setting to reign it in:

android:layout_width="330dp"
Mariettemarigold answered 25/1, 2021 at 23:16 Comment(1)
Not a good example for wrapping up the text in small space. best practice must use wrap_content or match_parent.Trinatrinal
H
0

Tried all the solutions provided here but none of these helps, finally got the solution using android:layout_width="0dp" instead of android:layout_width="fill_parent" in textview.

Headrick answered 17/1, 2023 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.