Search code examples
androidpopupwindowlinkmovementmethod

Creating a URL link within a popup window


In my overflow menu I have a popup window that displays About information. It works fine, but I wanted to add a clickable link to my website and I can't get it to work. The code so far is:

         case R.id.about:
              LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              View popupView = inflater.inflate(R.layout.about_dialog, null);
              PopupWindow pw = new PopupWindow(popupView, width, width, true);
              TextView link = (TextView)findViewById(R.id.urlLink);
              link.setMovementMethod(LinkMovementMethod.getInstance());
              pw.showAtLocation(new LinearLayout(this), Gravity.CENTER, 0, 0);
          return true;

I've defined a string in resources as:

<resources>
    <string name="webLink"><a href="www.google.com">www.google.com</a></string>
</resources>

And the chunk in the XML itself is:

 <TextView
        android:id="@+id/urlLink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="24sp"
        android:textStyle="bold"
        android:gravity="center"
        android:text="@string/webLink" />

It seems to crash when it gets to the setMovementMethod line.

Can anybody see what I've missed? Thanks & best wishes Tony Reynolds


Solution

  • I'm putting up this answer to show that Mike M's comment fixed it.