1

I tried following the following example to make an HTTP link in my TextView in my alert dialog clickable, but it does not work:

How do I make links in a TextView clickable?

The only way I can get it to work is if I put the text of the link directly into my string and put android:autoLink="web" into my TextView. But I do not want to see the whole link, just the name.

Here is where my dialog is created:

@Override
protected Dialog onCreateDialog(int id) {
    LayoutInflater factory = LayoutInflater.from(this);

    switch (id) {
    case DISCLAIMER:
            final View disclaimerView = factory.inflate(R.layout.disclaimer_dialog, null);
            final TextView dtv = (TextView) disclaimerView.findViewById(R.id.disclaimer_body);

            dtv.setText("v"+versionName+"\n\n\u00A9"+Calendar.getInstance().get(Calendar.YEAR)+" "+"Developer\n\n" +
                getString(R.string.alert_dialog_disclaimer_body));

            dtv.setMovementMethod(LinkMovementMethod.getInstance());                

            return new AlertDialog.Builder(MyActivity.this)                
                .setView(disclaimerView)
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
                .create();
    }

    return null;
}

Here is my TextView located in R.layout.disclaimer_dialog:

<TextView
    android:id="@+id/disclaimer_body"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="10dp"
    android:gravity="center"
    />

Here is my string resource:

<string name="alert_dialog_disclaimer_body">This application was developed and written by me.\n\nGraphs use AChartEngine licensed under <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License 2.0</a>.\n\nThe rest goes here.</string>
Community
  • 1
  • 1
jsstp24n5
  • 594
  • 1
  • 6
  • 13

1 Answers1

1

The documentation to TextView.setText(CharSequence) explains:

Sets the string value of the TextView. TextView does not accept HTML-like formatting, which you can do with text strings in XML resource files. To style your strings, attach android.text.style.* objects to a SpannableString, or see the Available Resource Types documentation for an example of setting formatted text in the XML resource file.

And following their suggestion, Formatting and Styling explains, under the Styling with HTML markup heading, how you can have both HTML markup and formatting codes.

I think the easiest thing in your situation would simply be to adjust your layout to have your copyright notice in one View that you use as you already are, and put the disclaimer below in a second View that you simply leave alone:

<TextView
    android:id="@+id/disclaimer_body"
    android:text="@string/alert_dialog_disclaimer_body"
    ...
    />
Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109