I have this code in my app:
public class Home extends Activity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
final ProgressDialog progressBar;
if(isOnline()){
WebView webView = (WebView) findViewById(R.id.home_web);
webView.setBackgroundColor(Color.parseColor(getString(R.color.colore_bg)));
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setPluginsEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
progressBar = ProgressDialog.show(this,getString(R.string.caricamento),getString(R.string.attendere));
webView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
});
webView.loadUrl("http://www.mysite.com/android.php");
}else{
Toast.makeText(this,getString(R.string.no_connessione),Toast.LENGTH_LONG).show();
}
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("here");
if (Uri.parse(url).getHost().equals("mysite.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;
}
}
public boolean isOnline(){
ConnectivityManager cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if(ni==null){
return false;
}
return ni.isConnected();
}
}
The shouldOverrideUrlLoading doesn't work, neither print the system.out, it seems to be never called. How can I repair this? I need to open all the link (except the main page www.mysite.com/iphone.php) in the default browser
You've set the WebViewClient twice, thus replacing the first one (shouldOverrideUrlLoading) with the second one (onPageFinished). Combine the two for it to work:
public class Home extends Activity{
private ProgressDialog progressBar;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
if(isOnline()){
progressBar = ProgressDialog.show(this,getString(R.string.caricamento),getString(R.string.attendere));
WebView webView = (WebView) findViewById(R.id.home_web);
webView.setBackgroundColor(Color.parseColor(getString(R.color.colore_bg)));
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setPluginsEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl("http://www.mysite.com/android.php");
}else{
Toast.makeText(this,getString(R.string.no_connessione),Toast.LENGTH_LONG).show();
}
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
if (progressBar != null && progressBar.isShowing()) {
progressBar.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("here");
if (Uri.parse(url).getHost().equals("mysite.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;
}
}
}
(Please ignore the bad formatting :p)
Related
I am able to load the website properly in the app but it has a function which redirects users to whatsapp. The api works fine on mobile browser and on PC/laptop. But in the android app it loads for a second and then says webpage unavailable. What am I missing?
Image 1 stays only for a second.
After 1 second loading time :
Main Java Code:
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView=(WebView) findViewById(R.id.webview);
mywebView.loadUrl("https://zzzz/");
WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebView.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed(){
if(mywebView.canGoBack()) {
mywebView.goBack();
}
else{
super.onBackPressed();
}
}
}
Override this shouldOverrideUrlLoading and do this in it
Code
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if(url.startsWith("tel:") || url.startsWith("whatsapp:")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
});
Your code should be like this
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView=(WebView) findViewById(R.id.webview);
mywebView.loadUrl("https://naturesexpress.in/");
WebSettings webSettings=mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if(url.startsWith("tel:") || url.startsWith("whatsapp:")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
});
}
I am trying to build a Webview app using android studio and I am currently having a problem. The index page shows perfectly on the app but whenever i click a hyperlink it redirects me to the browser and then to the website how can I get it to redirect me to another page whilst in the app built from webview. Here is a snippet of the main activity java
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView = (WebView) findViewById(R.id.webview);
WebSettings websettings=mywebView.getSettings();
mywebView.loadUrl("https://www.cavaapperal.co.za/");
websettings.setJavaScriptEnabled(true);
}
public class myWebClient extends WebViewClient{
#Override
public void onPageStarted (WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public void onBackPressed (){
if (mywebView.canGoBack()) {
mywebView.goBack();
} else{
super.onBackPressed();
}
}
}
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
});
WebView link click open default browser
Kindly see the following link
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 want to do an app that is mainly base on webview. MainActivity is to load www.example.com/products.php.
www.example.com/products.php - display all products*
www.example.com/products.php?id=123 - display individual product base on product ID
www.example.com/cart.php - when clickcing on "Add to cart" on product.php?id=123, it will not open a new Activity but remain in the same activity.
MainActivity.java
private WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView) findViewById(R.id.webView);
WebSettings wss = wv.getSettings();
wss.setJavaScriptEnabled(true);
wv.loadUrl("http://www.example.com/product.php");
wv.setWebViewClient(new WebViewClient());
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com/cart.php")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}else{
// 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;
}
}
}
#Override
public void onBackPressed() {
if(wv.canGoBack()){
wv.goBack();
}else{
super.onBackPressed();
}
}
And also for "shouldOverrideUrlLoading " , it shows depreciated... so what's the new name for this?
I utilize the facebook html like for my app to open a webview like this https://developers.facebook.com/docs/plugins/like-button/
The webview shows a Like and Share button, but after I login to facebook, it doesnt return to the Like and Share button, but a blank page, the share button works fine.
So how do I return to the facebook like url after logging in?
public class LikeFacebookActivity extends BaseActivity {
private WebView webView;
private final String URL = "facebookIDhere";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.like_facebook_webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
showLoading();
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(URL);
ActionBar actionbar = getActionBar();
actionbar.setCustomView(R.layout.actionbar_top_like_facebook);
actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Button backButton = (Button) findViewById(R.id.buttonGeneralBack);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
#Override
public void onBackPressed() {
finish();
overridePendingTransition(R.anim.animation_slide_from_left,
R.anim.animation_slide_to_right);
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("something")) return true;
return false;
}
public void onPageFinished(WebView view, String url) {
hideLoading();
}
}
}
I have solved this, just use system.out.println to see which page does facebook load after loggin in
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("something")) return true;
return false; //Default is to not override unless our condition is met.
}
public void onPageFinished(WebView view, String url) {
hideLoading();
//String webUrl = webView.getUrl();
//System.out.println(webUrl);
if(url.startsWith("https://www.facebook.com/plugins/close_popup.php#_=_")){
String redirectUrl = URL;
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}
}