Handle mailto link in WebView Android example
activity_main.xml<?xml version="1.0" encoding="utf-8"?>MainActivity.java
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/base_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fbfffc"
>
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:textColor="#0084ff"
/>
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {Screenshots
private Context mContext;
private Activity mActivity;
private WebView mWebView;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
mActivity = MainActivity.this;
mWebView = findViewById(R.id.web_view);
mTextView = findViewById(R.id.text_view);
Toast.makeText(mContext,"API LEVEL: "+Build.VERSION.SDK_INT,Toast.LENGTH_SHORT).show();
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
Toast.makeText(mContext, "Old Method",Toast.LENGTH_SHORT).show();
if(url.startsWith("http")){
Toast.makeText(mContext,"Page link",Toast.LENGTH_SHORT).show();
return false;
}else if(url.startsWith("mailto")){
handleMailToLink(url);
return true;
}
return false;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
Toast.makeText(mContext, "New Method",Toast.LENGTH_SHORT).show();
String url = request.getUrl().toString();
if(url.startsWith("http")){
Toast.makeText(mContext,"Page link",Toast.LENGTH_SHORT).show();
return false;
}else if(url.startsWith("mailto")){
handleMailToLink(url);
return true;
}
return false;
}
});
mWebView.loadUrl("http://www.rapidtables.com/web/html/mailto.htm");
}
protected void handleMailToLink(String url){
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:"));
mTextView.setText("");
String to = url.split("[:?]")[1];
if(!TextUtils.isEmpty(to)){
String[] toArray = to.split(";");
intent.putExtra(Intent.EXTRA_EMAIL,toArray);
mTextView.append("TO : " + to);
}
if(url.contains("cc=")){
String cc = url.split("cc=")[1];
if(!TextUtils.isEmpty(cc)){
cc = cc.split("&")[0];
String[] ccArray = cc.split(";");
intent.putExtra(Intent.EXTRA_CC,ccArray);
mTextView.append("\nCC : " + cc );
}
}else {
mTextView.append("\n" + "No CC");
}
if(url.contains("bcc=")){
String bcc = url.split("bcc=")[1];
if(!TextUtils.isEmpty(bcc)){
bcc = bcc.split("&")[0];
String[] bccArray = bcc.split(";");
intent.putExtra(Intent.EXTRA_BCC,bccArray);
mTextView.append("\nBCC : " + bcc );
}
}else {
mTextView.append("\n" + "No BCC");
}
if(url.contains("subject=")){
String subject = url.split("subject=")[1];
if(!TextUtils.isEmpty(subject)){
subject = subject.split("&")[0];
try{
subject = URLDecoder.decode(subject,"UTF-8");
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
intent.putExtra(Intent.EXTRA_SUBJECT,subject);
mTextView.append("\nSUBJECT : " + subject );
}
}else {
mTextView.append("\n" + "No SUBJECT");
}
if(url.contains("body=")){
String body = url.split("body=")[1];
if(!TextUtils.isEmpty(body)){
body = body.split("&")[0];
try{
body = URLDecoder.decode(body,"UTF-8");
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
intent.putExtra(Intent.EXTRA_TEXT,body);
mTextView.append("\nBODY : " + body );
}
}else {
mTextView.append("\n" + "No BODY");
}
if(!TextUtils.isEmpty(to)){
if(intent.resolveActivity(getPackageManager())!=null){
// Finally, open the mail client activity
startActivity(intent);
}else {
Toast.makeText(mContext,"No email client found.",Toast.LENGTH_SHORT).show();
}
}
}
}