1

What i am trying to do is have a clickable hyperlink in the message displayed by an AlertDialog. My goal is to have an AlertDialog pop up asking if the user will accept or decline the Terms and Conditions similar to the following:

enter image description here

When Clicking on the "Google Play Terms of Service" Hyperlink, I would like to start a new Activity. I have looked on here and I can find how to add a Hyperlink to a website as well as adding hyperlinks for text in a TextView, but i am unable to do this for the AlertDialog. I am looking for either a way to set up the Dialog to have hyperlink text linking to an Activity, or to get the TextView that the message is displayed in (i can manipulate the TextView myself).

I have tried (TextView) getDialog().findViewById(android.R.id.message) to get the message TextView but this returns null.

Any help would be much appreciated!

Roboute Guilliman
  • 233
  • 1
  • 2
  • 14
  • Check this answer, i hope it will help you as well [http://stackoverflow.com/questions/1997328/how-can-i-get-clickable-hyperlinks-in-alertdialog-from-a-string-resource][1] [1]: http://stackoverflow.com/questions/1997328/how-can-i-get-clickable-hyperlinks-in-alertdialog-from-a-string-resource – Rashid Jan 15 '15 at 11:45
  • @user3347738 I have already looked at that answer, but as my question stated, I am looking to link through to an Activity, not a website. – Roboute Guilliman Jan 15 '15 at 11:49

2 Answers2

2

Use AlertDialog.setView() to display a custom layout containing your hyperlink TextView in the message area.

E.g. Make a hyperlink_layout.xml containing a TextView with id = hyperlink_text

It only has to contain what is shown in the message area of the AlertDialog, you don't need buttons/title etc.

Create the AlertDialog something like this (from an Activity):

LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.hyperlink_layout, null);

// get the text view from the view:
TextView hyperlinkText = (TextView)dialogView.findViewById(R.id.hyperlink_text);

// format/set it up how you like...

// handle the click on the hyperlink:
hyperlinkText.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // handle the click, launch the new Activity etc
        }
    });

// create the alert dialog
new AlertDialog.Builder(this)                       
    .setView(dialogView)
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // handle OK
        }
    })
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // handle Cancel
        }
    })
    .show();
samgak
  • 23,944
  • 4
  • 60
  • 82
  • Thanks for this. Is using custom views the only way to do this? I have a custom view but i was looking to use the native AlertDialog. – Roboute Guilliman Jan 15 '15 at 11:51
  • I'm not aware of any other way to do it but that doesn't mean there isn't one. It's still using the native AlertDialog, so you don't have to layout your own buttons/title/icon etc, that's all handled by the AlertDialog class so it's easier than making a custom Dialog from scratch. Plus it will be styled like an AlertDialog – samgak Jan 15 '15 at 11:54
  • No problem, I spent all of yesterday looking for another way and i'm happy to do it this way. I will accept your Answer and thanks again! – Roboute Guilliman Jan 15 '15 at 12:05
  • how can the text contents be forced to be center-aligned? – computingfreak Nov 18 '16 at 09:24
0

It will be better to create this dialog as Activity with transparent background.

QArea
  • 4,955
  • 1
  • 12
  • 22