1

I am creating an alertdialog, where in the message I put a String. That String come with some text I wish to make them links, so when they are shown in the dialog, the user can click over them and go to other views. Let me show you an example of what I want:

public class Activity extends FragmentActivity{

(...)

    public void myMethod(){
        String message = "Some links: link1, link2, link3";
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
        builder.setTitle("This is a title");
        builder.setMessage(message);
        builder.setCancelable(true);
        builder.setOnCancelListener(onCancelListener);
        builder.create().show();
    }
}

I want to do something to link1, link2 and link3 so when they are shown in the dialog, they appear as links. Then, when the user clicks over link1, a new activity is show; when clicks over link2, another view is shown, and the same with link3.

I wanted to have a onlinkclicklistener or something like that so I capture when the user clicks over the link.

Any ideas of how to do that? I hope you can understand what I want :)

RedDeath
  • 283
  • 4
  • 17

2 Answers2

11
public void myMethod(){
        String link1 = "<a href=\"http://www.google.com\">http://www.google.com</a>";
        String message = "Some links: "+link1+"link1, link2, link3";
        Spanned myMessage = Html.fromHtml(message);


        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("This is a title");
        builder.setMessage(myMessage);
        builder.setCancelable(true);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
        TextView msgTxt = (TextView) alertDialog.findViewById(android.R.id.message);
        msgTxt.setMovementMethod(LinkMovementMethod.getInstance());
    }
maheshsgr
  • 2,422
  • 1
  • 14
  • 11
  • this works excelent. if I change your link1 to ""aName";", is there any posibility that, when I click over "aName" (which is shown as a link), I capture that click instead of opening "chrome" or "Internet" options? (well, actually those options only appears if I actually use a link, not "aName", cause if I use "aName" the App crashes because is not a http link) – RedDeath Mar 04 '15 at 11:24
1

As I said in my comment you should use custom dialog box.

so create a new xml file for layout of your custom dialog box.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:text="firstButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        />

    <Button
        android:id="@+id/button2"
        android:text="SecondButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

The above will be your layout for custom dialog box.

now inside your method where your using AlertDialog.Builder use below code.

public void myMethod(){
final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.custome);
                dialog.setTitle("Title...");

                // set the custom dialog components - text, image and button
                Button button1 = (Button) dialog.findViewById(R.id.button1);
                button1.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getBaseContext(),MyActivity2.class);
                        startActivity(intent);
                    }
                });

                Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                // if button is clicked, close the custom dialog
                dialogButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
            }
}

So this will show your custom dialog box with layout specified in above xml and when you click on button1 it will open MyActivity2 in new view.

for more detail you should check here

eLemEnt
  • 1,741
  • 14
  • 21
  • of course, this is a very good answer, and with some changes this will work. I will look for other simplier suggestions, but if I don't find, I will mark this as answer :) – RedDeath Mar 04 '15 at 11:27
  • @RedDeath no problm buddy but do check the link given in answer. – eLemEnt Mar 04 '15 at 11:28