Lawtonj94
Lawtonj94

Reputation: 439

Android Webview vs Android Chrome Browser

I am trying to load a web page in a web view, I am able to do that, however there is an a button that allows you to upload a file. This button works fine when I load the page in the Chrome app on android and it opens the file browser, how ever on web view nothing happens. is it possible to use a chrome for web view, or some how get this button to trigger the file chooser in from the web view?

I have tried changing the web view options to enable javascript ect, below is the code for the webactivity:

 public class WebActivity extends Activity  {
Button b1;
WebView wv;





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

    b1=(Button)findViewById(R.id.button);

    wv=(WebView)findViewById(R.id.webView);
    //wv1.setWebViewClient(new MyBrowser());

    wv.setWebChromeClient(new WebChromeClient());
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            WebSettings webSettings = wv.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setAppCacheEnabled(true);
            webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
            webSettings.setAllowFileAccess(true);
            wv.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);

            wv.loadUrl("MY WEBSITE URL");
            wv.setWebChromeClient(new WebChromeClient());
            wv.setWebViewClient(new WebViewClient() {

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    // TODO Auto-generated method stub
                    if (url.startsWith("tel:") || url.startsWith("mailto:")) {
                        Intent intent = new Intent(Intent.ACTION_VIEW,
                                Uri.parse(url));
                        startActivity(intent);
                    }else if(url.startsWith("http:") || url.startsWith("https:")) {
                        view.loadUrl(url);
                    }
                    return true;
                }
            });

        }
    });
}

}

Upvotes: 2

Views: 4651

Answers (2)

Jenisha Makadiya
Jenisha Makadiya

Reputation: 832

Try the below code.

 WebView wv;
    WebSettings webSettings = wv.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setAllowFileAccess(true);
    wv.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);

    wv.loadUrl("YOUR URL HERE");
            wv.setWebViewClient(new WebViewClient() {

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    // TODO Auto-generated method stub
                    if (url.startsWith("tel:") || url.startsWith("mailto:")) { 
                        Intent intent = new Intent(Intent.ACTION_VIEW,
                                Uri.parse(url)); 
                        startActivity(intent); 
                    }else if(url.startsWith("http:") || url.startsWith("https:")) {
                        view.loadUrl(url);
                    }
                    return true;
                }
            });

    wv.setWebChromeClient(new WebChromeClient());

Hope this will help you.

Upvotes: 1

somerandomusername
somerandomusername

Reputation: 2023

There are some options that are not enabled by default, most likely you don't have javascript.

WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

Upvotes: 0

Related Questions