2

I have an activity which contains text and a fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context="bla.HomeActivity">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="A Text"
                android:id="@+id/home_text_view"/>
            <fragment
                android:name="bla.CalendarAgendaFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/cf"
                tools:layout="@layout/fragment_calendar_agenda" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

The fragment only contains a ListView which is filled dynamically:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/calendar_agenda_listview"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

The onCreateView method of the fragment looks like this:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_calendar_agenda, container, false);

    agenda_dates = new ArrayList<CalendarDate>();
    agenda_dates.add(new CalendarDate("10.10.2015", "Kuchen backen"));
    agenda_dates.add(new CalendarDate("10.10.2015", "Fenster putzen"));

    list = (ListView) v.findViewById(R.id.calendar_agenda_listview);
    adapter = new AgendaAdapter(getActivity(), R.layout.calendar_agenda_listview_item, agenda_dates);

    list.setAdapter(adapter);

    return v;
}

The code works and both CalendarDates are added to the fragment.

The problem is that the fragment is only about 40dp high and so only the first ListView item is shown. I can only see the second item by inline scrolling inside of the list view. How can I set the fragment's hight according to it's content?

YMMD
  • 3,730
  • 2
  • 32
  • 43
  • The code is incomplete above, there is Scrollview, +1 LinearLayout in first xml? What are orientatioins of linear layout – Jemshit Dec 12 '15 at 09:43

2 Answers2

2

You can set the height programatically, as seen here

Function:

public static boolean setListViewHeightBasedOnItems(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter != null) {

        int numberOfItems = listAdapter.getCount();

        // Get total height of all items.
        int totalItemsHeight = 0;
        for (int itemPos = 0; itemPos < numberOfItems, itemPos++) {
            View item = listAdapter.getView(itemPos, null, listView);
            item.measure(0, 0);
            totalItemsHeight += item.getMeasuredHeight();
        }

        // Get total height of all item dividers.
        int totalDividersHeight = listView.getDividerHeight() * 
                (numberOfItems - 1);

        // Set list height.
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalItemsHeight + totalDividersHeight;
        listView.setLayoutParams(params);
        listView.requestLayout();

        return true;

    } else {
        return false;
    }

}
rafaelc
  • 57,686
  • 15
  • 58
  • 82
0

The problem is that the ListView doesn't work correctly with wrap_content. See this question about it: Android: wrap_content is not working with ListView

You should change the size of your ListView programmatically, as Rafael suggested.

Community
  • 1
  • 1
geNia
  • 985
  • 9
  • 20