javaandroidandroid-layoutswiperefreshlayout

How do I add SwipeRefreshLayout to my layout?(Without list/gridview)


I'm developing a weather app and I'm done with the networking(it can search and display the data for any city, and can also display the icons as well). I'm trying to add a swiperefreshlayout to the xml in fragment so that I can refresh the app from time to time and update the app data. But when I checked the official documentation for it i.e https://developer.android.com/training/swipe/add-swipe-interface, they said "You enable this behavior by adding the widget to your layout file as the parent of a ListView or GridView". Meanwhile, I don't use either of those, I only use constraint layout and nestedscrollview, so I tried adding the SwipeRefreshLayout i.e

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/alerts_swiperefresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

both as parent and child of my constraint layout but it covers the whole xml layout and only it is reflecting in both cases. Is there any possibility of adding it to my layout? (Without list/gridview). If there is, please how can I do it? otherwise what other steps should I follow?

Here is the layout:

fragment_first.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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"
    android:orientation="vertical"
    android:fillViewport="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".first.FirstFragment">

        <!-- TODO: Update blank fragment layout -->


        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="@string/current"
            android:textColor="#FF3D19"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="358dp"
            android:layout_height="337dp"
            android:contentDescription="@string/orange_panel"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView3"
            app:srcCompat="@drawable/orange_panel" />

        <TextView
            android:id="@+id/textView14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="68dp"
            android:layout_marginTop="19dp"
            android:text="@string/sunrise"
            android:textColor="#0aff69"
            android:textSize="24sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView3" />

        <ImageView
            android:id="@+id/imageView11"
            android:layout_width="94dp"
            android:layout_height="40dp"
            android:layout_marginTop="19dp"
            android:contentDescription="@string/right_arrow"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView3"
            app:srcCompat="@drawable/right_arrow"
            app:tint="#FFFFFF" />

        <TextView
            android:id="@+id/textView15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="19dp"
            android:layout_marginEnd="76dp"
            android:text="@string/sunset"
            android:textColor="#ff0061"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView3" />

        <ImageView
            android:id="@+id/imageView14"
            android:layout_width="146dp"
            android:layout_height="151dp"
            android:layout_marginStart="45dp"
            android:contentDescription="@string/rise_panel"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView11"
            app:srcCompat="@drawable/rise_panel" />

        <ImageView
            android:id="@+id/imageView15"
            android:layout_width="146dp"
            android:layout_height="151dp"
            android:layout_marginEnd="44dp"
            android:contentDescription="@string/set_panel"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView11"
            app:srcCompat="@drawable/set_panel" />

        <TextView
            android:id="@+id/textView16"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="@string/weather_conditions"
            android:textColor="#4230FF"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView14" />

        <ImageView
            android:id="@+id/imageView16"
            android:layout_width="358dp"
            android:layout_height="337dp"
            android:layout_marginTop="4dp"
            android:contentDescription="@string/blue_panel"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView16"
            app:srcCompat="@drawable/blue_panel" />

        <TextView
            android:id="@+id/textView18"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginStart="53dp"
            android:layout_marginTop="33dp"
            android:text="@string/temperature"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="@+id/imageView16"
            app:layout_constraintTop_toTopOf="@+id/imageView16" />

        <TextView
            android:id="@+id/textView20"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="53dp"
            android:layout_marginTop="24dp"
            android:text="@string/humidity"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView23" />

        <TextView
            android:id="@+id/textView21"
            android:layout_width="0dp"
            android:layout_height="24dp"
            android:layout_marginStart="53dp"
            android:layout_marginTop="24dp"
            android:text="@string/wind_speed"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView20" />

        <TextView
            android:id="@+id/textView22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="53dp"
            android:layout_marginTop="24dp"
            android:text="@string/visibility"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView21" />

        <TextView
            android:id="@+id/textView23"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="53dp"
            android:layout_marginTop="24dp"
            android:text="@string/pressure"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView18" />

        <TextView
            android:id="@+id/textView24"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="53dp"
            android:layout_marginTop="24dp"
            android:text="@string/cloud"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView22" />

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="52dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/imageView3"
            app:srcCompat="@drawable/sun"
            android:contentDescription="@string/current_icon" />

        <TextView
            android:id="@+id/textView10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="@string/current_temp"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="30sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView6" />

        <TextView
            android:id="@+id/textView11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="@string/current_output"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="24sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView10" />

        <ImageView
            android:id="@+id/imageView12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="17dp"
            android:layout_marginTop="26dp"
            app:layout_constraintStart_toStartOf="@+id/imageView14"
            app:layout_constraintTop_toTopOf="@+id/imageView14"
            app:srcCompat="@drawable/small_sun"
            android:contentDescription="@string/rise_sun" />

        <TextView
            android:id="@+id/textView25"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="17dp"
            android:layout_marginBottom="31dp"
            android:text="@string/rise_time"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            app:layout_constraintBottom_toBottomOf="@+id/imageView14"
            app:layout_constraintStart_toStartOf="@+id/imageView14" />

        <ImageView
            android:id="@+id/imageView13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="17dp"
            android:layout_marginTop="26dp"
            app:layout_constraintStart_toStartOf="@+id/imageView15"
            app:layout_constraintTop_toTopOf="@+id/imageView15"
            app:srcCompat="@drawable/small_sun"
            android:contentDescription="@string/set_sun" />

        <TextView
            android:id="@+id/textView26"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="17dp"
            android:layout_marginBottom="31dp"
            android:text="@string/set_time"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            app:layout_constraintBottom_toBottomOf="@+id/imageView15"
            app:layout_constraintStart_toStartOf="@+id/imageView15" />

        <TextView
            android:id="@+id/textView28"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="33dp"
            android:layout_marginEnd="53dp"
            android:text="@string/temp_out"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="@+id/imageView16"
            app:layout_constraintTop_toTopOf="@+id/imageView16" />

        <TextView
            android:id="@+id/textView29"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="53dp"
            android:text="@string/press_out"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView28" />

        <TextView
            android:id="@+id/textView30"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="53dp"
            android:text="@string/humid_out"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView29" />

        <TextView
            android:id="@+id/textView33"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="53dp"
            android:text="@string/ws_out"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView30" />

        <TextView
            android:id="@+id/textView34"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="53dp"
            android:text="@string/visi_out"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView33" />

        <TextView
            android:id="@+id/textView35"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="53dp"
            android:text="@string/cloud_out"
            android:visibility="gone"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="@+id/imageView16"
            app:layout_constraintTop_toBottomOf="@+id/textView34" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

