Skip to content Skip to sidebar Skip to footer

How Can I Make My Webview Load A Url Shared From Another App?

I'm new to developing, for my first app I'm trying to make a web browser. My problem is I cant get my webview to load a url from a shared Intent. All I get is a blank page. I've sp

Solution 1:

you can do something like this

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns: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"app:layout_behavior="@string/appbar_scrolling_view_behavior"tools:context="org.sakaiproject.sakai.WebViewActivity" ><WebViewandroid:id="@+id/webview"android:layout_width="match_parent"android:layout_height="match_parent" /><ProgressBarandroid:id="@+id/webview_progressbar"style="?android:attr/progressBarStyleHorizontal"android:layout_width="match_parent"android:layout_height="4dp"android:layout_alignParentTop="true"android:focusable="false" /></RelativeLayout>

Now on the Activity you must get the url with getDataString()

publicclassWebViewActivityextendsAppCompatActivity {
    privateWebView webView;
    privateProgressBar progressBar;
    privateString url = null;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        // get the url
        url = getIntent().getDataString();
        progressBar = (ProgressBar) findViewById(R.id.webview_progressbar);
        webView = (WebView) findViewById(R.id.webview);
        webView.setWebChromeClient(newWebChromeClient() {

            @OverridepublicvoidonProgressChanged(WebView view, int progress) {
        // update progressbar
                progressBar.setProgress(progress);
            }
        });
        webView.setWebViewClient(newWebClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(url);
    }

    @OverridepublicvoidonBackPressed() {
        if (webView.canGoBack())
            webView.goBack();
        elsesuper.onBackPressed();
    }

    publicclassWebClientextendsWebViewClient {
        @OverridepublicvoidonPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @OverridepublicvoidonPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
        }

        @OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
            // return super.shouldOverrideUrlLoading(view, url);
            progressBar.setVisibility(View.VISIBLE);
            view.loadUrl(url);
            returnfalse;
        }

    }
}

and on the manifest

<intent-filter><actionandroid:name="android.intent.action.VIEW" /><categoryandroid:name="android.intent.category.DEFAULT" /><dataandroid:scheme="http" /><dataandroid:scheme="https" /></intent-filter>

Post a Comment for "How Can I Make My Webview Load A Url Shared From Another App?"