Search code examples
androidtextviewandroid-relativelayout

How to prevent truncation in RelativeLayout


I am making an application to display movies sorted by Popularity and date.

When I click on a movie, it takes me to the description of the movie. I am using RelativeLayout for the description UI. But the movie name is truncated if its too large.

I have tried few solutions from stackoverflow for the textView like

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

I know there's one solution of using LinearLayout. But I wanted to know if there's a solution using RelativeLayout only.

Please Find the screenshot by clicking on this link. The text gets truncated and its replaced by three dots

Here's the full code :

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:paddingStart="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin_less"
        android:paddingTop="@dimen/activity_vertical_margin_less"
        android:paddingBottom="@dimen/activity_vertical_margin_less"
        tools:context="com.example.puneet.movieout.MovieInfoDisplay"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Movie Title"
            android:id="@+id/textView"
            android:textStyle="bold"
            android:layout_below="@+id/imageView"
            android:layout_alignParentStart="true"
            android:layout_marginTop="20dp"
            android:textColor="@color/black"
            android:textSize="20sp" />
        <TextView
            android:scrollHorizontally="true"
            android:ellipsize="end"
            android:lines="1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Movie name is Long. Movie name is Long"
            android:layout_marginTop="20dp"
            android:id="@+id/textView2"
            android:layout_above="@+id/textView4"
            android:layout_toRightOf="@id/textView"
            android:layout_alignStart="@+id/textView6"
            android:layout_below="@+id/imageView"
            android:textSize="20sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="User Rating"
            android:id="@+id/textView3"
            android:textStyle="bold"
            android:layout_below="@+id/textView2"
            android:layout_alignParentStart="true"
            android:textColor="@color/black"
            android:textSize="20sp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="User Rating"
            android:layout_toRightOf="@id/textView3"
            android:id="@+id/textView4"
            android:layout_below="@+id/textView"
            android:layout_alignStart="@+id/textView6"
            android:textSize="20sp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Release Date"
            android:id="@+id/textView5"
            android:textStyle="bold"
            android:layout_below="@+id/textView3"
            android:layout_alignParentStart="true"
            android:textColor="@color/black"
            android:layout_marginRight="10dp"
            android:textSize="20sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView6"
            android:layout_toRightOf="@id/textView5"
            android:layout_below="@+id/textView4"
            android:textSize="20sp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Overview"
            android:id="@+id/textView7"
            android:textStyle="bold"
            android:layout_below="@+id/textView5"
            android:layout_alignParentStart="true"
            android:textColor="@color/black"
            android:textSize="20sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textView8"
            android:layout_toRightOf="@id/textView7"
            android:layout_below="@+id/textView6"
            android:layout_alignStart="@+id/textView6"
            android:textSize="20sp"/>
    </RelativeLayout>
</ScrollView>

Solution

  • If you don't want to get the text truncated , you have to remove the ellipsize attribute and add your TextView inside HorizontalScrollView. Your view should be like :

    <HorizontalScrollView
            android:id="@+id/txt_scrollview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
              android:layout_above="@+id/textView4"
                android:layout_toRightOf="@id/textView"
                android:layout_alignStart="@+id/textView6"
                android:layout_below="@+id/imageView"
            android:scrollbars="none"
            >
     <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Movie name is Long. Movie name is Long"            
                android:id="@+id/textView2"          
                android:textSize="20sp"/>
    </HorizontalScrollView>
    

    This way you can embed your text inside the scroll without the text being truncated.