Android - TextView Hyperlink to another Activity

2

Is there any way to format a TextView so that one of your words is a HyperLink for another app's Activity?

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

Example: Mouse

"Device" input with one to three buttons ...

Device would be a clickable word.

    
asked by anonymous 29.09.2017 / 21:50

1 answer

2

You only use SpannableString and use onClick of 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

    
29.09.2017 / 22:15