Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textview show partial text

Tags:

I have a TextView in which I must load a message. The TextView has maximum 2 lines (android:lines="2"). The message may contain an '\n' character.

How should I load the message in that TextView so that words would be wrapped and if in those 2 lines message does not fit, at the end of last visible word I must add three dots (...)? How can I detect the length of text that fits in that TextView?

My code for TextView is

<TextView
            a:id="@+id/tv_message"
            a:gravity="top"
            a:layout_width="wrap_content"
            a:layout_height="wrap_content"
            a:layout_alignParentTop="true"
            a:layout_toRightOf="@id/iv_icon"
            a:layout_marginLeft="2dp"
            a:layout_marginTop="4dp"
            a:paddingRight="7dp"
            a:paddingBottom="5dp"
            a:textSize="12sp"
            a:typeface="sans"
            a:ellipsize="marquee"
            a:lines="2"
            a:maxLines="2"
            a:textColor="@android:color/black"
            />

But in application, text appears on two lines, even if there is a line containing the signature. The message is like: "message text" + "\nSignature" but the message can be on 1,2,3 lines, depending of message length.

like image 859
Buda Gavril Avatar asked Feb 01 '11 07:02

Buda Gavril


1 Answers

In XML, use the property android:ellipsize="marquee"for the TextView.

Your TextView can be defined like:

<TextView 
android:id="@+id/text" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:maxLines="2" 
android:lines="2" 
android:ellipsize="marquee"/>

Edit: I've tried the following code, adapted from yours:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlt_news"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/cell_background"
    android:padding="5dip" >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:paddingLeft="5dip"
        android:text="Name"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/rss_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:layout_marginLeft="2dip"
        android:adjustViewBounds="true"
        android:maxHeight="100dip"
        android:maxWidth="100dip"
        android:src="@drawable/rss_cell_icon"
        android:visibility="visible" />

    <TextView
        android:id="@+id/tv_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="4dp"
        android:layout_toRightOf="@id/rss_icon"
        android:ellipsize="marquee"
        android:gravity="top"
        android:lines="2"
        android:maxLines="2"
        android:paddingBottom="5dp"
        android:paddingRight="7dp"
        android:text="Description of the news, just the short paragraph, \nSignature, to see how it works if there are more than two lines"
        android:textColor="@android:color/black"
        android:textSize="12sp"
        android:typeface="sans" />

</RelativeLayout>

And here is the output:
enter image description here

like image 68
Adinia Avatar answered Sep 30 '22 12:09

Adinia