Issue
I'm trying to create both links to open internally within the my app's WebView and others which open externally within whichever default browser. I have seen previous questions and answers regarding but none seem to be working and I think the solutions may be outdated?
Any suggestions of how best to achieve this would be appreciated. Opening links within the app is working fine, it's just getting links to open in an external browser where applicable. Maybe some kind of class or url parameter check? I haven't managed to get either to work so far.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
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();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
myWebView.loadUrl("https://website.com/");
myWebView.setWebViewClient(new WebViewClient());
}
@Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}
Solution
You can do it like this
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(wantToOpenExternal){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
String title = "Select a browser";
Intent chooser = Intent.createChooser(intent, title);
startActivity(chooser);
return true;//cancel the current load
}
return false;//continue the current load
}
});
Answered By - rahat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.