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;
}
});
}
});
}
}
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.
There are some options that are not enabled by default, most likely you don't have javascript.
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
Related
public class MainActivity extends AppCompatActivity {
String url = "https://www.pinterest.com";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
}
});
webView.loadUrl(url);
}
}
Web View is not loading "https://www.pinterest.com" within the application but it was working with chrome browser using intent. I tried of other urls it's working. While loading "https://www.pinterest.com" using webview, getting the output screen like below
You try this:
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowContentAccess(true);
settings.setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.pinterest.com/");
In manifest:
android:hardwareAccelerated="true"
set the JavascriptEnabled and AllowContentAccess and setDomStorageEnabled to true in settings
I have a webview app and facing a problem that
when I click on Hyperlink then it opens in default browser.
But I want to open that link in same webview.
How to open it in same webview.
Here is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.myWebView);
myWebView.loadUrl("file:///android_asset/abc/index.html");
myWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("file:///android_asset/abc/index.html")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Do it like,
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Return false in all cases.
ok.. you have added a WebViewClient also..
then try it in this way...
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
this should work for you..
I make small app which involve aspx pages , I try open it throw my webview I got blank screen .
I try open it with chrome browser it give me certificate needed
my code is very simple I add some documentation to explain what is going on
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
Bundle bundle = getIntent().getExtras();
if(bundle != null){
urlSite = bundle.getString("passed_url");
}
_toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(_toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
// enable javascript
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setPluginState(WebSettings.PluginState.ON_DEMAND);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
new ParseURl().execute(new String[]{urlSite});
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
if(urlSite != null){
webView.clearCache(true);
webView.loadUrl(urlSite);
// webView.loadUrl("file:///android_asset/af.html");
}
}
After some research I found the answer, just override onReceivedSslError method and skip it.
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}
I have a browsable webview but unfortunately when I open a new page inside the webview i loose the login state and have to login again.
here my code:
wv = (WebView) findViewById(R.id.webArea);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
CookieManager.setAcceptFileSchemeCookies(true);
}
CookieSyncManager.createInstance(Home.this);
CookieSyncManager.getInstance().startSync();
dialog = new ProgressDialog(this);
dialog.setMessage("Loading");
dialog.setCanceledOnTouchOutside(false);
dialog.show();
wv.getSettings().setDatabaseEnabled(true);
wv.getSettings().setAllowFileAccess(true);
wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setLoadsImagesAutomatically(true);
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
dialog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
});
wv.loadUrl(url);
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setAllowFileAccess(true);
What's wrong?
Try this to set cookieSyncManager -
CookieSyncManager.createInstance(mWebView.getContext());
If doesn't work, then we will have to check how your URL navigates.
I use WebChromeClient for opening a link to news.html on my server, it opens safari and shows the content, ok.
But...
When I implent WebViewClient shouldOverrideUrlLoading to intercept the call when it's link to a .pdf file (use another class for it) the WebChromeClient link to news.html stays in webview and does not open safari anymore...
I am doing something wrong, but what?
Code snippet: (links are long so I shortened it)
myWebView = (WebView)findViewById(R.id.webView);
myWebView.getSettings().setJavaScriptEnabled(true);
//only to catch url override
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("http://www.domain.nl/pdf")){
Intent i = new Intent();
i.putExtra("url", url);
i.setClassName("nl.domain.domain", "nl.domain.domain.PdfActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return true ;
}
else {
return false ;
}
}
});
myWebView.setWebChromeClient(new WebChromeClient());
//loading
myWebView.loadUrl("http://www.domain.nl/news.html");
return false; means that the url wasn't handled and it should be opened in the WebView. If you want to open the url in an external browser you need to explicitly do it.
myWebView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("http://www.domain.nl/pdf")){
Intent i = new Intent();
i.putExtra("url", url);
i.setClassName("nl.domain.domain", "nl.domain.domain.PdfActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return true ;
}
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url));
return true;
}
});
To open the PDF file url :
add the "http://docs.google.com/viewerembedded=true&url=" string at starting of PDF file url string...
try this it work
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView;
webView = new WebView(this);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setPluginsEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
if (webUrl.equals(XmlFileUrl.MemberDownloadAppForm)) {
webUrl = "http://docs.google.com/viewerembedded=true&url=" + webUrl;
}
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
setContentView(webView);
}
An example that helped me to do something similar Here
For geolocation add:
myWebView.getSettings().setGeolocationEnabled(true);
and :
private class MyWebChromeClient extends WebChromeClient {
.........
#Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback)
{
// callback.invoke(String origin, boolean allow, boolean remember);
callback.invoke(origin, true, false);
}
}