Search code examples
androidtextviewandroid-cardviewandroid-gridlayout

Displaying TextView in CardView


I have a problem - sometimes text of the TextView climbs out of the CardView (view id - plot). And it looks like that: enter image description here

My layout structure:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/poster"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:id="@+id/scrollView"
        android:scrollbars="none"
        android:paddingTop="300dp">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardElevation="@dimen/spacing_xmedium"
                app:cardUseCompatPadding="true">

                <android.support.v7.widget.GridLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/white"
                    app:columnCount="2">
                    <TextView
                        android:id="@+id/tv_title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/spacing_large"
                        android:layout_marginRight="@dimen/spacing_large"
                        android:layout_marginTop="@dimen/spacing_large"
                        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
                        app:layout_columnSpan="2"
                        app:layout_columnWeight="1"
                        app:layout_gravity="fill_horizontal"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/tv_rating"
                        android:layout_margin="@dimen/spacing_medium"
                        android:textSize="16sp"
                        android:textStyle="bold"
                        android:text="@string/rating"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/rating"
                        android:layout_margin="@dimen/spacing_medium"
                        android:textSize="15sp"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/tv_runtime"
                        android:textSize="16sp"
                        android:textStyle="bold"
                        android:layout_margin="@dimen/spacing_medium"
                        android:text="@string/runtime" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/runtime"
                        android:layout_margin="@dimen/spacing_medium"
                        android:textSize="15sp" />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/tv_release_year"
                        android:layout_margin="@dimen/spacing_medium"
                        android:textSize="16sp"
                        android:textStyle="bold"
                        android:text="@string/release_year"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/release_year"
                        android:layout_margin="@dimen/spacing_medium"
                        android:textSize="15sp"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/tv_country"
                        android:textSize="16sp"
                        android:layout_margin="@dimen/spacing_medium"
                        android:textStyle="bold"
                        android:text="@string/country" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/spacing_medium"
                        android:id="@+id/country"
                        android:textSize="15sp" />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/spacing_medium"
                        android:id="@+id/tv_director"
                        android:textSize="16sp"
                        android:textStyle="bold"
                        android:text="@string/director"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/spacing_medium"
                        android:id="@+id/director"
                        android:textSize="15sp"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/spacing_medium"
                        android:id="@+id/tv_writer"
                        android:textSize="16sp"
                        android:textStyle="bold"
                        android:text="@string/writer"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/spacing_medium"
                        android:id="@+id/writers"
                        android:textSize="15sp"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/spacing_medium"
                        android:id="@+id/tv_genre"
                        android:textSize="16sp"
                        android:textStyle="bold"
                        android:text="@string/genre"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/spacing_medium"
                        android:id="@+id/genre"
                        android:textSize="15sp"/>

                    <android.support.v7.widget.Space
                        app:layout_columnSpan="2"
                        app:layout_columnWeight="1"
                        app:layout_rowWeight="1" />

                    <TextView
                        android:id="@+id/plot"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/spacing_xmedium"
                        android:typeface="sans"
                        android:textSize="18sp"
                        app:layout_columnSpan="2"
                        app:layout_columnWeight="1"
                        app:layout_gravity="fill_horizontal" />
                </android.support.v7.widget.GridLayout>
            </android.support.v7.widget.CardView>
        </FrameLayout>
    </ScrollView>
</FrameLayout>

This layout I've included in another layout which has a CoordinatorLayout as a root. So, I can't figure out which view affects on this TextView.


Solution

  • In your view with id tv_title and plot, where you already have app:layout_gravity="fill_horizontal", set the android:layout_width as 0dp. In all the views that compose the right part of "grid" per se, for instance the view with id rating, add app:layout_gravity="fill_horizontal" and set the android:layout_width as 0dp.