4

Firstly, I did a Google search and looked at StackOverFlow questions related to text being cut off for TextView. They did not help to solve the problem at hand, though they provided better understanding of certain things.

Here is my problem.

I am using TableLayout to display records with two text fields. Each row has two TextView cells. layout_width is set to wrap_content for all cells. But, even though the text is displayed in multiple lines, every line in that multi-line text is cut off at the end.

e.g.

   <?xml version="1.0" encoding="utf-8"?>
   </TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content">
   <TableRow>
        <TextView
            android:text="Notes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/visitation_notes" />
        <TextView
            android:text="Need to visit again. Visit till the doctor understands the importance of the Test. The problem is, it is very difficult to get appointment for the visit. Still, we need to keep trying till the point reaches to him/her."
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/visitation_notes_value"
            android:textAlignment="viewStart"/>
   </TableRow>
   </TableLayout>

produced the view

Notes.Need to visit again. Visit till the doctor understands the importance of the Test. The problem is, it is very difficult to get appointment for the visit. Still, we need to keep trying till the point reaches to him/her.

with the text written in bold getting cut off.

If I use LinearLayout in place of TableLayout, it works fine. But, the problem with that is I can't have second column of each row starting at same position(with no hardcoding).

Please help me how to make sure TextView content won't get cut off.

Here is the screenshot of view produced for example given above.

Satish Thulva
  • 274
  • 2
  • 7
  • 1
    Attach screenshot – Rahul Dec 28 '16 at 14:02
  • I've attached screenshot now. Please check now. – Satish Thulva Dec 28 '16 at 14:12
  • What is width and height of TableRow? I tried your code and its working fine for me – Rahul Dec 28 '16 at 14:14
  • Oh, I'm not sure what am I missing. For row also, width is set to match_parent and height is set to wrap_content. – Satish Thulva Dec 28 '16 at 14:28
  • I don't understand why the question is down voted. I did go through a plethora of questions on the same topic and found code snippets as answers with not enough explanation. As this is my first question, I'd be grateful if I get a feedback on what else I might've done before asking the question. – Satish Thulva Dec 29 '16 at 08:06

4 Answers4

3

Do this

<TableRow>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Note" />

    <TextView
        android:layout_weight="3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="Need to visit again. Visit till the doctor understands the importance of the Test. The problem is, it is very difficult to get appointment for the visit. Still, we need to keep trying till the point reaches to him/her." />


