Android, Codes and Tips

Just in

12/15/2019

How to add Swipe to Refresh in Android Studio apps

After working and searching for hours and you can't get the solution you are looking for. You are welcome to the home of Android Developer. If you have
being using Android Studio for writing Java apps, Java codes is everything sensitive raging from capital letters, punctuation errors, misspelled of words and plugin updates.

Android Studio comes up with new updates always which depreciated the older once. A code you write today, use it and develop an app may be outdated and depreciated tomorrow. If you copy somebody's code or forked a code from github, there are chances that the code which worked earlier won't work again unless you do serious editing.

In MainActivity Java Class populate

private WebView webview;
    SwipeRefreshLayout mySwipeRefreshLayout;
    private ProgressBar progressBar;

Add in Java file'
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
.
.
.
.
.

/Swipe to refresh functionality
        mySwipeRefreshLayout = this.findViewById(R.id.swipeContainer);
        mySwipeRefreshLayout.setOnRefreshListener(
                new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        webview.reload();
                    }
                }
        );

 Add in activity main XML file
<ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="10dp"
        android:layout_alignParentTop="true"
        android:background="@color/colorPrimaryDark"
        android:indeterminate="false"
        android:max="100"
        android:progress="20"
        android:progressTint="@color/colorAccent"
        app:layout_constraintBottom_toTopOf="@+id/swipeContainer"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

No comments:

Post a Comment