简体   繁体   中英

Link on a Android textView

I am trying to put a link on a textview , but when I click this link the application breaks!! This is my code:

final TextView msg = (TextView)view_aux.findViewById(R.id.accordion_msg);
msg.setText(Html.fromHtml(dbStructure.getMsg()));
msg.setTypeface(tF);
msg.setTextColor(Color.DKGRAY);
msg.setMovementMethod(LinkMovementMethod.getInstance());

Where dbStructure.getMsg() returns a String . This String could be something like:

< a href="/reference/android/widget/RelativeLayout.html">RelativeLayout< /a> 

lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on.

It seems nice, but the app stops when I press it.

EDIT
The error thrown ActivityNotFoundException.

the link you are trying to open is broken

/reference/android/widget/RelativeLayout.html

there is nothing corresponding to the above link.

replace it with the proper url like this

http://developer.android.com/reference/android/widget/RelativeLayout.html

Thank you very much for every one... the problem is (as @Antonio @danidee @TheRedFox and @Arslan say) the format of the url... it doesn´t start with http. With permission from you all, I am going to answer my own question:

final TextView msg = (TextView)view_aux.findViewById(R.id.accordion_msg);
String msg_text = dbStructure.getMsg();
if(msg_text.contains("href=\"")) {
    String[] msg_aux = msg_text.split("<a href=\"");
    if (!msg_aux[1].toLowerCase().startsWith("http"))
        msg_aux[1] = "href=\"http://" + msg_aux[1];
    else
        msg_aux[1] = "href=\"" + msg_aux[1];
    msg_text = msg_aux[0] + msg_aux[1];
}
msg.setText(Html.fromHtml(msg_text));
msg.setTypeface(tF);
msg.setTextColor(Color.DKGRAY);
msg.setMovementMethod(LinkMovementMethod.getInstance());

Thank you.

EDIT on the code, these lines:

else
    msg_aux[1] = "href=\"" + msg_aux[1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM