Home > front end >  My webview doesn't start after creating progress bar
My webview doesn't start after creating progress bar

Time:11-18

I created my new webapp. It worked well. But I wanted to add a progress bar. The progress bar works, but my webview doesn't show anynore. I don't understand why. Can someone may help, please?

I tried all the codes I found online. They are ok for the others, but not for me.

Here my manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:fullBackupOnly="true"
        android:icon="@drawable/donorionepng"
        android:label="PCDO GENOVA"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.PCDOPCDOGENOVA"
        android:usesCleartextTraffic="true">
        <activity
            android:name=".MainActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:exported="true"/>
        <activity
            android:name=".SplashActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />



                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Here my MainActivity.java

package it.donorionegenova.pcdopcdogenova;

import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.content.pm.ActivityInfo;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.ClientCertRequest;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.core.content.res.ResourcesCompat;
import com.onesignal.OneSignal;
import android.annotation.SuppressLint;

public class MainActivity extends AppCompatActivity {
    private static final String ONESIGNAL_APP_ID = "........ de2";

    private SwipeRefreshLayout swipeRefreshLayout;
    private WebView webView;
    ProgressBar progressBar;

    @SuppressLint("SetJavaScriptEnabled")

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

        ProgressBar progressBar = findViewById(R.id.prBar);


        webView = findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("https://www.donorione-genova.it/home-app");
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                progressBar.setProgress(newProgress);
                if (newProgress == 100) {
                    webView.setVisibility(View.VISIBLE);
                    progressBar.setVisibility(View.GONE);
                } else {
                    webView.setVisibility(View.GONE);
                    progressBar.setVisibility(View.VISIBLE);
                }
            }
        });


        TextView textView = findViewById(R.id.textview);

        textView.postDelayed(() -> textView.setText("loading..."), 500); // delay of 2 seconds before setting a text to textView

        textView.postDelayed(() -> textView.setVisibility(View.GONE), 2000);


        // Enable verbose OneSignal logging to debug issues if needed.
        OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);

        // OneSignal Initialization
        OneSignal.initWithContext(this);
        OneSignal.setAppId(ONESIGNAL_APP_ID);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);


        WebSettings settings = webView.getSettings();

        settings.setJavaScriptEnabled(true);
        settings.setDisplayZoomControls(false);
        settings.supportZoom();
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        // settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        settings.setDomStorageEnabled(true);
        settings.setLoadsImagesAutomatically(true);

        webView.clearHistory();
        webView.clearCache(true);
        registerForContextMenu(webView);


        webView = findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("https://www.donorione-genova.it/home-app");
        webView.setWebViewClient(new WebViewClient() {


            @Override
            public boolean shouldOverrideUrlLoading(WebView View, WebResourceRequest request) {


                final Uri uri = request.getUrl();
                if (uri.toString().startsWith("mailto:")) {
                    //Handle mail Urls
                    startActivity(new Intent(Intent.ACTION_SENDTO, uri));
                } else if (uri.toString().startsWith("tel:")) {
                    //Handle telephony Urls
                    startActivity(new Intent(Intent.ACTION_DIAL, uri));
                } else {
                    //Handle Web Urls
                    webView.loadUrl(uri.toString());
                }
                return true;
            }
        });


        //per fare il refresh

        swipeRefreshLayout = findViewById(R.id.swipe);

        swipeRefreshLayout.setOnRefreshListener(() -> {
            swipeRefreshLayout.setRefreshing(true);
            new Handler().postDelayed(() -> {
                swipeRefreshLayout.setRefreshing(false);
                webView.reload();
            }, 2000);
        });
        swipeRefreshLayout.setColorSchemeColors(
                ResourcesCompat.getColor(
                        getResources(), R.color.holo_blue_dark, null),
                ResourcesCompat.getColor(
                        getResources(), R.color.holo_orange_dark, null),
                ResourcesCompat.getColor(
                        getResources(), R.color.holo_green_dark, null),
                ResourcesCompat.getColor(
                        getResources(), R.color.holo_red_dark, null));

    }
        public void gohome(View v)
    {
        webView.loadUrl("https://donorione-genova.it/home-app");


    }
    public void clickexit(View v)
    {
        moveTaskToBack(true);
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(1);

      }
      public void onBackPressed() {
        if(webView.canGoBack()) {
            webView.goBack();
       }
        else
            super.onBackPressed();
      }


}

And here my ActivityMain:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

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

        <ProgressBar
            android:id="@ id/prBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="?android:attr/progressBarStyleHorizontal" />


        <WebView
            android:id="@ id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:visibility="visible" >

        </WebView>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <Button
        android:id="@ id/button"
        android:layout_width="185dp"
        android:layout_height="wrap_content"
        android:background="#0063B1"
        android:onClick="clickexit"
        android:text="@string/Chiudi"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@ id/button2">

    </Button>

    <Button
        android:id="@ id/button2"
        android:layout_width="185dp"
        android:layout_height="wrap_content"
        android:background="#0063B1"
        android:onClick="gohome"
        android:text="@string/Home"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@ id/button"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toStartOf="parent">

    </Button>

    <TextView
        android:id="@ id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#0000ff"
        android:textSize="40sp"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </TextView>

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

I didn't use SwipeRefreshLayout, but the documentation says you need to include another layout inside it. As you added a second view inside, you are now using it as a default ViewGroup, which is probably not working as expected. Put a LinearLayout or whatever inside the SwipeRefreshLayout and your components inside your LinearLayout.

  • Related