0

I am using android:autoLink="web" in my TextViews to convert URLs into clickable links. This works great. Since the links are user-generated I want to ask the user with a dialog beforehand, whether they really want to openthis link.

I haven't found anything, is there a way to intercept that click and show a dialog before forwarding it to the typical ACTION_VIEW intent?

Mahoni
  • 7,088
  • 17
  • 58
  • 115

1 Answers1

0

Try add to your TextView properties like:

<TextView
    android:text="http://www.google.com"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:onClick="onUrlClick"
    android:linksClickable="false"
    android:clickable="true"
    />

and then override the onClick method like:

    public void onUrlClick(final View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            TextView myTextView = (TextView)view;
            String myUrl = String.valueOf(myTextView.getText());
            Intent browse = new Intent( Intent.ACTION_VIEW , Uri.parse(myUrl) );
            startActivity( browse );
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
}

It worked for me. It is only example, for good practice you should separate the creation from onClick method.

Androj
  • 34
  • 3