0

I am trying to set a link url and link to start anther activity. here my code, its working with html link but dont know how to do link that will start anther activity.

example of my code:

final AlertDialog d = new AlertDialog.Builder(this)
            .setPositiveButton(android.R.string.ok, null)
            .setMessage(Html.fromHtml(getResources().getString(R.string.infoAuthor)+" <br> <a href=\"https://www.youtube.com">click here for help</a>"))
            .create();
            d.show();
            // Make the textview clickable. Must be called after show()   
            ((TextView)d.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());

if you can give some code how to do it.

Vitaly Menchikovsky
  • 7,684
  • 17
  • 57
  • 89

2 Answers2

0

Try looking at this post. The code the author uses to accomplish this is:

public static class MyOtherAlertDialog {

 public static AlertDialog create(Context context) {
  final TextView message = new TextView(context);
  // i.e.: R.string.dialog_message =>
            // "Test this dialog following the link to dtmilano.blogspot.com"
  final SpannableString s = 
               new SpannableString(context.getText(R.string.dialog_message));
  Linkify.addLinks(s, Linkify.WEB_URLS);
  message.setText(s);
  message.setMovementMethod(LinkMovementMethod.getInstance());

  return new AlertDialog.Builder(context)
   .setTitle(R.string.dialog_title)
   .setCancelable(true)
   .setIcon(android.R.drawable.ic_dialog_info)
   .setPositiveButton(R.string.dialog_action_dismiss, null)
   .setView(message)
   .create();
 }
}

An alternate soultion to the one above from the same post is this:

   // Linkify the message
    final SpannableString s = new SpannableString(msg);
    Linkify.addLinks(s, Linkify.ALL);

    final AlertDialog d = new AlertDialog.Builder(activity)
        .setPositiveButton(android.R.string.ok, null)
        .setIcon(R.drawable.icon)
        .setMessage( s )
        .create();

    d.show();

    // Make the textview clickable. Must be called after show()
    ((TextView)d.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
Community
  • 1
  • 1
Nick
  • 9,285
  • 33
  • 104
  • 147
0

in your layout xml

       <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"           
        android:autoLink="web|phone|email"           
        android:text="" />

web to urls , phone , and emails

You can also change the color android:textColorLink="@color/linkscolor"

inflate your view from xml and set dialog view with setView method

ingyesid
  • 2,864
  • 2
  • 23
  • 21