1

How to make SwipeRefreshLayout wrap_content?

Here is my layout mydialog_fragmet.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/contentLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/appBackgroundColorLight"
        android:orientation="vertical"
        >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:paddingTop="16dp"
            android:scrollbarSize="2dp"
            android:scrollbarStyle="outsideOverlay"
            android:scrollbarThumbVertical="@color/colorAccent"
            />

        <include layout="@layout/view_add_place_button"/>

    </LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>

In result LinearLayout id/contentLayout becomes match_parent. Here is screenshot :

enter image description here

But when I use the layout without SwipeRefreshLayout content is wrap_content: mydialog_fragmet.xml:

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

            <android.support.v7.widget.RecyclerView
                android:id="@+id/placesRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:paddingTop="16dp"
                android:scrollbarSize="2dp"
                android:scrollbarStyle="outsideOverlay"
                android:scrollbarThumbVertical="@color/colorAccent"
                app:maxHeight="300dp"
                />

            <include layout="@layout/view_add_place_button"/>

        </LinearLayout>

Here is right result:

enter image description here

NickUnuchek
  • 11,794
  • 12
  • 98
  • 138

2 Answers2

-1

The solution for this specific behavior is to wrap the view in a parent layout, using a specific dp is only recommended if you want it on that size

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/appBackgroundColorLight"
        android:orientation="vertical">

   <android.support.v4.widget.SwipeRefreshLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/contentLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/appBackgroundColorLight"
        android:orientation="vertical"
        >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:paddingTop="16dp"
            android:scrollbarSize="2dp"
            android:scrollbarStyle="outsideOverlay"
            android:scrollbarThumbVertical="@color/colorAccent"
            />

        <include layout="@layout/view_add_place_button"/>

    </LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Rodolfo Abarca
  • 565
  • 7
  • 15
-2

Found solution:

<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="wrap_content"
    android:orientation="vertical"
    >

    <com.example.views.MaxHeightSwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:maxHeight="382dp">//size 382 = 300(RecyclerView) + 82 (addButton)

        <com.example.views.MaxHeightNestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:maxHeight="382dp">

            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/appBackgroundColorLight"
                android:orientation="vertical"
                tools:background="@color/appBlue"
                >

                <com.example.views.MaxHeightRecyclerView
                    android:id="@+id/placesRecyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clipToPadding="false"
                    android:paddingTop="16dp"
                    android:scrollbarSize="2dp"
                    app:maxHeight="300dp"
                    android:scrollbarStyle="outsideOverlay"
                    android:scrollbarThumbVertical="@color/colorAccent"
                    />

                <include layout="@layout/view_add_place_button"/>

            </LinearLayout>
        </com.example.views.MaxHeightNestedScrollView>

    </com.example.views.MaxHeightSwipeRefreshLayout>

</LinearLayout>

Where MaxHeight* view is from https://stackoverflow.com/a/48728490/2425851

NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
  • Settings a static db value is not a very reliable solution. The size of 1db changes with dpi of screen device. – Iaroslav Ternovyi Jul 10 '19 at 15:41
  • @Iaroslav Ternovyi The size of 1dp changes in number of px in different pixels density phones, dp exists for this: have same size in different screen densities. So it isn't wrong to use "dp static values", it is wrong to use them to replace a "wrap_content" or a "match_parent" expected behavior. – Z3R0 Feb 28 '20 at 14:39