Click here to Skip to main content
15,125,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm developing a weather app and i'm done with networking, it can search and display the data for any city. It also displays icons as well. I'm trying to add a swipe-refresh layout to the xml in fragment so that it can refresh the app and display the current data. But when i checked the official doc for it Adding Swipe-to-Refresh To Your App  |  Android Developers[^] 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 used either of those, i only use constraint layout and nestedscrollview, so I tried adding the SwipeRefreshLayout both as parent and child of my constraint layout and it failed. In both cases, it overlapped the full screen which is totally not how i want it to be.

Is there any possibility of adding it to my layout with the current description?

Here's the code:

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>


What I have tried:

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


as parent and child of constraintlayout but it failed to work
Posted

1 solution

Try Something Like This

XML
<pre><LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <include layout="@layout/sub_report_content"/>
            </androidx.core.widget.NestedScrollView>
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    </LinearLayout>

Where The Include tag above provides The XML File To Be Refreshed Which is as Follows
<pre lang="XML"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:padding="3dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_app_sub_report">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </RelativeLayout>
</RelativeLayout>

So make sure you create two xml. One to provide the swiping using the swiperefreshlayout widget and the other is the one that you have which should be called from the first Xml Using the include tag as shown above.
   
Comments
Chinedum Ezeozue 29-Nov-21 17:33pm
   
Thanks. I have fixed it since by the way

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900