Issue
I have a webview android app and I have whatsapp option on the website which I am using inside the webview but whatsapp link is not wokring I am new to android development I already tired so many other ways from the google and stackoverflow but still problem not fixed can anybody please help ?
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
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://mywebsite.com");
webSettings.setJavaScriptEnabled(true);
mywebView.setWebViewClient(new WebViewClient());
}
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();
}
}
}
Solution
This problem I fixed by myself here is the code I used to fix this issue.
Here is the solution:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url == null) {
return false;
}
if (url.startsWith("market://")) {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
if (url.startsWith("https://api")) {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
if (url.startsWith("mailto:")) {
try {
List<String> to = new ArrayList<String>();
List<String> cc = new ArrayList<String>();
List<String> bcc = new ArrayList<String>();
String subject = null;
String body = null;
url = url.replaceFirst("mailto:", "");
String[] urlSections = url.split("&");
if (urlSections.length >= 2) {
to.addAll(Arrays.asList(urlSections[0].split(",")));
for (int i = 1; i < urlSections.length; i++) {
String urlSection = urlSections[i];
String[] keyValue = urlSection.split("=");
if (keyValue.length == 2) {
String key = keyValue[0];
String value = keyValue[1];
value = URLDecoder.decode(value, "UTF-8");
if (key.equals("cc")) {
cc.addAll(Arrays.asList(url.split(",")));
}
else if (key.equals("bcc")) {
bcc.addAll(Arrays.asList(url.split(",")));
}
else if (key.equals("subject")) {
subject = value;
}
else if (key.equals("body")) {
body = value;
}
}
}
}
else {
to.addAll(Arrays.asList(url.split(",")));
}
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("message/rfc822");
String[] dummyStringArray = new String[0]; // For list to array conversion
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, to.toArray(dummyStringArray));
if (cc.size() > 0) {
emailIntent.putExtra(android.content.Intent.EXTRA_CC, cc.toArray(dummyStringArray));
}
if (bcc.size() > 0) {
emailIntent.putExtra(android.content.Intent.EXTRA_BCC, bcc.toArray(dummyStringArray));
}
if (subject != null) {
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
}
if (body != null) {
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
}
view.getContext().startActivity(emailIntent);
return true;
}
catch (UnsupportedEncodingException e) {
/* Won't happen*/
}
}
else {
view.loadUrl(url);
return true;
}
return false;
}
Answered By - Mubashar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.