Add Swipe To Refresh Feature To Your App Using Android Studio
If you haven't watched the video watch it now!
Source Code
Android Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="swiperefresh.com.swipetorefresh">
<uses-permission android:name="android.permission.INTERNET" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package swiperefresh.com.swipetorefresh; import android.graphics.Bitmap; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends AppCompatActivity { private WebView web1; private SwipeRefreshLayout refreshLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); web1 = (WebView)findViewById(R.id.web1); refreshLayout = (SwipeRefreshLayout)findViewById(R.id.refreshLayout); web1.loadUrl("https://google.com"); web1.getSettings().setJavaScriptEnabled(true); web1.setWebViewClient(new WebViewClient()); web1.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap facIcon) { } @Override public void onPageFinished(WebView view, String url) { if(refreshLayout.isRefreshing()){ refreshLayout.setRefreshing(false); } } }); refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { web1.reload(); } }); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout 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" tools:context="swiperefresh.com.swipetorefresh.MainActivity"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/refreshLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/web1" android:layout_width="match_parent" android:layout_height="match_parent"> </WebView> </android.support.v4.widget.SwipeRefreshLayout> </android.support.constraint.ConstraintLayout>
Build.Gradle (Module:app)
apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "swiperefresh.com.swipetorefresh" minSdkVersion 19 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.0' implementation 'com.android.support:design:26.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'}
If you have any doubts comment below
Comments
Post a Comment