Convert Website Into An Android App | #7 Adding a progress bar

เคนเคฎ เค…เคชเคจे App เค•ो Advance เคฌเคจाเคจे เค•े เคฒिเค เคนเคฎ เค‰เคธเคฎे Progress Bar เคฒเค—ा เคธเค•เคคे เคนैं เคœिเคธเคธे User เคธเคฎเค เคชाเคंเค—े เค•ी Page เค…เคญी Load เคนो เคฐเคนा เคนै!


1. เคจिเคšे เคฆिเค เคนुเค Code เค•ो Image เคฎें เคฆिเค–ाเค เค…เคจुเคธाเคฐ activity_main.xml เคฎें Paste เค•ीเคœिเคฏे!
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="55dp"
android:layout_height="55dp"
android:indeterminate="false"
android:max="100"
android:progress="20"
android:progressTint="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />




2. เคซिเคฐ เคธे เคนเคฎे เคจिเคšे เคฆिเค เคนुเค Code เค•ो Image เคฎे เคฆिเค–ाเค เค…เคจुเคธाเคฐ MainActivity.java เคฎें Paste เค•ीเคœिเคฏे।

private ProgressBar progressBar;

เค…เคฌ เค†เคชเค•ो  เคซिเคฐ เคธे เค‡เคธเค•े เคฒिเค Class Import เค•เคฐเคจा เคชเฅœेเค—ा-




3. เคซिเคฐ เค†เคชเค•ो เคจिเคšे เคฆिเค เคนुเค Code เค•ो Image เคฎे เคฆिเค–ाเค เค…เคจुเคธाเคฐ MainActivity.java เคฎें Paste เค•เคฐเคจा เคนै!
//Progress bar
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);




4. เคซिเคฐ เค†เคชเค•ो เคจिเคšे เคฆिเค เคนुเค Code เค•ो Image เคฎे เคฆिเค–ाเค เค…เคจुเคธाเคฐ MainActivity.java เคฎें เคชเคนเคฒे เคตाเคฒे Code เคธे Replace เค•เคฐเคจा เคนै!
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());
webview.setWebChromeClient(new WebChromeClientDemo());

เค…เคฌ เคนो เคธเค•เคคा เคนै เค†เคชเค•े เคฎे เคญी เคเคธा (เคจिเคšे เคฆिเค–ाเคฏा เค—เคฏा เคนै) Error เคฆिเค–ाเคˆ เคฆेเค—ा! เคคो เค†เคช เค‡เคธे ignor เค•ीเคœिเคฏे Next  Code Paste เค•เคฐเคจे เคชเคฐ เคฏเคน Solve เคนो เคœाเคฏेเค—ा।




5. เค…เคฌ เค†เคชเค•ो เคธिเคฐ्เคซ เคเค• เค”เคฐ Step Complete เค•เคฐเคจा เคนै।
เคซिเคฐ เคธे เค†เคชเค•ो เคจिเคšे เคฆिเค เคนुเค Code เค•ो Image เคฎे เคฆिเค–ाเค เค…เคจुเคธाเคฐ MainActivity.java เคฎें เคชเคนเคฒे เคตाเคฒे Code เคธे Replace เค•เคฐเคจा เคนै-
private class WebViewClientDemo extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
mySwipeRefreshLayout.setRefreshing(false);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}

}
private class WebChromeClientDemo extends WebChromeClient {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
}

เค…เคฌ Class Import เค•เคฐเคจे เค•े เคฒिเค 3 เคธे 4 Error เค†เคเค—ा เคคो เค†เคชเค•ो เคธเคญी Error เคฎे เค…เคชเคจे Mouse pointer เค•ो เคฒे เคœाเคจा เคนै เค”เคฐ Class Implrt เค•เคฐ เคฒेเคจा เคนै, เคจिเคšे Image เคฎे เคธเคญी เค•ो เคฆिเค–ाเคฏा เค—เคฏा เคนै। 



เคธเคญी Class เค•ो Import เค•เคฐเคจे เค•े เคฌाเคฆ เคฎुเคे เค‰เคฎ्เคฎीเคฆ เคนै เค†เคชเค•े เคฎे เค”เคฐ เค•ोเคˆ Error เคจเคนी เค†เคเค—ा।




เคจिเคšे เคฎेเคฐा Final (MainActivity.java) Code เคฆिเคฏा เค—เคฏा เคนै, เค†เคช เคฆेเค– เคธเค•เคคे เคนैं।
package com.example.technicalsangrah;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

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;
private ProgressBar progressBar;

@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());
webview.setWebChromeClient(new WebChromeClientDemo());

}

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

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

//Progress bar
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);

}


private class WebViewClientDemo extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
mySwipeRefreshLayout.setRefreshing(false);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}

}
private class WebChromeClientDemo extends WebChromeClient {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
}

//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

Post a Comment

Popular posts from this blog

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

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

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