I was creating a chat application where each time I type some text and press send, I create a new TextView
programmatically and add to my existing LinearLayout
like this -
public void addTextView(LinearLayout view, String text) {
TextView chatTextView = new TextView(getActivity());
chatTextView.setLinksClickable(true);
chatTextView.setMovementMethod(LinkMovementMethod.getInstance());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
chatTextView.setLayoutParams(lp);
// chatTextView.setAutoLinkMask(Linkify.ALL);
chatTextView.setText("me: " + Html.fromHtml(text));
view.addView(chatTextView);
}
As per answer 1 and this question I added chatTextView.setMovementMethod(LinkMovementMethod.getInstance());
to this TextView
but still my link is not clickable. (Testing on an emulator, this code is in a fragment)
Text I am trying to add here is -
<a href="http://www.google.com">url</a>
Have tried -
- autoLink = web (it will highlight direct urls (ie www.google.com) but not hyperlinks (ie href).
- Linkify.WEB_URL
EDIT
I tested right now and found that setMovementMethod
works fine with hyperlinks if textview is from xml layout, but if it is dynamic, it doesn't work.