0

I have this line in the strings.xml file:

<string name="backup_help_body">Please watch this video if you did not understand the above steps: www.youtube.com</string>

and the above text in an AlertDialog:

AlertDialog.Builder backupHelpDialog = new AlertDialog.Builder(this);
                backupHelpDialog.setTitle(getString(R.string.backup_help_title));
                backupHelpDialog.setMessage(R.string.backup_help_body);
                backupHelpDialog.setIcon(R.drawable.my_notes);
                backupHelpDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface backupHelpDialog, int witch) {
                        // Do Not Do Anything.
                    }
                });

                backupHelpDialog.show();
                return true;

How can I make the link clickable in the AlertDialog without using a TextView?!

3 Answers3

0

Use HTML formatting :

Example :

<string name="my_link"><a href="http://somesite.com/">Click me!</a></string>

For reference :

Android-Is it possible to add a clickable link into a string resource

Now for making the link as clickable, you need to add the LinkMovementMethod to the corresponding Textview.

To get the related Textview, you need to fetch the mAlert field from AlertDialog using reflection and then findviewbyId(android.R.id.alertTitle)

Reference :

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.0_r1/android/app/AlertDialog.java#AlertDialog.0mAlert

Community
  • 1
  • 1
Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
  • Same problem! I can't click on the link. –  Jul 13 '15 at 20:06
  • I have no any TextView as you can see in my code. I have an ActionBar button, if the user press it, it will shows an AlertDialog. The AlertDialog contains the link which I need to make it clickable. –  Jul 13 '15 at 20:37
  • @MrGlitch Inside alert dialog, they were using a textview to set the title. You need to fetch it through reflection. For the time being the easiest way is to go for the custom alert view – Eldhose M Babu Jul 14 '15 at 05:28
0

I'm not sure you can do without a TextView, I would use a Custom AlertDialog, however you can create a "temporary" TextView within the code to perform what you need, here I leave the url where you have several examples

How can I get clickable hyperlinks in AlertDialog from a string resource?

Community
  • 1
  • 1
0

I think the best and fastest way (what I do) would be to add a custom layout with a textview inside, then add an onClickListener on that textview.

#SuperEasyToDo

AntonioSanchez
  • 327
  • 4
  • 14