Opening webview not in new browser

前端 未结 9 1720
走了就别回头了
走了就别回头了 2020-11-28 09:14

I am implementing a webview. I want two buttons on the top of the web page.

I have one vertical linear layout, inside of which is one horizontal layout with two but

相关标签:
9条回答
  • 2020-11-28 09:18

    The layout should something similar to this:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                     android:layout_height="fill_parent"
                     android:layout_width="fill_parent"
                     android:orientation="vertical">
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <Button android:id="@+id/button1"
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"                
                    android:layout_gravity="top"
                    android:layout_weight="1"/>
                <Button android:id="@+id/button2"
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"                
                    android:layout_gravity="top"
                    android:layout_weight="1" />
            </LinearLayout>
            <WebView 
                android:id="@+id/webview"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>
    </LinearLayout>
    

    And you can also refer to the Need help changing from stock android browser to webview to see if you are launching the url correctly.

    0 讨论(0)
  • 2020-11-28 09:19

    I had the exact same problem and fortunately after browsing the web for about an hour I mixed some of the things I found and it worked.

    this is the code:

    WebView webView;
    webView = ((WebView) rootView.findViewById(R.id.detail_area));
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl(mItem.link);
    

    where "detail_area" was my webview, rootView was my selected item inside a "Master/Detail Flow", link was the URL I wanted to open.

    0 讨论(0)
  • 2020-11-28 09:22

    This might be a late post but it might help other developers...

    You need to set setWebViewClient on webview before loading the URL on it like below,

    webview.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });
    

    Some background of the above code, Documentation states in literals words like below about the shouldOverrideUrlLoading method.

     * @param view The WebView that is initiating the callback.
     * @param request Object containing the details of the request.
     * @return {@code true} if the host application wants to leave the current WebView
     *         and handle the url itself, otherwise return {@code false}.
     */
    @Override
    @SuppressWarnings("deprecation") // for invoking the old shouldOverrideUrlLoading.
    @RequiresApi(21)
    public boolean shouldOverrideUrlLoading(@NonNull WebView view,
            @NonNull WebResourceRequest request) {
        if (Build.VERSION.SDK_INT < 21) return false;
        return shouldOverrideUrlLoading(view, request.getUrl().toString());
    }
    

    If you see the documentation above for returning the value, it says,

    @return {@code true} if the host application wants to leave the current WebView
      *and handle the url itself, otherwise return {@code false}.
    

    So it simply says, If you return true from shouldOverrideUrlLoading method, it'll ask the default browser of your device to handle the request of opening the URL and if you return false, then your URL will be loaded through webview only.

    Now you can load your URL in webview either after this setWebViewClient call or you can also load your URL inside shouldOverrideUrlLoading method before returning the value.

    0 讨论(0)
  • 2020-11-28 09:23

    this code work for me thanks...

        WebView wbView = (WebView) findViewById(R.id.webView);
    
        wbView.getSettings().setJavaScriptEnabled(true);
        wbView.setWebViewClient(new WebViewClient());
        wbView.loadUrl("http://ppid.polinela.ac.id");
    
    0 讨论(0)
  • 2020-11-28 09:26

    Add the below line in your xml file which having webview

    tools:context=".MyActivity" (name of your activity)

    0 讨论(0)
  • 2020-11-28 09:30

    I tried this. its working for me. it does not open new window. it will open webview page only. its hiding for new browser asking window open..

    private WebView webView;
    String strUrl="url" ;
    
    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(strUrl);
    webView.setWebViewClient(new WebViewClient());
    
    0 讨论(0)
提交回复
热议问题