-2

There is another question like this, except I am trying to avoid TextView, since I don't have an XML file to accompany this error message. I am trying to redirect my user to a link when he clicks on the hyperlink text. Here is the code that I have so far:

public void messageAlert(){
    String link = "www.google.com";
    new AlertDialog.Builder(this)
    .setTitle("ErrorMessage")
    .setMessage("You have a massive error. Please go to "+ link) // hyperlink here
    .setIcon(R.drawable.ic_action_error)
    .setPositiveButton("Continue", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int n){

            }
    })
    .show();
}

Any help would be greatly appreciated! P.S. I am a beginner to Android

1 Answers1

0

You need to use a href the accepted answer on this link shows a good example. a href example

TextView tv1 = new TextView(this);
tv1.setText(Html.fromHtml("<a href=\"" + "http:\\www.google.com" + "\">" + "Go to google" + "</a>"));
tv1.setClickable(true);
tv1.setMovementMethod(LinkMovementMethod.getInstance());

Just add this TextView to your Dialog and it will show the Go to google as a normal Hyperlink which on click will open a browser and search for http:\www.google.com

Community
  • 1
  • 1
Weschne
  • 51
  • 3
  • How do I add it to my dialog? – Carl Johnson Feb 19 '16 at 22:01
  • You could create a custom dialog with its own layout xml file dialog.xml there you add the textview. (http://developer.android.com/guide/topics/ui/dialogs.html) There you find an example just create first an `View view = inflater.inflate(R.layout.dialog, null);` from your layout file use this to find the textview `TextView tv1 = (TextView) view.findViewById(R.id.textview);` and do the settings after this `builder.setView(view);` and the rest stays the same as in the example. – Weschne Feb 19 '16 at 22:14
  • `public void show() { LayoutInflater layoutInflater = LayoutInflater.from(mActivity); View view= layoutInflater.inflate(R.layout.dialog, null); TextView tv1 = (TextView) view.findViewById(R.id.textview); tv1.setText(Html.fromHtml("" + "Go to google" + "")); tv1.setClickable(true); tv1.setMovementMethod(LinkMovementMethod.getInstance()); final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mActivity); dialogBuilder.setView(view); AlertDialog dialog = dialogBuilder.create(); dialog.show(); }' – Weschne Feb 19 '16 at 22:20
  • Whats `mActivity` and I don't have a dialog – Carl Johnson Feb 19 '16 at 22:49
  • mActivity is an Activity object which you have to declare before calling the function show(). `Activity mActivity;` If you have this function in your activity you can replace "mActivity" with "this" and this will show you a dialog. You only need the dialog.xml file with a TextView declared in it. with the id = textview. – Weschne Feb 19 '16 at 22:56