jueves, 25 de octubre de 2012

How do I make links in a TextView clickable?



Problem:

I need to put anchor html code into a textview and make it clickable in the android application.

Solution:

This goes in the Java code


    // text2 has links specified by putting <a> tags in the string
    // resource.  By default these links will appear but not
    // respond to user input.  To make them active, you need to
    // call setMovementMethod() on the TextView object.

    TextView t2 = (TextView) findViewById(R.id.text2);
    t2.setMovementMethod(LinkMovementMethod.getInstance());



This goes into the xml view


<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="@string/txtCredits"
    android:id="@+id/infoTxtCredits"
    android:layout_below="@+id/imgCredits" android:layout_centerInParent="true"
    android:layout_marginTop="20dp"></TextView>

No hay comentarios:

Publicar un comentario