开发者

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@fake.com");
            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 underline开发者_Python百科d 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.


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@fake.com");

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


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)


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