Progress Bar Added

This commit is contained in:
Sultan Mustafijul Hoque 2023-12-07 13:36:20 +05:30
parent 1cdb111305
commit dff2f691a3
3 changed files with 54 additions and 1 deletions

View File

@ -2,12 +2,16 @@ package com.sultan.plantdatabse;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.annotation.Nullable;
@ -18,6 +22,7 @@ import com.google.zxing.integration.android.IntentResult;
public class WebViewActivity extends AppCompatActivity implements View.OnClickListener {
private ProgressBar progressBar;
Button scanAgainButton;
Button btnHome;
@ -29,6 +34,7 @@ public class WebViewActivity extends AppCompatActivity implements View.OnClickLi
scanAgainButton = findViewById(R.id.scanAgain);
btnHome = findViewById(R.id.btnHome);
progressBar = findViewById(R.id.progressBar);
// Retrieve the URL from the intent
String url = getIntent().getStringExtra("url");
@ -36,7 +42,26 @@ public class WebViewActivity extends AppCompatActivity implements View.OnClickLi
// Load the URL in the WebView
WebView webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// Show the progress bar when the page starts loading
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
// Hide the progress bar when the page finishes loading
progressBar.setVisibility(View.INVISIBLE);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
// Handle page load error
Toast.makeText(WebViewActivity.this, "Failed to load the page", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.INVISIBLE);
}
});
assert url != null;
webView.loadUrl(url);
@ -45,6 +70,16 @@ public class WebViewActivity extends AppCompatActivity implements View.OnClickLi
btnHome.setOnClickListener(this);
}
@Override
public void onBackPressed() {
WebView webView = findViewById(R.id.webView);
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.scanAgain) {

View File

@ -11,6 +11,23 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/lottieAnimationView"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
app:lottie_autoPlay="true"
app:lottie_fileName="loading.json"
app:lottie_loop="true"
android:visibility="invisible" />
<Button
android:id="@+id/scanAgain"
android:layout_width="wrap_content"

File diff suppressed because one or more lines are too long