I have a screen which loads a webview, now for handling opening of external apps and other links, I've used url.startsWith() and url.contains(). These work fine for the webview. There is a home button that also uses the same functionality of the webview for handling external links.
My problem
There is also a share button on the top toolbar, the problem is when that button is clicked, the links aren't handled anymore by the above methods even though I have used them in the share button.
Here is the code for handling links inside the webview which works for onCreate and homeIcon methods but not for shareText():
public class MainActivity extends AppCompatActivity {
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("https://google.com");
Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
webSettings.setDefaultTextEncodingName("utf-8");
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && (url.startsWith("whatsapp://") || url.startsWith("tel") || url.startsWith("market"))) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
assert url != null;
if (url.startsWith("intent://") && url.contains("scheme=http")) {
url = Uri.decode(url);
String bkpUrl = null;
Pattern regexBkp = Pattern.compile("intent://(.*?)#");
Matcher regexMatcherBkp = regexBkp.matcher(url);
if (regexMatcherBkp.find()) {
bkpUrl = regexMatcherBkp.group(1);
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://" + bkpUrl));
startActivity(myIntent);
return true;
} else {
return false;
}
}
return false;
}
}
});
}
@SuppressLint("SetJavaScriptEnabled")
public void shareText(View view) {
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("https://google.com");
Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
webSettings.setDefaultTextEncodingName("utf-8");
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && (url.startsWith("whatsapp://") || url.startsWith("tel") || url.startsWith("market"))) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
assert url != null;
if (url.startsWith("intent://") && url.contains("scheme=http")) {
url = Uri.decode(url);
String bkpUrl = null;
Pattern regexBkp = Pattern.compile("intent://(.*?)#");
Matcher regexMatcherBkp = regexBkp.matcher(url);
if (regexMatcherBkp.find()) {
bkpUrl = regexMatcherBkp.group(1);
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://" + bkpUrl));
startActivity(myIntent);
return true;
} else {
return false;
}
}
return false;
}
}
});
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
String shareBodyText = "Check out this awesome store at https://showroom.dotpe.in/stylenstyle882. Or download the app now.";
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject/Title");
intent.putExtra(android.content.Intent.EXTRA_TEXT, shareBodyText);
startActivity(Intent.createChooser(intent, "Sharing Option"));
}
@SuppressLint("SetJavaScriptEnabled")
public void homeIcon(View view){
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("https://google.com");
Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
webSettings.setDefaultTextEncodingName("utf-8");
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && (url.startsWith("whatsapp://") || url.startsWith("tel") || url.startsWith("market"))) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
assert url != null;
if (url.startsWith("intent://") && url.contains("scheme=http")) {
url = Uri.decode(url);
String bkpUrl = null;
Pattern regexBkp = Pattern.compile("intent://(.*?)#");
Matcher regexMatcherBkp = regexBkp.matcher(url);
if (regexMatcherBkp.find()) {
bkpUrl = regexMatcherBkp.group(1);
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://" + bkpUrl));
startActivity(myIntent);
return true;
} else {
return false;
}
}
return false;
}
}
});
}
How should I handle this? Or the buttons in the toolbar can't handle this?
from How to handle external links in WebView after share button is clicked?
No comments:
Post a Comment