Convert Website Into An Android App | #6 Add Swipe down to Refresh functionality

 เคนเคฎ  เค…เคชเคจे App เคฎें Swipe Down เค•เคฐเคจे เคชเคฐ Refresh เคนो เคœाเคฏे เคœैเคธे เค•ी chrome browser เคฏा เคซिเคฐ YouTube เคฎें เคนोเคคा เคนै! เคตैเคธा Function เคฒเค—ा เคธเค•เคคे เคนैं เค‡เคธเค•े เคฒिเค เคจिเคšे Process เคฆिเคฏा เค—เคฏा เคนै-

1. เคธเคฌเคธे เคชเคนเคฒे เค†เคชเค•ो activity_main.xml เคฎें เคœाเคจा เคนै เค”เคฐ เคจिเคšे เคฆिเค เคนुเค Code เค•ो Image เคฎें เคฆिเค–ाเค เคนुเค เคธ्เคฅाเคจ เคชเคฐ Paste เค•เคฐเคจा เคนै!

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

<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
tools:ignore="MissingConstraints" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>


เค…เคฌ เคนो เคธเค•เคคा เคนै เค†เคชเค•े เคฎें เค‡เคธ เคคเคฐเคน เค•ा Error เค† เคœाเคฏे-




- เค†เคชเค•ो Android studio เคฎें Error เคนเคฎेเคธा เคŠเคชเคฐ เคธे Solve เค•เคฐเคจा เคนै

- เค†เคชเค•ो เค…เคชเคจे Mouse เค•ो Error เคตाเคฒे เคธ्เคฅाเคจ เคชเคฐ เคฒे เคœाเคจा เคนै เคซिเคฐ เคจिเคšे เค†เคชเค•ो เค‰เคธเค•ो Solve เค•เคฐเคจे เค•े เคฒिเค Add dependency เค•ा option เค† เคœाเคฏเค—ा (เคจिเคšे Image เคฎें เคฆिเค–ाเคฏा เค—เคฏा เคนै) เค‰เคธเคฎे เค†เคชเค•ो click เค•เคฐเคจा เคนै เคฌเคธ เค†เคชเค•ा Android Studio Internet เคธे connectet เคนोเคจा เคšाเคนिเค, เคซिเคฐ problem solve เคนो เคœाเคฏเค—ा!





2. เค…เคฌ MainActivity.java เคฎे เคœाเคจा เคนै เค”เคฐ เคจिเคšे เคฆिเค เคนुเค Code เค•ो image เคฎें เคฆिเค–ाเค เคนुเค เคธ्เคฅाเคจ เคชเคฐ paste เค•เคฐเคจा เคนै

SwipeRefreshLayout mySwipeRefreshLayout;

เคซिเคฐ เค†เคชเค•ो class import เค•เคฐเคจे เค•े เคฒिเค เคเค• Error เค†เคเค—ा เคคो เคซिเคฐ เคธे เค†เคชเค•ो mouse เค•ो เคตเคนां เคชเคฐ เคฒे เคœा เค•เคฐ class import เค•เคฐ เคฒेเคจा เคนै

เค†เคชเค•ा class เคฏเคนाँ เคชเคฐ Import เคนो เคœाเคฏेเค—ा




3. เค…เคฌ เค†เคชเค•ो เคซिเคฐ เคธे เคจिเคšे เคฆिเค เคนुเค Code เค•ो image เคฎें เคฆिเค–ाเค เคนुเค เคธ्เคฅाเคจ เคชเคฐ paste เค•เคฐ เคฒेเคจा เคนै

//Swipe to refresh functionality
mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);

mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webview.reload();
}
}
);



4. เค…เคฌ เคจिเคšे เคฆिเค เคนुเค code เค•ो เคชเคนเคฒे เคธे เคœो Code เคฅा เค‰เคธเค•े เคธ्เคฅाเคจ เคชเคฐ Image เคฎें เคฆिเค–ाเค เค…เคจुเคธाเคฐ Replace เค•เคฐ เคฆीเคœिเคฏे 

private class WebViewClientDemo extends WebViewClient {
@Override
//Keep webview in app when clicking links
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mySwipeRefreshLayout.setRefreshing(false);
}
}

เค…เคฌ เค†เคช เค…เคชเคจे App เค•ो Test เค•เคฐเค•े เคฆेเค–िเคฏे Swipe down เค•เคฐเคจे เคชเคฐ Refresh เคนोเคจे เคฒเค—ेเค—ा।



เคจिเคšे เคฎेเคฐा Final Main_Activity.java File เคฆिเคฏा เค—เคฏा เคนै-

package com.example.technicalsangrah;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

public class MainActivity extends AppCompatActivity {

String websiteURL = "https://youtube.com/technicalsangrah"; // sets web url
private WebView webview;
SwipeRefreshLayout mySwipeRefreshLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if( ! CheckNetwork.isInternetAvailable(this)) //returns true if internet available
{
//if there is no internet do this
setContentView(R.layout.activity_main);
//Toast.makeText(this,"No Internet Connection, Chris",Toast.LENGTH_LONG).show();

new AlertDialog.Builder(this) //alert the person knowing they are about to close
.setTitle("No internet connection available")
.setMessage("Please Check you're Mobile data or Wifi network.")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
//.setNegativeButton("No", null)
.show();

}
else
{
//Webview stuff
webview = findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webview.loadUrl(websiteURL);
webview.setWebViewClient(new WebViewClientDemo());

}

//Swipe to refresh functionality
mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);

mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webview.reload();
}
}
);

}


private class WebViewClientDemo extends WebViewClient {
@Override
//Keep webview in app when clicking links
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mySwipeRefreshLayout.setRefreshing(false);
}
}

//set back button functionality
@Override
public void onBackPressed() { //if user presses the back button do this
if (webview.isFocused() && webview.canGoBack()) { //check if in webview and the user can go back
webview.goBack(); //go back in webview
} else { //do this if the webview cannot go back any further

new AlertDialog.Builder(this) //alert the person knowing they are about to close
.setTitle("EXIT")
.setMessage("Are you sure. You want to close this app?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}

}

class CheckNetwork {

private static final String TAG = CheckNetwork.class.getSimpleName();

public static boolean isInternetAvailable(Context context)
{
NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

if (info == null)
{
Log.d(TAG,"no internet connection");
return false;
}
else
{
if(info.isConnected())
{
Log.d(TAG," internet connection available...");
return true;
}
else
{
Log.d(TAG," internet connection");
return true;
}

}
}
}

.

Comments

Popular posts from this blog

Convert Website Into An Android App | #10 Set Up Push Notifications

Convert Website Into An Android App | #8 Adding a Splash Screen