Cannot scroll text inside of CardView

59 Views Asked by At

I try to make the text "@string/info1" can be scrolled inside of cardView but it did not work. After expanding the cardView, it show all the text which is making the cardView's size be so big. How can I fix this problem?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.infoApp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:orientation="vertical"
        android:gravity="center">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@drawable/cardbackground">
    
                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:maxHeight="200dp"
                    android:onClick="expand"
                    android:layout_margin="20dp"
                    app:cardCornerRadius="20dp"
                    app:cardElevation="10dp"
                    app:contentPadding="20dp">
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/card_layout1"
                        android:orientation="vertical"
                        android:animateLayoutChanges="true">
    
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="WHO"
                            android:textSize="30sp"
                            android:textStyle="bold"
                            android:textColor="#113B68"/>
    
                        <ScrollView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">
    
                            <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:id="@+id/detail_info1"
                                android:visibility="gone"
                                android:text="@string/info1"
                                android:textSize="18sp"/>
    
                        </ScrollView>
    
                    </LinearLayout>
    
                    <ImageView
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:id="@+id/icon_layout1"
                        android:layout_gravity="end|right"
                        android:src="@drawable/baseline_arrow_drop_down_24" />
    
                </androidx.cardview.widget.CardView>
    
            </LinearLayout>
    
    </LinearLayout>

before expanding cardview

after expanding cardview

Here a few of screenshot before & after of expanding the cardView.

2

There are 2 best solutions below

3
General Tony On

When height was in wrap_content, it will expand the view itself, So change the height of cardView to fixed height, So it will work as you expected.

1
Mücahit Gök On

do not wrap content the height. do ficed value the height.