Android - Textview Hyperlink to another Activity

Asked

Viewed 802 times

2

Is there any way to format a Textview so that one of your words is a Hyperlink to another app Activity?

In this case it is a kind of Dictionary, where in the explanation of the word may have another word that is also registered, and when clicking the word would direct to the Activity with its explanation.

Example: Mouse

"Input device" with one to three buttons...

Device would be a clickable word.

1 answer

2


You just use one SpannableString and use the onClick class ClickableSpan. In this case I created a subclass, in case you want to create a link in more than one word. Ex:

public class MainActivity extends Activity {
TextView textView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);

    SpannableString ss = new SpannableString("Dispositivo de entrada dotado de um a três botões...");

    //aqui você coloca o índice da palavra que você quer, no caso 0 até 11
    ss.setSpan(new CustomClickableSpan(), 0, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

   textView.setText(ss);
   textView.setMovementMethod(LinkMovementMethod.getInstance());
}

class CustomClickableSpan extends ClickableSpan {
    @Override
    public void onClick(View textView) {
    // aqui você abre a Activity que você quer
    // Intent....
   }

    @Override
    public void updateDrawState(TextPaint ds) {
       ds.setColor(Color.BLUE);//cor do texto 
       ds.setUnderlineText(false); //remove sublinhado
    }
}
}

Documentation: Clickablespan, Spannablestring

  • 1

    Man, that’s exactly what I was looking for. Thank you so much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.