How to enable Addressbar in Android WebView? - android

I´m actually using a WebView in my Application and it works pretty good.
Now I´d like to be able to change the actual URL, just like that Addressbar in the Android Stock browser, where you can see the URL and where you simply can change it.
How can I enable this bar? Or do I have to implement it myself?!
Thanks!
My Code looks like:
private void setupWebView() {
webview = new WebView(this);
webview.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = webview.getSettings();
webSettings.setUserAgentString("foo");
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(false);
webSettings.setBuiltInZoomControls(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
MyActivity.this.setProgress(progress * 100);
}
});
}

Impossible:
The WebView class is an extension of Android's View class that allows
you to display web pages as a part of your activity layout. It does
not include any features of a fully developed web browser, such as
navigation controls or an address bar. All that WebView does, by
default, is show a web page.
http://developer.android.com/guide/webapps/webview.html
If you want to change URL by code:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");

Related

How to remove or disable WebView header?

I'm trying to remove the webview header, I've tried a number of possible solutions but to no avail.
One of my attempts:
android:theme="#android:style/Theme.NoTitleBar"
I'm loading the webview using this code:
setContentView(R.layout.activity_websitenew1);
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.loadUrl("http://www.domain.com");
myWebView .setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
What is happening in your case is that the URL is being handled by Android which is choosing the handler for you. This handler is the system's browser application. What you want is for control to stay within your app.
You can do this in code as follows:
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.domain.com");
See http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView,%20java.lang.String) for details.

URL opened in browser instead of web view

I am using below code to open a link in web view -
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setVisibility(View.VISIBLE);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl(link);
But it opens the link in browser.
I want that the url should only open in my web view. Is there any mistake in the code. Please suggest.
Thanks in advance.
You need to override WebViewClient of your webview and set it. Like it is mentioned in one of the comments on your question.
mWebView.setWebViewClient(new WebViewClient());
And google first, you may not need to post the question.
You should init WebViewClient: mWebView.setWebViewClient(new WebViewClient())
This might help:
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//set URL Here
return false;
}
}

Android - can't seem to enable JavaScript on a WebView?

Is there a way to enable JavaScript in a WebView?
I am currently trying to enable JavaScript by doing this:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
webview = new WebView(this);
setContentView(webview);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(1);
webview.getSettings().setPluginState(PluginState.ON);
WebSettings webSettings = webview.getSettings();
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setUseWideViewPort(true);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
webview.setWebChromeClient(new WebChromeClient(){});
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
webSettings.setDatabaseEnabled(true);
webSettings.setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
webview.loadUrl("http://www.youtube.com/user/Okudjavavich");
}
But JS seems to not be enabled because the YouTube videos do not play YouTube ads. Would anyone know what may be the problem?
Is there a way to enable JavaScript in a WebView?
Yes, via the code that you have:
webview.getSettings().setJavaScriptEnabled(true);
In your case, the problem was that your test (playing YouTube videos and looking for ads) was complicated. A simpler test helped determine that JavaScript indeed was enabled, but that something else was influencing Google's YouTube page-serving. In this case, it appears to be based on user agent.

why my webview is opend in a own browser

I have a android code like this:
WebView myWebView = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://test.com");
the problem is, when I start the app, it is opend in a new browser intend and not in my webView of the app.
how to avoid this?
Add a WebViewClient. This will prevent the default browser to open the URL (or get a selection dialog when there is no default)
myWebView.setWebViewClient(new WebViewClient());
More information can be found on Android website: http://developer.android.com/guide/webapps/webview.html
With the WebViewClient you can do more things, like preventing to load an URL, or change the URL.
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}

Enabling general JavaScript in WebViewClient

