I am not able to insert a fragment in a tab android, would help when open, nothing appears is blank –
public class AndroidMobileAppSampleActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mainWebView.loadUrl("http://mobile-sample-app.heroku.com");
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Correct code:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// view.loadUrl(url); REMOVE!!!
return false;
}
Related
I'm trying to listen to url changes from a WebView client and just log the updates. I'm navigating through my webView and shouldOverrideUrlLoading() is never called. I don't know why.
Here is my code :
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Intent i = getIntent();
String url= i.getStringExtra("url");
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String currentUrl = url;
Log.i("currentUrl",": " + currentUrl);
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
}
}
public class WebViewActivity extends AppCompatActivity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
webview = new WebView(this);
webview.setWebViewClient(new MyWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
setContentView(webview);
}
private class MyWebViewClient extends WebViewClient {
public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
return true;
}
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://stackoverflow.com")) {
return false;
}
return true;
}
}
I want to block an url in the WebView, how to block the url? This code doesn't work.
You should return true if you want specific behavior according to documentation.
Returns true if the host application wants to leave the current
WebView and handle the url itself, otherwise return false.
block all the urls working solution even i tried it.
public class MainActivity extends Activity {
private WebView wv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wv1 = new WebView(this);
setContentView(wv1);
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl("http://www.kart.epizy.com");
wv1.setWebViewClient(new MyWebViewClient());
wv1.setClickable(true);
}
public class MyWebViewClient extends WebViewClient {
public boolean shuldOverrideKeyEvent(WebView view, KeyEvent event) {
// Do something with the event here
return true;
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//return false to allow urls or add your condition like
//return url.equals("https://play.google.com/store/apps/developer?id=kArt+Developers");
return true;
}
} }
I have a class which extends WebViewFragment and what I want to achieve is to be able to load other URL from another class into this WebViewFragment class. Here is how my code looks like:
public class FragWeber extends WebViewFragment {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
WebView webView = getWebView();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.google.com");
}
}
Any idea how to access the WebView to load other URL?
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String url1 = url; // can add your URL address
view.loadUrl(url);
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
}
});
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 am opening a URL in web view. I want to prevent it from opeing in web browser. User may click anything it should be with in that web view. Can set that ? How ?
public class AcDetails extends Activity {
private String url;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acdetails);
url = getIntent().getExtras().getString("AC");
getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
WebView wv = (WebView) findViewById(R.id.webac);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(url);
}
}
No need to write a new class, just do it in-line:
WebView wv = (WebView) findViewById(R.id.webac);
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(url);
http://developer.android.com/resources/tutorials/views/hello-webview.html
You have to write a new class, which should extend the Webviewclient class:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
And change your code to:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acdetails);
url = getIntent().getExtras().getString("AC");
getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
WebView wv = (WebView) findViewById(R.id.webac);
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new HelloWebViewClient());
wv.loadUrl(url);
}
}