Solution

  • We can add SwipeRefreshLayout without RecyclerView, ListView or GridView

    SwipeRefreshLayout is basically used for refresh the content of a view with the vertical gesture feature.

    Add SwipeRefreshLayout as parent of NestedScrollView if layout has scroll, If layout is not scrollable then just add SwipeRefreshLayout as parent of Layout

    For Example - If screen is scrollable:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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"
        android:orientation="vertical"
        tools:context=".first.FirstFragment">
        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:fillViewport="true">
    
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                 <!--Your sub views-->
            </androidx.constraintlayout.widget.ConstraintLayout>
    
        </androidx.core.widget.NestedScrollView>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    

    For Example - If screen is not scrollable:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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"
        android:orientation="vertical"
        tools:context=".first.FirstFragment">
    
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                 <!--Your sub views-->
            </androidx.constraintlayout.widget.ConstraintLayout>
    
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    

    How to stop refreshing SwipeRefreshLayout:

    You need to add swipeRefreshLayout.setRefreshing(false); to stop refreshing

    For example: I added handler thread in class to stop refreshing of SwipeRefreshLayout

        SwipeRefreshLayout realSwipe = findViewById(R.id.real_swipe);
    
        realSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // perform you action here for ex. add refresh screen code here
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // this code is for stop refreshing icon, After 1000 ms automatically refresh icon will stop
                        realSwipe.setRefreshing(false);
                    }
                }, 1000);
            }
        });