Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android hyperlinks on TextView in custom AlertDialog not clickable

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

like image 588
Favolas Avatar asked Aug 22 '12 09:08

Favolas


2 Answers

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" />
like image 193
rajpara Avatar answered Sep 18 '22 04:09

rajpara


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"
like image 44
user370305 Avatar answered Sep 20 '22 04:09

user370305