I'm creating a interface with an image, a textview and two buttons. The interface is not screen-size full, it's only like partial of the screen (like popup message). The position of the image and the button are fixed, but the content of textview is dynamic (pass through function from other file).
I was hoping the textview is flexible but have some height limit; meaning like if only one word is pass through to this interface, the interface would be like normal; but if a long string is pass through, the interface would expand a little bit but have a maximum to hit.
Here is what I currently do:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="16dp">
<ImageView
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_gravity="center_horizontal"
android:tint="@color/colorAccent"
app:srcCompat="@drawable/ic_error" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="150dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12dp"
android:gravity="center"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="bold"
android:scrollbars="vertical"
tools:text="title" />
</androidx.core.widget.NestedScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:layout_marginTop="15dp">
<TextView android:id="@+id/btn_back"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/grey_darker_rect_selector"
android:foreground="?selectableItemBackground"
android:gravity="center"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="@android:string/cancel"
android:textAllCaps="false"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold" />
<TextView android:id="@+id/btn_continue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/orange_dark_rect_curved"
android:foreground="?selectableItemBackground"
android:gravity="center"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="@android:string/ok"
android:textAllCaps="false"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
The code above works fine if a long string is pass through: the image and buttons are fixed, and the middle textview is scrollable; but if only a short string is pass through, there will be a big gap/blank space between the text and the button (due to the layout_height I set to 150dp).
I tried setting the maxHeight of the NestedScrollView to a certain amount, but seems like the maxHeight would be ignore and the height of the textview will expand until fullscreen size. If the string is too long, it might hide the image and button as well because the textview already occupied the whole screen.
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxHeight="300dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="bold"
android:scrollbars="vertical"
tools:text="title" />
</androidx.core.widget.NestedScrollView>
Is there anyway I can do, to make the height of the textview to expand dynamically until certain limit?