Reputation: 253
I've got a TextView. When multi-line text inside is short and fit to available space I set gravity to top:
tv.setGravity(Gravity.TOP);
When the text is longer than space in TV I'd like to show only the end of text by:
tv.setGravity(Gravity.BOTTOM);
Layout:
<RelativeLayout
android:id="@+id/textLayout"
android:layout_marginTop="16dp"
android:layout_width="fill_parent"
android:layout_height="110dp"
android:orientation="vertical"
android:visibility="visible" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="top"
android:text="Long text bla bla."
android:textColor="@android:color/black"
android:textColorHint="@android:color/black"
android:textSize="37sp" />
...
</RelativeLayout>
What is the simplest solution to check if text (in many lines) is longer than available space (I don't want to hardcode number of lines because size is different for different screens)? Is there any indicator?
Upvotes: 0
Views: 4194
Reputation: 3064
Try put TextVew
in LinearLayout
and compare textview height with linearlayout height.
Like this
<RelativeLayout
android:id="@+id/textLayout"
android:layout_marginTop="16dp"
android:layout_width="fill_parent"
android:layout_height="110dp"
android:orientation="vertical"
android:visibility="visible" >
<LinearLayout
android:id ="@+id/linear"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="top"
android:text="Long text bla bla."
android:textColor="@android:color/black"
android:textColorHint="@android:color/black"
android:textSize="37sp" />
To compare between theme :
if(text.getHeight()>linear.getHeight()){
//Here TextView longer than available space
}
Hope this helped you.
Upvotes: 1
Reputation: 12656
Rather than changing the gravity
in order to force display of the end of the text, I suggest that you use ellipsis with the dots at the start so that the end of your text will display.
Example:
<TextView
android:ellipsize="start"
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="top"
android:text="Long text bla bla."
android:textColor="@android:color/black"
android:textColorHint="@android:color/black"
android:textSize="37sp" />
You may also end up choosing some of the other android:ellipsize
options such as "middle
" and "marquee
". See: TextView:android:ellipsize
Upvotes: 1