0

I'm trying something simple. Include a hyperlink in an AlertDialog message. I can include the hyperlink and it is drawn as a hyperlink as desired. But when clicking on it, the app crashes:

ActivityMain.java

AlertDialog ad = new AlertDialog.Builder(this).create();
        ad.setMessage(Html.fromHtml("<a href=\\\"http://www.google.com\\\">Check this link out</a>));
        ad.setButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        ad.show();
        TextView textView = (TextView) ad.findViewById(android.R.id.message);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
        textView.setMovementMethod(LinkMovementMethod.getInstance());

In my AndroidManifext.xml I added the intend-filter android.intent.action.VIEW, but its still crashing.

        <activity
            android:name="org.app.test"
            android:screenOrientation="portrait"
            android:label="@string/app_name"
            android:theme="@style/AppTheme"
            >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Logcat error message

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=\"http://www.google.com\" (has extras) }
            at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1672)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1442)
            at android.app.Activity.startActivityForResult(Activity.java:3511)
            at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
            at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
            at android.app.Activity.startActivityForResult(Native Method)
            at android.app.Activity.startActivityForResult(Activity.java:3472)
            at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
            at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
            at android.app.Activity.startActivityForResult(Native Method)
            at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:748)
            at android.app.Activity.startActivity(Activity.java:3714)
            at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
            at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
            at android.app.Activity.startActivity(Native Method)
            at android.app.Activity.startActivity(Activity.java:3682)
            at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
            at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
            at android.app.Activity.startActivity(Native Method)
            at android.content.ContextWrapper.startActivity(ContextWrapper.java:323)
            at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
            at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
            at android.content.ContextWrapper.startActivity(Native Method)
            at android.text.style.URLSpan.onClick(URLSpan.java:62)
            at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:217)
            at android.widget.TextView.onTouchEvent(TextView.java:9180)
            at android.view.View.dispatchTouchEvent(View.java:8107)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2280)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1615)
            at android.app.Dialog.dispatchTouchEvent(Dialog.java:757)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2228)
            at android.view.View.dispatchPointerEvent(View.java:8315)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4596)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4464)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4022)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4076)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4045)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4156)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4053)
            at android.view.ViewRootImpl$Async

What's going wrong?

tZappo
  • 55
  • 1
  • 6

2 Answers2

0

You need to make a call to the users web browser to open it or a call to webview to open in app. You don't have anything coded to cover that.

WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl(www.google.com);

The above is for in app. For pulling the users web client:

getWindow().requestFeature(Window.FEATURE_PROGRESS);
webview.getSettings().setJavaScriptEnabled(true);
final Activiy activity = this;
webview.setWebChromeClient(new WebChromeClient() {


public void onProgressChanged(WebView view, int progress) {
     activity.setProgress(progress * 1000);
   }
 });
 webview.setWebViewClient(new WebViewClient() {
   public void onReceivedError(WebView view, int errorCode, String  description, String failingUrl) {
     Toast.makeText(activity, "Oh no! " + description,     
    Toast.LENGTH_SHORT).show();
       }
     });
    webview.loadUrl("www.google.com");
Jorden
  • 99
  • 6
  • Ok, can you give an example please? – tZappo Aug 24 '15 at 22:46
  • Which are you trying to do? That's a big thing. I'm assuming in app? – Jorden Aug 24 '15 at 22:50
  • Yes, its an Android app. I just want to add the hyperlink to my AlertDialog so that its opening the URL on click in the smartphones default browser (most likely Chrome). It shouldn't be a big thing... Just a small alert dialog. I dont want to create an own Activity for that or something bigger... – tZappo Aug 24 '15 at 22:56
  • add import android.webkit.WebView at the beginning of your script, make sure to add permission for internet use as I did not see that in your script and you still have to make a call to a new webview. All of it goes in onCreate() – Jorden Aug 24 '15 at 23:08
  • I want to avoid a webview because it creates a lot of overhead... Permission for internet is set of course ``. I want to do it with AlertDialog. The link is already drawn, just want to get rid of that error... – tZappo Aug 24 '15 at 23:28
  • I'm going to build it and post it for you when I'm done. – Jorden Aug 25 '15 at 00:00
  • curious, are you testing with AVD or an actual device? – Jorden Aug 25 '15 at 00:00
  • I'm testing with actual device... Thanks for your effort. I guess finally I found a simple solution that works well now after hours of trying... :) See below... – tZappo Aug 25 '15 at 00:26
0

If I use Linkify, the link can be clicked without error and is opened in the webbrowser.

String msg = "<a href=\"http://www.google.com\">Check this link out</a>";

final SpannableString s = new SpannableString(msg);
Linkify.addLinks(s, Linkify.ALL);

AlertDialog ad = new AlertDialog.Builder(this)
                        .setMessage(s)
                        .setTitle("Click a link in AlertDialog")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .create();

TextView textView = (TextView) ad.findViewById(android.R.id.message);
textView.setMovementMethod(LinkMovementMethod.getInstance());

That's ALMOST what I want to achieve... But the link is not rendered correctly... :(

This is the result it prints into my AlertDialog:

< a href=\"http://www.google.com\">Check this link out< /a>

Of course it should show it without HTML tags, like this -> Check this link out ...


So here is the final solution to get well formated Hyperlinks inside AlertDialogs without any errors:

    String msg = "Check this link out, and I will send you to Google.";

    AlertDialog ad = new AlertDialog.Builder(this)
            .setTitle("Open a Hyperlink inside AlertDialog")
            .setMessage(msg)
            .setIcon(android.R.drawable.ic_dialog_info)
            .create();

    ad.show();

    TextView message = (TextView) ad.findViewById(android.R.id.message);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    addLinks(message , "Check this link out", "http://www.google.com");

The only thing we need to do is add a small new function addLinks(Textview, String, String) to our class. Otherwise the generated URL will be wrong when we would use only Linkify.addLinks(TextView textView, Pattern pattern, String scheme).

public static void addLinks(TextView textView, String linkThis, String toThis)
    {
        Pattern pattern = Pattern.compile(linkThis);
        String scheme = toThis;
        Linkify.addLinks(textView, pattern, scheme, new Linkify.MatchFilter() {
            @Override
            public boolean acceptMatch(CharSequence s, int start, int end) {
                return true;
            }
        }, new Linkify.TransformFilter() {

            @Override
            public String transformUrl(Matcher match, String url) {
                return "";
            }
        });
    }

So, this makes it really comfortable! :)

tZappo
  • 55
  • 1
  • 6