Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Linkify text in dialog

Hi I've gone through all of the different linkify tutorials I could find but none of them work here is my current code:

final SpannableString s = new SpannableString("Please send any questions to [email protected]");
            Linkify.addLinks(s, Linkify.EMAIL_ADDRESSES);
            AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
            builder.setTitle("Warning!")
                   .setMessage(s)
                   .setCancelable(false)
                   .setPositiveButton("Accept", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {

                       }
                   })
                   .setNegativeButton("Decline", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            Activity.this.finish();
                       }
                   }).show();

However when I actually run the app it shows the text like blue and underlined as if it were linked but selecting the text doesn't prompt to open the email app. I've also tried with urls and the browser doesn't work is there something that's missing?

Thanks for any help.

like image 702
user577732 Avatar asked Dec 03 '22 07:12

user577732


1 Answers

In order to have a clickable area on dialog you need to use TextView (View) and set autoLink=all in layout file or invoke setAutoLinkMask() method from within the code.

final SpannableString s = new SpannableString("Please send any questions to [email protected]");

//added a TextView       
final TextView tx1=new TextView(this);
tx1.setText(s);
tx1.setAutoLinkMask(RESULT_OK);
tx1.setMovementMethod(LinkMovementMethod.getInstance());

Linkify.addLinks(s, Linkify.EMAIL_ADDRESSES);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Warning!")
  .setCancelable(false)
  .setPositiveButton("Accept", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int id) {
       }
      })
  .setNegativeButton("Decline", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int id) {
               finish();
      }
     })
  .setView(tx1)
  .show();
like image 67
KV Prajapati Avatar answered Dec 18 '22 19:12

KV Prajapati