ExpandableListView expandable list not expanding when its height is wrap_content, and something is below it

Jai Saxena picture Jai Saxena · Jun 9, 2015 · Viewed 9.7k times · Source

Here is my layout code. When i click on the expandable list view it does not expand. However, if I increase the height of list it expands. Is there a way to increase the height of list dynamically through code? Can i make it to expand in the middle of my layout. Because if I increase the height, it'll just add unnecessary white space in collapsed list.

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">

    <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="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.pocketcash.pocketcash.HomeActivity">

        <Switch
            android:id="@+id/detailedCashInfoSwitch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:onClick="cashToggleSwitchClicked"
            android:text="Toggle Detailed Cash Info" />

        <Button
            android:id="@+id/addCashButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/expandableListView2"
            android:layout_centerHorizontal="true"
            android:onClick="launchAddCash"
            android:text="Add cash" />

        <Button
            android:id="@+id/addExpenditureButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/addCashButton"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="25dp"
            android:onClick="launchAddExpenditure"
            android:text="Add expenditure" />

        <TextView
            android:id="@+id/recentLogsText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/addExpenditureButton"
            android:layout_marginTop="50dp"
            android:text="Recent Logs:"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/logText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/recentLogsText"
            android:layout_marginTop="0dp"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/recentViewAllText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/recentLogsText"
            android:layout_marginLeft="10dp"
            android:layout_toEndOf="@+id/recentLogsText"
            android:layout_toRightOf="@+id/recentLogsText"
            android:onClick="recentViewLogClicked"
            android:text="(view all)"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/addViewAllText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/addCashButton"
            android:layout_centerHorizontal="true"
            android:onClick="addViewLogClicked"
            android:text="(view logs)"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/expenseViewAllText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/addExpenditureButton"
            android:layout_centerHorizontal="true"
            android:onClick="expenseViewLogClicked"
            android:text="(view logs)"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <ImageView
            android:id="@+id/userImage"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/detailedCashInfoSwitch"
            android:layout_marginTop="34dp"
            android:background="@drawable/default_user" />

        <TextView
            android:id="@+id/nameText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/userImage"
            android:layout_marginLeft="25dp"
            android:layout_toEndOf="@+id/userImage"
            android:layout_toRightOf="@+id/userImage"
            android:text="Name"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/logoutText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/nameText"
            android:layout_alignStart="@+id/nameText"
            android:layout_below="@+id/nameText"
            android:clickable="true"
            android:onClick="logUserOut"
            android:text="logout"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <ImageButton
            android:id="@+id/settingsButton"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_alignTop="@+id/nameText"
            android:layout_marginLeft="25dp"
            android:layout_toEndOf="@+id/nameText"
            android:layout_toRightOf="@+id/nameText"
            android:background="@drawable/settingsicon"
            android:onClick="launchSettingsActivity" />

        <fragment
            android:id="@+id/fragment_currentBalanceInfo"
            android:name="com.pocketcash.pocketcash.CurrentBalanceInfoFragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/userImage"
            android:layout_centerHorizontal="true"
            tools:layout="@layout/fragment_current_balance_info" />

        <ExpandableListView
            android:id="@+id/expandableListView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/fragment_currentBalanceInfo"
            android:layout_centerHorizontal="true"
            android:clickable="true" />

    </RelativeLayout>
</ScrollView>

Answer

Jai Saxena picture Jai Saxena · Jun 15, 2015

After much research on this topic, i could only reach the conclusion that a scrollable object like the expandable listview cant be declared within another scrollable view. However i tweaked around with my java code a little bit for solving this issue during runtime.

    expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            int height = 0;
            for (int i = 0; i < expListView.getChildCount(); i++) {
                height += expListView.getChildAt(i).getMeasuredHeight();
                height += expListView.getDividerHeight();
            }
            expListView.getLayoutParams().height = (height+6)*10;
        }
    });

    // Listview Group collapsed listener
    expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            expListView.getLayoutParams().height = 61;
        }
    });

This way i adjusted the height of the widget dynamically when it was expanded or collapsed. Its not a dependable method and it works on hit and trial so i ultimately had to move this widget to a completely new activity. However this did work flawlessly. But if anyone finds a better answer, please do share.