</TableRow>
saumil patel
  • 139
  • 1
  • 5
  • Thank you for your help. It does work. But, please help me why it works. As far as I know, `layout_weight` property helps us to divide extra space left after determining how much space each `View` takes in `LinearLayout`. I don't understand how this works when we set `layout_width=wrap_content` and the content size exceeds screen size in case of `TextView`. – Satish Thulva Dec 29 '16 at 06:22
  • you can adjust size using ems attribute . and if you set width=0dp still it work but you have to manage layout_weight value. – saumil patel Dec 29 '16 at 07:24
  • you can fix the width of textview by using ems. – saumil patel Dec 29 '16 at 07:27
  • Thank you. I ended up setting `layout_width = 0' for both `TextView`s and `layout_weight = 1` for first view and `layout_weight = 3` for the second(for better view). But, can you help me understand why my original code does not work ? – Satish Thulva Dec 29 '16 at 08:02
2

I got the truncated TextView inside a TableRow element issue resolved with the following code:

<TableRow>

    <TextView
        android:layout_gravity="right"
        android:text="Address: "
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingRight="10dp"
        android:text="123 W. Abc St., San Diego CA 90333"/>

</TableRow>
Gene Bo
  • 11,284
  • 8
  • 90
  • 137
  • This works, would be worth to mention that layout_weight combined with wrap_content is what's making it work. – Isma Nov 10 '19 at 18:44
1

Try this code and suits for all devices

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bgcolor"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="10dip" >

<LinearLayout
    android:id="@+id/llAvailableBookings"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center|top"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="25dip"
        android:gravity="center"
        android:textSize="24.5sp"
        android:visibility="gone" />

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:stretchColumns="*" >

        <TableRow>

            <TextView
                android:id="@+id/visitation_notes"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Notes" />

            <TextView
                android:id="@+id/visitation_notes_value"
                android:layout_width="1dp"
                android:layout_height="wrap_content"
                android:singleLine="false"
                android:text="Need to visit again. Visit till the doctor understands the importance of the Test. The problem is, it is very difficult to get appointment for the visit. Still, we need to keep trying till the point reaches to him/her."
                android:textAlignment="viewStart" />
        </TableRow>
    </TableLayout>
</LinearLayout>
Rajakumar
  • 907
  • 1
  • 7
  • 17
  • Thanks for the reply. But, is n't singleLine deprecated ? Moreover, if it can be done, I prefer not to hardcode 0dp and 1dp for view width. Anyway, I tried the above code and the view doesn't look nice. It has hardly one word on each line. – Satish Thulva Dec 28 '16 at 14:22
  • Which device are you using for simulation? – Rajakumar Dec 28 '16 at 14:25
  • I'm using Nexus 4. Does it make a difference ? – Satish Thulva Dec 28 '16 at 14:30
  • I updated my code please check with that and tested in all devices its perfect..Thanks in advance – Rajakumar Dec 28 '16 at 14:30
  • Sometimes it may vary based on the device – Rajakumar Dec 28 '16 at 14:31
  • Thanks again for your efforts. It does work. But, can you please explain why it works and why my method does n't work ? – Satish Thulva Dec 29 '16 at 06:11
  • oh great..according to my knowledge most of the table layouts are completely based on weight concepts and i think you did some slight mistake somewhere and i cannot able to find clearly what's exact problem in that part of code which you posted.....If you like my answer please vote for it...Thanks in advance – Rajakumar Dec 29 '16 at 06:26
  • I can accept only after I receive a logical explanation of why the solution works. I need to figure out what went wrong with my approach too. – Satish Thulva Dec 29 '16 at 07:00
  • @SatishThulva you did not post entire xml code if you post means we can find what's your problem – Rajakumar Dec 29 '16 at 07:10
  • 1
    @SatishThulva the main use of layout_weight is to split the section into number of parts, if you make your table row layout_width=1 the child layouts(Textview) will split the weight 1 in to two equivalent parts. which means if you given your child layout width 0dp then the parent layout weight 1 will automatically split the textviews into equivalent values(0.5 weight and 0.5 weight), i think now you can Upvote HsRaja answer. – Bethan Dec 29 '16 at 07:11
  • @HsRaja : I left out the `TableLayout` element enclosing the `TableRow`elements which also has `layout_width` set to `match_parent`. I thought that won't create a problem. Nonetheless, I updated the question with full code. – Satish Thulva Dec 29 '16 at 07:33
  • @BKumar : Thank you for your comment. I learnt what `layout_weight` does. According to the docs, it helps us to divide **extra** space left after determining how much space each `View` takes in `LinearLayout` and I know that `TableRow` extends `LinearLayout`. So, if I set `layout_width = 0` and `layout_weight = 1` for both `TextView` elements, it produces a view with both `TextView`s taking half the space of row. This is what I did in the end. The answer is also achieving desired effect using only `stretchColumns`, I believe. Can you help me understand why my original code doesn't work ? – Satish Thulva Dec 29 '16 at 07:57
1

TableRow is a subclass/descendant of LinearLayout, so this is actually a problem of different default behavior in TableRow versus its superclass. If you have a horizontally-aligned LinearLayout containing multiple TextViews, Lint will generate a warning to explicitly turn the baselineAligned attribute on/off. But TableRow has the same ambiguity, without the convenient Lint warning.

So the layout error you're seeing is due to TableRow trying - and failing - to guess what you want the baseline alignment behavior to be. Add the android:baselineAligned attribute to each relevant TableRow and your layout should display correctly.

<?xml version="1.0" encoding="utf-8"?>
   </TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content">
   <TableRow
        android:baselineAligned="false">
        <TextView
            android:text="Notes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/visitation_notes" />
        <TextView
            android:text="Need to visit again. Visit till the doctor understands the importance of the Test. The problem is, it is very difficult to get appointment for the visit. Still, we need to keep trying till the point reaches to him/her."
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/visitation_notes_value"
            android:textAlignment="viewStart"/>
   </TableRow>
   </TableLayout>
MandisaW
  • 971
  • 9
  • 21