0

I have a ListView and a button below. The ListView is inside an AlertDialog and becomes scrollable at some point when the list gets big. The problem is at that point, when list gets too big, the button underneath is replaced by the ListView. How can I Solve this Problem?

My guess is that I should limit the maximum height of the ListView. But at the same time I don’t want the height to be static. I still want the height of the AlertDialog, or ListView, to change when possible without removing the button. Here’s an image:

image

I tried to add this:

listTeachers.addFooterView(cLayout);

But then got the error:

“java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams”

My XML: (Part of it. The ListView and the Button)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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">

<RelativeLayout
    android:id="@+id/tv_title"
    android:layout_width="match_parent"
    android:layout_height="60dp">



</RelativeLayout>



<ListView
    android:id="@+id/list_teachers"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/tv_title"
    android:divider="#004e4e4e"
    android:dividerHeight="0dp"></ListView>

<android.support.constraint.ConstraintLayout
    android:id="@+id/tv_buttons"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_below="@id/list_teachers">


    <ImageButton
        android:id="@+id/imageButton_Delete"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginBottom="8dp"
        android:adjustViewBounds="true"
        android:background="#00FFFFFF"
        android:scaleType="fitXY"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.50"
        app:layout_constraintVertical_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/delete_closed2"
        tools:layout_conversion_wrapHeight="105"
        tools:layout_conversion_wrapWidth="105" />



</android.support.constraint.ConstraintLayout>
  • Are you willing to change the implementation of your dialog? You should be able to get what you're looking for if you change the root from `RelativeLayout` to `ConstraintLayout` (and use `RecyclerView` instead of `ListView`). See my answer here for how to make a RecyclerView wrap its content when it is small enough but become scrollable when it needs to be: https://stackoverflow.com/a/49452628/8298909 – Ben P. Aug 09 '19 at 19:28
  • Ok cool thanks. I want to keep the ListView, but nice to know there are other solutions if I get stuck. :) – Daniel Andersson Aug 09 '19 at 19:31

0 Answers0