6

Following my previous question I still have problems managing hyperlinks on my custom AlertDialog but I believe I narrowed down the problem because I think it only not works because it's a custom dialog.

I have followed all the instructions here and here but can't seem to bypass this situation

On strings.xml I have a string defined this way:

<string name="link_text_manual"><b>text2:</b> This is some other
  text, with a <a href="http://www.google.com">link</a> specified
  via an &lt;a&gt; tag.  Use a \"tel:\" URL
  to <a href="tel:4155551212">dial a phone number</a>.
</string>

If I create my dialog this way:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Data")
        .setIcon(R.drawable.info)
       .setMessage(R.string.link_text_manual)
       .setCancelable(true)
       .setNegativeButton("OK", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
           }
       });

AlertDialog welcomeAlert = builder.create();
welcomeAlert.show();
// Make the textview clickable. Must be called after show()
((TextView)welcomeAlert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());

it shows like this:

Image 1

This works as expected and all clicks are clickable.

Now I want the same thing but on a custom layout.

Created a TextView on my info.xml like this:

<TextView
    android:id="@+id/infoDetailText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/infoVersionTitle"
    android:autoLink="all"
    android:linksClickable="true"
    android:padding="4dp" />

And this is the code:

    Context ctx = this;
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.info, (ViewGroup) findViewById(R.id.infoLayout));

    AlertDialog.Builder about = new AlertDialog.Builder(ctx);
    about.setView(layout);
    about.setTitle("Data");
    about.setIcon(R.drawable.info);

    AlertDialog displayInfo = about.create();
    displayInfo.show();
    // Make the textview clickable. Must be called after show()
    TextView textView = (TextView) displayInfo.findViewById(R.id.infoDetailText);
    textView.setText(R.string.link_text_manual);
    textView.setMovementMethod(LinkMovementMethod.getInstance());

With the above code it shows like this (links not clickable and even not marked as links):

Image 2

If I remove

    textView.setMovementMethod(LinkMovementMethod.getInstance());

it shows like the bellow image (Links marked as links but not clickable):

Image 3

I've also tried to use Linkify and android:autoLink="web" but the result is always the same.

setMessage() works but with a custom layout with a TextView can't make it to work.

Probably this is simple but can't seem to make it work. Any suggestions?

Thanks

6
  • 1
    Try textView.setText(Html.fromHtml(R.string.link_text_manual)); and also set android:clickable attribute to textview in xml Aug 22, 2012 at 9:18
  • @hemanthkumar Thanks. Done that but is the exact same thing as image 2. Links not clickable and even not marked as links
    – Favolas
    Aug 22, 2012 at 9:28
  • @user370305 Thanks but its always the same. With your suggestion it's like image 2. Links not clickable and even not marked as links
    – Favolas
    Aug 22, 2012 at 9:30
  • @user370305 Without textView.setMovementMethod(LinkMovementMethod.getInstance());, textView.setText(Html.fromHtml("Click <a href=\"http://www.poon-world.com\">here</a> to switch on the red light.\n")); It's like image 3. Links marked as links but not clickable. With textView.setMovementMethod(LinkMovementMethod.getInstance()); like image 2.
    – Favolas
    Aug 22, 2012 at 9:35
  • possible duplicate of How do I make links in a TextView clickable?
    – Wroclai
    Aug 22, 2012 at 9:52

3 Answers 3

7

You defined the TextView without android:autoLink="all" , android:clickable="true" & android:linksClickable="false"

For info.xml TextView as below

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/infoDetailText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="4dp" />
1
  • 1
    Finally. I already have tried Html.fromHtml but with no success. Since the only difference was the way you defined the TextView went back to my TextView and removed android:autoLink="all" android:clickable="true" android:linksClickable="false" Now it works even with getting the string as textView.setText(R.string.link_text_manual); Many Many thanks
    – Favolas
    Aug 22, 2012 at 10:10
7

Anyway, your code works perfectly,...

Code for AlertDialog:

LayoutInflater inflater = LayoutInflater.from(ctx);
View layout = inflater.inflate(R.layout.info, null);
AlertDialog.Builder about = new AlertDialog.Builder(ctx);
about.setTitle("Data");
about.setIcon(R.drawable.info);

// Make the textview clickable. Must be called after show()
TextView textView = (TextView) layout.findViewById(R.id.infoDetailText);
textView.setText(R.string.link_text_manual);
textView.setMovementMethod(LinkMovementMethod.getInstance());
about.setView(layout);
AlertDialog displayInfo = about.create();
displayInfo.show();

Code of TextView XML:

<TextView
    android:id="@+id/infoDetailText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/infoVersionTitle"
    android:padding="4dp" />

Removed two attributes..

android:autoLink="all"
android:linksClickable="true"
1
  • 1
    Thanks. Following @rajpara answer figured that out so marked is as the correct answer. Upvoted yours. I can't believe how long it took. It's strange how removing those attributes made it work when in all other threads they were always recommended... Anyway, many thanks for your time and efford
    – Favolas
    Aug 22, 2012 at 10:13
0

Try this

setText(Html.fromHtml(String));
2
  • Thanks but textView.setText(Html.fromHtml(getString(R.string.link_text_manual)));is the exact same thing as the second image.
    – Favolas
    Aug 22, 2012 at 9:24
  • Also set this property for the text view. tv.setMovementMethod(LinkMovementMethod.getInstance());
    – Kamal
    Aug 22, 2012 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.