user577732
user577732

Reputation: 4056

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.

Upvotes: 4

Views: 3831

Answers (3)

devgianlu
devgianlu

Reputation: 1580

Alternatively, you can reuse the TextView created.

AlertDialog.Builder builder;

builder.setMessage(R.string.yourMessage);

Dialog dialog = builder.create();
dialog.setOnShowListener(d -> {
    TextView text = dialog.getWindow().findViewById(android.R.id.message);
    text.setAutoLinkMask(Linkify.ALL);
    text.setMovementMethod(LinkMovementMethod.getInstance());
});

Upvotes: 0

Ben
Ben

Reputation: 3814

Here's Kotlin in case it's helpful:

val s = SpannableString(getString(R.string.actions_list_info_button_body))
val tx1 = TextView(context!!)
tx1.text = s
tx1.autoLinkMask = RESULT_OK
tx1.movementMethod = LinkMovementMethod.getInstance()

The rest is the same.

The result likely will not look great so you probably will want to add some padding as well:

// Adjust Padding to dp
val scale: Float = resources.displayMetrics.density
val dpAsPixels: Int = (25 * scale + 0.5f).toInt()
text.setPadding(dpAsPixels,20,dpAsPixels,0)

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94653

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();

Upvotes: 17

Related Questions