While searching for an answer in google, it seems that I'm not the only one stuck with a problem that seems impossible to solve.
I've managed to create a WebView with a custom WebViewClient - this makes it possible for me to have a processdialog and show an error message if an URL couldn't be loaded.
But this creates a problem with JavaScript. The URL I'm loading has some JavaScript which changes some HTML elements CSS styles (showing or hiding element) or redirects to another location onclick - or maybe even want's to show an alert box. But by using the WebViewClient none of those are working.
This is how I load a page:
public void loadUrl(String url)
{
final ProgressDialog dialog = ProgressDialog.show(myActivity.this, "", getString(R.string.loading), true);
final WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setVerticalScrollBarEnabled(false);
myWebView.setHorizontalScrollBarEnabled(false);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
Toast.makeText(myActivity.this, url, Toast.LENGTH_SHORT).show(); //Debugging purposes
if (url.endsWith(".mp4"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "video/mp4");
view.getContext().startActivity(intent);
}
else
{
view.loadUrl(url);
}
return true;
}
public void onPageFinished(WebView view, String url)
{
//Toast.makeText(myActivity.this, "Oh no!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
Toast.makeText(myActivity.this, description, Toast.LENGTH_SHORT).show();
String summary = "<html><body><strong>" + getString(R.string.lost_connection) + "</body></html>";
myWebView.loadData(summary, "text/html", "utf-8");
}
}); //End WebViewClient
myWebView.loadUrl(url);
}
This could probably be done in a smarter way, but I'm new at both Java and Android development...
Is it possible for me to enable the JavaScript for the WebViewClient at all?
Removing the WebViewClient solves the problem, but then I can't catch events when the page errors or is finished loading.
I don't know what your exact problem is, but i can enable the JavaScript and a custom WebViewclient without any problem:
WebView vistaWeb = (WebView) findViewById(R.id.webview);
vistaWeb.setWebChromeClient(new MyCustomChromeClient(this));
vistaWeb.setWebViewClient(new MyCustomWebViewClient(this));
vistaWeb.clearCache(true);
vistaWeb.clearHistory();
vistaWeb.getSettings().setJavaScriptEnabled(true);
vistaWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
The proper way to enable JavaScript is by add the below two lines:
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
Even if after adding its not working then try adding the below line.
mWebView.getSettings().setDomStorageEnabled(true);
Now It should work. :)
Try this to enable javascript
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl(url);
What happened in my case : I was serving local html files and when applying
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
but then my urls stopped working. Solution to make both JS and local href work was to add
web.getSettings().setAllowFileAccess(true);
web.getSettings().setAllowFileAccessFromFileURLs(true);
where
web = (WebView) findViewById(R.id.embedded_web);
-> Hello Please Try This Code This Code Run Fine
-> For More Information of Documentation Please Visit https://developer.android.com/guide/webapps/webview#java
-> This Link Work Fine in Nov 2019 Currently I Don't Know
-> Your XML Code For Design is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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"
android:orientation="vertical"
tools:context=".MainActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
-> And Your Java Code For Backend is
package com.developer.harshil.kaneria.webview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("https://github.com/Harshil-Kaneria");
}
}
-> Here in Java Code
WebSettings webSettings = myWebView.getSettings();
And This
webSettings.setJavaScriptEnabled(true);
-> It is Allow To JavaScript Run When Page is Load
-> Thanks A Lot For Read.
Do "Javascript-URLs" get routed through shouldOverrideUrlLoading? Try checking that and if so, return false for links like that (so that the webview handles the link, not your WebViewClient)
Similar to #mdelolmo answer, but in Kotlin:
webview.setWebChromeClient(WebChromeClient())
webview.setWebViewClient(WebViewClient())
webview.clearCache(true)
webview.clearHistory()
webview.getSettings().setJavaScriptEnabled(true)
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true)
How enable programmatically answered in other answers. But some javascripts not worked if your webview is in nestedscrollview. Remove it and add webSettings.setJavaScriptEnabled(true);
This code happen to works for me, I hope it can helps you and other developer:
webview = findViewById(R.id.WebView);
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.loadUrl("www.example.com");

Categories

Resources