Rajesh Rajesh - 1 month ago 22
Android Question

Open Email client in AlertDialog

I want to open the Email client when the text is clicked in the AlertDialogwindow. I used Setspan method and Linkify but no result. Here my code:

SpannableString s = new SpannableString("contact");
s.setSpan(new URLSpan("http://www.google.com"), 0, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
final TextView tx1 = new TextView(this);
tx1.setText(getString(R.string.locerstatus) +s);
tx1.setAutoLinkMask(RESULT_OK);
tx1.setMovementMethod(LinkMovementMethod.getInstance());

Linkify.addLinks(s, Linkify.EMAIL_ADDRESSES);
AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) { }
})
.setView(tx1).show();

Answer

Try to use ClickableSpan like this way:

final TextView tx1 = new TextView(this);

tx1.setAutoLinkMask(RESULT_OK);
tx1.setMovementMethod(LinkMovementMethod.getInstance());

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setCancelable(false)
        .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                                        int id) {
                    }
                })
        .setView(tx1);

final AlertDialog dialog = builder.create();

String sourceString = "This is test string with contact word";
String keyWord = "contact";
SpannableString spannableString = new SpannableString(sourceString);

ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View widget) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("plain/text");
        intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.address" });
        intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
        intent.putExtra(Intent.EXTRA_TEXT, "mail body");
        startActivity(Intent.createChooser(intent, ""));
        dialog.dismiss();
    }
};
spannableString.setSpan(clickableSpan, sourceString.indexOf(keyWord), sourceString.indexOf(keyWord) + keyWord.length(), 0);

tx1.setText(spannableString);

dialog.show();

Update

If You want to change color of clickable text You should override also updateDrawState() method of ClickableSpan(), like this:

ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View widget) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("plain/text");
        intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.address" });
        intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
        intent.putExtra(Intent.EXTRA_TEXT, "mail body");
        startActivity(Intent.createChooser(intent, ""));
        dialog.dismiss();
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setColor(Color.BLUE);
        ds.setUnderlineText(true);
    }
};